一个ahk文件中的2个脚本不起作用,但是一个接一个地工作?

时间:2019-02-12 14:41:15

标签: autohotkey

他们一次工作一次。但是,它们不能在单个ahk文件中工作。告诉我您需要解决什么?

; 1)脚本使用以下命令将文本和链接在浏览器中复制到剪贴板     ; F2按钮

; Allow partial titlebar text matches.
SetTitleMatchMode, 2

; Create a window title group to match on for our hotkey. 
GroupAdd, myBrowsers, Mozilla Firefox
GroupAdd, myBrowsers, Google Chrome


#IfWinActive, ahk_group myBrowsers
{
F2::
{
    ; Copy user selection to a variable.
    SendInput, ^c
Sleep, 50
    myText := Clipboard
    Sleep, 100

    ; Switch to address bar in browser and copy URL to another variable.
    SendInput, ^l
    Sleep, 100
    SendInput, ^c
    Sleep, 100
    myURL := Clipboard
    Sleep, 100

    ; Format data bits.     
    ;myData := myURL . "`r`n`r`n" .
    myData := myText . "`r`n`r`n" . myURL
    Sleep, 100

    ; Put formatted data back onto clipboard.
    Clipboard := myData
}
Return
} 
#IfWinActive
return

; 2)脚本通过alt + n

在记事本中的资源管理器中打开任何文件
; AutoExecute Section must be on the top of the script
#NoEnv
SetWorkingDir %A_ScriptDir%
GroupAdd, Explore, ahk_class CabinetWClass         ; Add a class to the 
group
GroupAdd, Explore, ahk_class ExploreWClass         ; Add a other class to 
the group


; Always on Top CTRL+SPACE on current window
; http://www.labnol.org/software/tutorials/keep-window-always-on- 
top/5213/
^SPACE::  Winset, Alwaysontop, , A


; Open file With Notepad++ from Explorer using Alt+N hotkey
; https://autohotkey.com/board/topic/77665-open-files-with-portable- 
notepad/

#ifWinActive,ahk_group Explore              ; Set hotkeys to work in 
explorer only
; Alt+N
!n::
ClipSaved := ClipboardAll
Clipboard := ""
 Send ^c
ClipWait, 0.5
file := Clipboard
Clipboard := ClipSaved
Run C:\Program Files (x86)\Notepad++\notepad++.exe "%file%"
return

为什么这些脚本一个接一个地工作,但是不能在一个文件中工作。您能推荐什么?

1 个答案:

答案 0 :(得分:1)

您不能两次AutoExecute Section,也不能在一个脚本中的不同位置。

; AutoExecute Section must be on the top of the script

#NoEnv
SetWorkingDir %A_ScriptDir%

; Create a window title group to match on for our hotkey. 
GroupAdd, myBrowsers, Mozilla Firefox
GroupAdd, myBrowsers, Google Chrome

GroupAdd, Explore, ahk_class CabinetWClass        ; Add a class to the group
GroupAdd, Explore, ahk_class ExploreWClass         ; Add a other class to the group

; Allow partial titlebar text matches.
SetTitleMatchMode, 2

 ; === end of auto-execute section ===



; Always on Top CTRL+SPACE on current window
; http://www.labnol.org/software/tutorials/keep-window-always-on-top/5213/

^SPACE::  Winset, Alwaysontop, , A



; https://autohotkey.com/docs/commands/_IfWinActive.htm

#IfWinActive,ahk_group Explore  ; Set hotkeys to work in explorer only

    ; Open any file in explorer in notepad by alt + n
    !n::
        ClipSaved := ClipboardAll
        Clipboard := ""
         Send ^c
        ClipWait, 0.5
        file := Clipboard
        Clipboard := ClipSaved
        Run C:\Program Files (x86)\Notepad++\notepad++.exe "%file%"
    return

#IfWinActive, ahk_group myBrowsers

    ; copies the text and link in the browser to the clipboard using the F2 button
    F2::
        ; Copy user selection to a variable.
        SendInput, ^c
        Sleep, 50
        myText := Clipboard
        Sleep, 100
        ; Switch to address bar in browser and copy URL to another variable.
        SendInput, ^l
        Sleep, 100
        SendInput, ^c
        Sleep, 100
        myURL := Clipboard
        Sleep, 100
        ; Format data bits.     
        ;myData := myURL . "`r`n`r`n" .
        myData := myText . "`r`n`r`n" . myURL
        Sleep, 100
        ; Put formatted data back onto clipboard.
        Clipboard := myData
    Return

#IfWinActive  ; turn off context sensitivity