Autohotkey脚本打开命令提示符

时间:2013-08-09 04:56:22

标签: cmd command-prompt autohotkey

我从AutoHotKey论坛收集了一个脚本,该脚本允许我在Windows资源管理器中打开的位置打开命令提示符。如果当前窗口不是资源管理器窗口,则会在脚本所在的位置打开提示。如果当前窗口不是资源管理器窗口,我想更改此行为并从C:\打开它。我试图编辑脚本,但它没有按预期工作。

#ifwinactive, ahk_class CabinetWClass
ControlGetText, address , edit1, ahk_class CabinetWClass
if (address <> "") {
Run, cmd.exe, %address%
}
else {
Run, cmd.exe, "C:"
}
ExitApp
#ifwinactive

6 个答案:

答案 0 :(得分:8)

在c:\ path中运行cmd.exe的命令是

运行,cmd.exe,c:\

每次运行cmd窗口的完整脚本看起来像这样

SetTitleMatchMode, 2
ifwinactive, ahk_class CabinetWClass
  ControlGetText, address , edit1, ahk_class CabinetWClass
else
  address =

; Exclude specific windows

ifwinactive, My Computer
  address =
ifwinactive, My Documents
  address =

if (address <> "") 
  Run, cmd.exe, %address%
else 
  Run, cmd.exe, C:\

ExitApp

答案 1 :(得分:7)

我意识到这是一个古老的问题,但我自己正在研究这个问题,并有一个更好的解决方案。

Windows有两种内置方法可以在当前资源管理器窗口的路径上启动cmd。按Shift +右键单击,然后单击打开命令窗口(或按w)。您也可以按alt + d,键入cmd,然后按Enter键。所以......

LWin & Return::
if WinActive("ahk_class CabinetWClass") 
or WinActive("ahk_class ExploreWClass")
{
  Send {Shift Down}{AppsKey}{Shift Up}
  Sleep 10
  Send w{enter}
}
else
{
  run, cmd, C:\
}
return

没有直接从资源管理器中抓取地址! :)

答案 2 :(得分:1)

这是来自AHK forums

的非常复杂的脚本
#NoEnv
#SingleInstance Force
#NoTrayIcon

SendMode Input
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode RegEx

#IfWinActive ahk_class ExploreWClass|CabinetWClass|Progman
#c::
    WinGetClass WinClass
    If ( WinClass = "Progman" )
    {
        Run %ComSpec% /K cd /D "C:\"
        Return
    }

    If ( InStr( "WIN_7,WIN_VISTA" , A_OSVersion ) )
    {
        ControlGetText, Path, ToolbarWindow322
        RegExMatch(Path, ":\s*(.*)", Path)
        Path := Path1
    }
    Else
    {
        ; Windows XP doesn't know the Edit1 control exists if
        ; the Address Bar is hidden, so check if it exists and temporarly
        ; show the Address bar if needed. Temporarly showing the Address bar
        ; will register the Edit1 control, which contains the path.
        ControlGetPos Edit1Pos , , , , Edit1
        If ( !Edit1Pos )
        {
            PostMessage 0x111 , 41477 , 0 ,  , A ; Show Address Bar
            Sleep 100
            PostMessage 0x111 , 41477 , 0 ,  , A ; Hide Address Bar
        }
        ControlGetText Path , Edit1
    }

    If ( InStr( Path , ":" ) )
    ; If(  InStr( Path , ":" ) && FileExist(Path) )
        Run %ComSpec% /K cd /D "%Path%"
    Else
        Run %ComSpec% /K cd /D "C:\"
Return

我稍微调整了WIN_7部分,因此代码独立于不可靠的Edit1控件,它并不总是暴露当前的资源管理器位置或不正确的位置。 If ( InStr( Path , ":" ) )确保Windows 7上没有自定义路径,如Computer或Windows XP上My Computer。我还添加了一个替代条件,如果你想对冲你的赌注,还会另外检查存在的路径。

答案 3 :(得分:1)

无法得到其他工作答案(自写完以来已经过了几年)。

我最后写了这个剧本:

#o::
    Send {Alt down}D{Alt up}cmd{enter}
return

答案 4 :(得分:1)

保持简单。除非你需要复杂性。

!f1::
    run, C:\Windows\System32\cmd.exe
return

!f1表示 Alt + F1 。为了我个人的喜好。把它改成你喜欢的任何东西。

答案 5 :(得分:1)

另一个从here入侵的解决方案。在Windows 10上适合我,但我承认它是全部复制粘贴。发布该邮件的目的是希望能够避免其他人对AHK脚本的恐惧。

;; Open terminal in current Explorer window folder
#If WinActive("ahk_class CabinetWClass") ; explorer

    F4::
    WinGetTitle, ActiveTitle, A
    If InStr(ActiveTitle, "\")  ; If the full path is displayed in the title bar (Folder Options)
        Fullpath := ActiveTitle
    else
    If InStr(ActiveTitle, ":") ; If the title displayed is something like "DriveName (C:)"
    {
        Fullpath := SubStr(ActiveTitle, -2)
        Fullpath := SubStr(Fullpath, 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 Fullpath := window.Document.Folder.Self.Path
        SplitPath, Fullpath, title
        If (title = ActiveTitle)
            break
    }
    Run, cmd.exe, %Fullpath%
    return 

#If
相关问题