Continued: AHK transparent window overlay, dictionaries and including scripts (plus Premier macro for generating 2 second frames)
I got some questions after the last blog post and therefore I decided to show another script, similar to the one I posted last. I was asked why I didn’t simply use a background color instead of the image background and here is why. The hotkey window I created loads up different images depending on the hotkey and the context I use the hotkey in and the script was a scaled down version of my original script.
For example I have several macros that I use with Premiere, a hardcore video editing software I use to edit Pluralsight videos. All our videos starts and ends with 2 second of sile
nce, and often I need to insert ‘frozen frames’.
Window for hotkey (zoomed in). I probably should aim for other colors, but you get the point :)
You have a few ways to do that in Adobe, using freeze frames and image snapshots of the video. I use t
he latter method as I can automate that using keyboard shortcuts and AHK. So in the tiny script below I:
- Take a screen shot from where I am currently in the timeline. Prior to running that macro I use Shift + 3 to get to the timeline window, then Home will take me to the start, and End and then Left to my very last frame. Shift +3 while in the timeline window will let me tab through the sequences (I use one for each submodule).
- I wait a little bit for the image to be generated
- Then I set the length of the clip to 2 seconds
My create frame function:
CreateFrame() {
SendInput ^+e
Sleep, 500
SendInput {Enter}
Sleep, 500
SendInput ^r
SendInput {Tab}
SendRaw 032
SendInput {Tab}
SendInput {Enter}
}
I’ll get back to the CreateWindow function to explain the second parameter. First let’s go back to the question about GUI backgrounds. To set he background color of a GUI you simply use
Gui, Color, 000000
The above sets the background to black, but you can of course use any color you would like to use. The images I use have rounded corners and therefore I need window transparency (which I removed in the previous script to keep the script terse). The text background also has to explicitly be set to transparent, otherwise it will inherit the color from the window, or use the default color. There are a few ways to deal with image transparency, but I simply set a background color on the window, then set the same color as transparent. For simpler images that works fine, for more complex ones you might want to use the GDI library to avoid crispy/pixelated edges.
Gui,Color, 000000
Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption
WinSet, TransColor, 000000
Gui, Add, Picture, x0 y0 w%bgWidth% h%bgHeight%, %bgPath%
; more things here
Gui, Add, Text, xm y12 x%xPlacementTextShadow% BackgroundTrans,%key%
; more things here
Gui, Show, x%xPlacement% y%yPlacement% NoActivate
Notice the WinSet, TransColor, 000000 and BackgroundTrans on the Text control
I’ve given the text a slight shadow effect by overlaying the text with a lighter color, that line can certainly be refactored a little bit :) Let’s talk about my %bgPath% variable.
The way I switch between images is by keeping a lookup dictionary in the script, with each key holding the filepath to the actual image for the background. Support for objects was added in AHK 1.* and with that an associative arrays (i.e. dictionary). Docs for objects can be found here, have a read for creating more manageable scripts
Here is my associative array/ dictionary declaration, and retrieving the value via the key (the key is passed in as a parameter in the function). The dictionary is trimmed down and is not using my explicitly declared script folder- you might want to do that. Script bits:
bgLookup := { “pr” : “C:\Users\IrisDaniela\Pictures\prbg.png”, “vs” : “C:\Users\IrisDaniela\Pictures\vsbg.png” }
bgPath := bgLookup[bg]
Notice the := which is how you assign quotated values to a variable. Forget that and you might be in for some fun debugging.
A question that never came up but I think is important, is how to we break up the scripts for reusability? I have a separate script for the creation of the window- and I think its adaptive enough to be used for different things. For each program that I have declared hotkey macros for I create a separate script to avoid them getting to long or loading up something I don’t need. I then include the window script at the top of my scrip so I can access my functions inside the window.ahk script. To include a script you sijmply use #include and then the path:
#include C:\Users\IrisDaniela\Documents\window.ahk
The above is at the top of my script for premier hotkeys.
To make sure I can’t run my magic commands when I’m not in the right contaxt, in other words when I don’t have the right window active, I use IfWinActive function passing in Premiere. Just as is it won’t give a match as the current project I’m editing will be added to the window name, so I need to match on whether or not the window title contains ‘Premiere’. The way you do that is by using SetTitleMatchMode, 2 before calling IfWinActive.
The keyboard shortcut (hotkey):
#f::
SetTitleMatchMode, 2
IfWinActive, Premiere
{
CreateWindow(“Win + F”, “pr”)
CreateFrame()
}
return
Here is the full script- window.ahk
CreateWindow(key,bg){
bgLookup := { “pr” : “C:\Users\IrisDaniela\Pictures\prbg.png”, “vs” : “C:\Users\IrisDaniela\Pictures\vsbg.png” }
GetTextSize(key,35,Verdana,height,width)
bgTopPadding = 30
bgWidthPadding = 90
bgHeight = % height + bgTopPadding
bgWidth = % width + bgWidthPadding
padding = 20
yPlacement = % A_ScreenHeight - (bgHeight + padding)
xPlacement = % A_ScreenWidth - bgWidth - padding
xPlacementText = % bgWidthPadding / 2
xPlacementTextShadow= % xPlacementText + 2
Gui, Margin, 0, 0
Gui,Color, 000000
Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption
WinSet, TransColor, 000000
bgPath := bgLookup[bg]
Gui, Add, Picture, x0 y0 w%bgWidth% h%bgHeight%, %bgPath%
Gui, Font, s35 c555555, Segoe UI SemiLight
Gui, Add, Text, xm y12 x%xPlacementTextShadow% BackgroundTrans,%key%
Gui, Font, s35 ccccccc, Segoe UI SemiLight
Gui, Add, Text, xm y10 x%xPlacementText% BackgroundTrans,%key%
Gui,+AlwaysOnTop -Border -SysMenu -Caption
Gui, Show, x%xPlacement% y%yPlacement% NoActivate
SetTimer, RemoveGui, 7000
}
GetTextSize(str, size, font,ByRef height,ByRef width) {
Gui temp: Font, s%size%, %font%
Gui temp:Add, Text, , %str%
GuiControlGet T, temp:Pos, Static1
Gui temp:Destroy
height = % TH
width = % TW
}
RemoveGui:
Gui, Destroy
return
Parts of my Premiere script:
#f::
SetTitleMatchMode, 2
IfWinActive, Premiere
{
CreateWindow(“Win + F”, “pr”)
CreateFrame()
}
return
CreateFrame() {
SendInput ^+e
Sleep, 500
SendInput {Enter}
Sleep, 500
SendInput ^r
SendInput {Tab}
SendRaw 032
SendInput {Tab}
SendInput {Enter}
}
Comments
Last modified on 2015-02-05