首页 > 解决方案 > 一个ahk文件中的2个脚本不起作用,但一个接一个起作用?

问题描述

他们一次一个地工作。但是,它们不能在单个 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

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

标签: autohotkey

解决方案


您不能在一个脚本中两次或在不同位置使用AutoExecute 部分。

; 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

推荐阅读