【问题标题】:Creating AutoHotKey shortcut to open powershell in specific folder in file explorer and excute certain code创建 AutoHotKey 快捷方式以在文件资源管理器的特定文件夹中打开 powershell 并执行某些代码
【发布时间】:2018-11-21 07:53:48
【问题描述】:

我想创建一个 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。

【问题讨论】:

  • 或者,您可以使用上下文菜单处理程序调用脚本,以便您可以右键单击文件夹并选择“重命名 JPG 文件”之类的内容。这样可以避免一直在后台运行 AHK 文件。

标签: powershell autohotkey


【解决方案1】:
#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2010-09-24
    • 2012-09-29
    • 1970-01-01
    • 2015-04-27
    相关资源
    最近更新 更多