Autohotkey脚本不会将剪贴板粘贴到CMD窗口中

时间:2019-01-11 16:25:41

标签: batch-file autohotkey

我正在尝试创建一个脚本,该脚本将成为某些文件夹结构生成的快捷方式。基本上,我有一个模板.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窗口中返回同一行。

有解决方案的想法吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

好吧,我找到了一个解决方案,它比另一个调用一个脚本要整洁得多。基本上,进行了更多研究,并设法仅使用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.
    }
}

答案 1 :(得分:0)

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