0
votes

I'm trying to make a script that will be a shortcut for some folder structure generation. Basically I have a template .ahk script that I have adapted to my needs, and a simple .bat script that creates the folders that I need created.

The .ahk script, calls upon the .bat script. The issue is, the .ahk script puts the .bat script location into clipboard, and should paste it into the cmd window that the .ahk opens in the directory that I execute the shortcut in. However, the paste function doesn't work. The CMD terminal shows the following:

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

This is my .ahk script:

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:\ "
    }
}

And this is the .bat script it calls upon:

@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"

The tricky part in the .ahk script is: Send ^v {Enter}

I can't understand why it doesn't paste it automatically. When I press ctrl+v manually, it pastes the string, and it executes the .bat script as it is supposed to.

I've tried doing it with Send {Ctrl down} {v} {Ctrl up} {enter} it still returns the same line in the cmd window.

Any ideas for a solution?

Thanks.

2
I am almost sure the problem will be solved if you put more Sleep before and after that line (you have one commented out). Just make both values 1000 ms and see how it works. - Mikhail V

2 Answers

0
votes

Okay, I found a solution which is a much neater than calling one script with another. Basically, did some more research, and managed to create the folder structure with AutoHotkey alone, without the need to use batch commands at all.

Here is the messy script if someone else needs it one day.

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.
    }
}
0
votes

I agree that including everything in one script is better. But here's just a little tidbit about the CMD window. If you right-click the title bar and select "edit", you have an option to paste. Also, I'm not sure if it has always worked this way (pre-Win10), but merely right-clicking in an open space will also paste.