I have just done a piece of code that does the following thing. When I make a selection by mouse in Firefox or EndNote, the script sents a Ctrl+c and checks the clipboard for a regex match. If there is a match, it changes the clipboard contents and shows a tooltip. It works fine for these two programs. Adobe Acrobat sometimes shows an error when a Ctrl+c is sent (even if a user presses a ctrl-c Acrobat sometimes shows famous "There was an error while copying to the Clipboard. An internal error occurred). So it decided to assign an F9 hotkey, but it works for all programs and not just for Acrobat. How do I assign an hotkey for only one window – Acrobat? Here's my code. I know it's lame – I am a newbie to programming in general, and in AHK in particular.
#If WinActive("ahk_exe firefox.exe") || WinActive("ahk_exe EndNote.exe") || WinActive("ahk_exe Acrobat.exe")
if WinActive("ahk_exe Acrobat.exe")
F9::
{
Clipboard:=""
send,^c
ClipWait, 1
ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer, ToolTipOff, -1000
}
return
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 500 )
{
Send ^c
if WinActive("ahk_exe firefox.exe")
{
If RegExMatch(Clipboard, "[0-9]\.\s[A-Za-z,]*\s[A-Za-z]*")
{
regex := "[0-9]\.\s*|\s?\([^)]*\)|\."
replace := ""
}
else If RegExMatch(Clipboard,"[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{4}")
{
Clipboard := RegExReplace(Clipboard, "^0", "")
regex := "\/"
replace := "."
}
else return
}
else if WinActive("ahk_exe EndNote.exe")
{
If RegExMatch(Clipboard, "[a-z]+\,\s[A-Z0-9‘“]")
{
regex := "\??!?\:|\?|!"
replace := "."
}
else return
}
ToolTip % Clipboard := RegExReplace(Clipboard, regex, replace)
SetTimer, ToolTipOff, -1000
}
return
#If
ToolTipOff:
ToolTip
return
I see some very fundamental problems in the first few lines. Let me explain...
There are two types of if-statements in AutoHotkey
If
and#If
. You usually always use the normalIf
-statements unless you are doing something with hotkeys and you want specific hotkeys to be context-sensitive.Here are some important rules:
Normal
If
-statements have to use curly braces {} to mark the area of code that should be executed if the expression is true. If you don't use curly braces, theIf
-statement will work as if you had put curly braces around the first command directly under theIf
-statement.Example:
Another example:
Normal
If
-statements cannot be used around a hotkey definition, but only within it.This is allowed:
This is NOT:
But there is a way around that and that is
#If
-statements.#If
-statements don't use curly braces ever.They can only be used on hotkey definitions.
And they can only be closed by another
#If
-statement.(It's very common to simply use an empty
#If
to close it.)Examples:
A more complex example:
As you might have guessed by now, hotkey definitions are always started by a pattern like this
hotkey::
and closed by aReturn
. Although you can define hotkeys on a single line. Examples:Hotkeys by themselves do never use curly braces! Though an
If
-statement within a hotkey still has to use them according to the before mentioned rules.