【问题标题】:Autohotkey Script doesn't paste clipboard into CMD windowAutohotkey 脚本不会将剪贴板粘贴到 CMD 窗口中
【发布时间】:2019-06-06 14:33:31
【问题描述】:

我正在尝试制作一个脚本,该脚本将成为某些文件夹结构生成的快捷方式。基本上,我有一个模板 .ahk 脚本,我已经适应了我的需要,还有一个简单的 .bat 脚本,可以创建我需要创建的文件夹。

.ahk 脚本调用 .bat 脚本。问题是,.ahk 脚本将 .bat 脚本位置放入剪贴板,并应将其粘贴到 .ahk 在我执行快捷方式的目录中打开的 cmd 窗口中。但是,粘贴功能不起作用。 CMD终端显示如下:

F:\Documents\Scripts\Template Scripts\Reference Media>v
'v' is  not recognized as an internal or external command, operable program or batch file.

这是我的 .ahk 脚本:

SetTitleMatchMode RegEx
return

; Stuff to do when Windows Explorer is open
;
#IfWinActive ahk_class ExploreWClass|CabinetWClass

    ; create new text file
    ;
    #t::Send !fwt

    ; open 'cmd' in the current directory
    ;
    #c::
        OpenCmdInCurrent()
    return
#IfWinActive


; Opens the command shell 'cmd' in the directory browsed in Explorer.
; Note: expecting to be run when the active window is Explorer.
;
OpenCmdInCurrent()
{
    ; This is required to get the full path of the file from the address bar
    WinGetText, full_path, A

    ; Split on newline (`n)
    StringSplit, word_array, full_path, `n

    ; Find and take the element from the array that contains address
    Loop, %word_array0%
    {
        IfInString, word_array%A_Index%, Address
        {
            full_path := word_array%A_Index%
            break
        }
    }  

    ; strip to bare address
    full_path := RegExReplace(full_path, "^Address: ", "")

    ; Just in case - remove all carriage returns (`r)
    StringReplace, full_path, full_path, `r, , all


    IfInString full_path, \
    {
        Run,  cmd /K cd /D "%full_path%"
        WinWait, ahk_exe cmd.exe
        ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
        clipboard := ""           ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
        clipboard = ( call "F:\Documents\Scripts\Template Scripts\CreateFolderTemplate.bat" )            ; copy this text:
        ClipWait, 2              ; wait max. 2 seconds for the clipboard to contain data. 
        if (!ErrorLevel)         ; If NOT ErrorLevel, ClipWait found data on the clipboard
        ;Sleep 250
        Send ^v {Enter}  ; paste the text
        Sleep 300
        clipboard := ClipSaved   ; restore original clipboard
        ClipSaved =              ; Free the memory in case the clipboard was very large.
        return
    }
    else
    {
        Run, cmd /K cd /D "C:\ "
    }
}

这是它调用的 .bat 脚本:

@echo off

md Approvals     
md "Behind The Scenes"
md "Client Notes"
md "Delivery/YYYY-DD-MM"
md "Other Media"
md "Personal Notes"
md "Project Files"/"Capture One"
md "Project Media (if applicable"
md "Production Media"
md "Reference Media"
cd "Production Media"
md "Media From Client"
md "Other Media"

.ahk 脚本中棘手的部分是:Send ^v {Enter}

我不明白为什么它不自动粘贴。当我手动按 ctrl+v 时,它会粘贴字符串,并按预期执行 .bat 脚本。

我已经尝试使用 Send {Ctrl down} {v} {Ctrl up} {enter} 它仍然在 cmd 窗口中返回同一行。

有解决方案的想法吗?

谢谢。

【问题讨论】:

  • 我几乎可以肯定,如果您在该行之前和之后放置更多 Sleep (您已注释掉一个),问题将得到解决。只需将两个值都设为 1000 毫秒,然后看看它是如何工作的。

标签: batch-file autohotkey


【解决方案1】:

好的,我找到了一个比用另一个脚本调用一个脚本更简洁的解决方案。基本上,做了更多的研究,并设法单独使用 AutoHotkey 创建文件夹结构,根本不需要使用批处理命令。

如果有一天其他人需要它,这是乱七八糟的脚本。

SetTitleMatchMode RegEx
return

; Stuff to do when Windows Explorer is open
;
#IfWinActive ahk_class ExploreWClass|CabinetWClass

    ; open 'cmd' in the current directory
    ;
    #c::
        MakeFoldersInCurrent()
    return
#IfWinActive


; Note: expecting to be run when the active window is Explorer.
;
MakeFoldersInCurrent()
{
    ; This is required to get the full path of the file from the address bar
    WinGetText, full_path, A

    ; Split on newline (`n)
    StringSplit, word_array, full_path, `n

    ; Find and take the element from the array that contains address
    Loop, %word_array0%
    {
        IfInString, word_array%A_Index%, Address
        {
            full_path := word_array%A_Index%
            break
        }
    }  

    ; strip to bare address
    full_path := RegExReplace(full_path, "^Address: ", "")

    ; Just in case - remove all carriage returns (`r)
    StringReplace, full_path, full_path, `r, , all


    IfInString full_path, \
    {
        WinWait, ahk_exe 
        FileCreateDir %full_path%\Approvals
        FileCreateDir %full_path%\Behind The Scenes
        FileCreateDir %full_path%\Client Notes
        FileCreateDir %full_path%\Delivery\YYYY-MM-DD
        FileCreateDir %full_path%\Other Media
        FileCreateDir %full_path%\Personal Notes
        FileCreateDir %full_path%\Production Media\Media From Client
        FileCreateDir %full_path%\Production Media\Other Media
        FileCreateDir %full_path%\Project Files\Capture One
        FileCreateDir %full_path%\Project Files\Photoshop
        FileCreateDir %full_path%\Project Media
        FileCreateDir %full_path%\Reference Media
        clipboard := ""           ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
        clipboard = "%full_path%\Project Files\Capture One"            ; copy this text in clipboard // I need it to paste in CaptureOne directly.
        ClipWait, 2              ; wait max. 2 seconds for the clipboard to contain data. // Not sure if necessary at all.
        if (!ErrorLevel)         ; If NOT ErrorLevel, ClipWait found data on the clipboard
        Sleep 300
        return
    }
    else
    {
        return ; not sure if else clause is needed at all.
    }
}

【讨论】:

    【解决方案2】:

    我同意将所有内容都包含在一个脚本中会更好。但这里只是关于 CMD 窗口的一点小知识。如果您右键单击标题栏并选择“编辑”,您可以选择粘贴。另外,我不确定它是否一直以这种方式工作(Win10 之前),但只需在空白处右键单击也会粘贴。

    【讨论】:

    • right-clicking in an open space will also paste - 仅在启用 QuickEditMode 时(不在新安装的窗口上)
    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    相关资源
    最近更新 更多