-1

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

1 Answer 1

4

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 normal If-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, the If-statement will work as if you had put curly braces around the first command directly under the If-statement.
Example:

If WinActive("Firefox") {
    Send, Test
    MsgBox, The script just typed "Test.
}

Another example:

If WinActive("Firefox")
    MsgBox, Firefox is the active window.

Normal If-statements cannot be used around a hotkey definition, but only within it.

This is allowed:

F1::
    If (A_OSVersion = "WIN_7") {
        MsgBox, Your operating system is Windows 7 and you just pressed F1.
    }
Return

This is NOT:

If (A_OSVersion = "WIN_7") {
    F1::
        MsgBox, Your operating system is Windows 7 and you just pressed F1.
    Return
}

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:

#If (A_OSVersion = "WIN_7")
    F1::
        MsgBox, Your operating system is Windows 7 and you just pressed F1.
    Return
#If

A more complex example:

#If (A_ScreenWidth >= 1920)
    F1::
        MsgBox, Your your screen is at least 1920 pixels wide.
    Return
    F2::
        MsgBox, Your operating system is %A_OSVersion%.
    Return
#If (A_ScreenWidth < 1920)
    F1::
        MsgBox, Your your screen width is smaller than 1920 pixels.
    Return
#If

As you might have guessed by now, hotkey definitions are always started by a pattern like this hotkey:: and closed by a Return. Although you can define hotkeys on a single line. Examples:

F1::MsgBox, Hello!
F2::a ;This will remap the F2 key to an a-key. 

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.

1
  • Thank you. I don't have access to AHK right now, but I think you answer will help me a lot. I will get back if I have a problem with the code.
    – menteith
    Mar 23, 2016 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.