创建AutoHotKey快捷方式以在文件资源管理器中的特定文件夹中打开Powershell并执行某些代码

时间:2018-11-21 07:53:48

标签: powershell autohotkey

我想创建一个AutoHotKey脚本,该脚本将打开powershell控制台(在文件资源管理器窗口中打开的同一文件夹中)并执行以下代码:

$nrRef = [ref] 0
Get-ChildItem -Filter *.jpg | Rename-Item -Newname {
'{0}_{1:d3}.jpg' -f (Split-Path -Leaf $_.DirectoryName), ++$nrRef.Value}

然后关闭Powershell控制台窗口。

注意:启动AHK脚本的快捷方式:在资源管理器窗口的特定文件夹中按下Ctrl + Shift + LeftMouseButton。

1 个答案:

答案 0 :(得分:0)

#NoEnv
#SingleInstance Force

#IfWinActive, ahk_class CabinetWClass ; explorer

    ; Ctrl + Shift + LeftMouseButton
    ^+LButton::
        ActivePath := GetExplorerActivePath()
        code =
        (
        $nrRef = [ref] 0
        Get-ChildItem -Filter *.jpg | Rename-Item -Newname {
        '{0}_{1:d3}.jpg' -f (Split-Path -Leaf $_.DirectoryName), ++$nrRef.Value}
        )
        Run powershell.exe -windowstyle hidden -Command &{%code%}, %ActivePath%
    return

#IfWinActive

; get the path of the active file explorer:
GetExplorerActivePath(){
    WinGetTitle, ActiveTitle, A
    If InStr(ActiveTitle, "\")  ; If the full path is displayed in the title bar (Folder Options)
        ActivePath := ActiveTitle
    else
    If InStr(ActiveTitle, ":") ; If the title displayed is something like "DriveName (C:)"
    {
        ActivePath := SubStr(ActiveTitle, -2)
        ActivePath := SubStr(ActivePath, 1, -1)
    }
    else ; If the full path is NOT displayed in the title bar 
    ; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
    for window in ComObjCreate("Shell.Application").Windows
    {
        try ActivePath := window.Document.Folder.Self.Path
        SplitPath, ActivePath, title
        If (title = ActiveTitle)
            break
    }
    return ActivePath
}

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