【问题标题】:Autohotkey script to open command prompt用于打开命令提示符的 Autohotkey 脚本
【发布时间】:2013-08-11 00:07:12
【问题描述】:

我从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

【问题讨论】:

  • 您使用的是哪个 Windows 版本?

标签: cmd command-prompt autohotkey


【解决方案1】:

在c:\路径下运行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

【讨论】:

  • #IfwinactiveIfwinactive有什么区别?
  • 我发现了一个警告。当我在 My Computer 窗口中时,提示从脚本运行的位置开始。
  • 不同之处在于您可以在“#ifwinactive”块中定义热键,这些热键仅在 winodw 处于活动状态时才处于活动状态。这就是“#ifwinactive 命令”的目的。
  • 针对“我的​​电脑”问题。 “IfWinActive”命令还允许您排除某些窗口。请在“IfWinActive / IfWinNotActive”上查找自动热键帮助文件,其中对此进行了详细描述。
  • 我查看了 ExcludeTitle 属性,但我不知道如何将此 My Computer 传递到此排除列表。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    无法获得其他工作答案(它们已经写了几年了)。

    我最终写了这个脚本:

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

    【讨论】:

      【解决方案4】:

      这是来自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 这样的自定义路径。如果您想对冲您的赌注,我还添加了一个替代条件,该条件还检查是否存在路径。

      【讨论】:

      • @SohamDasgupta 好吧,这根本不是很具体。您必须按WIN+C 才能触发该功能。
      • @SohamDasgupta 我只发布了热键本身,当然你必须调用SetTitleMatchMode, RegEx,如链接线程中所述。我也添加了自动执行部分。
      【解决方案5】:

      保持简单。当然,除非您需要复杂性。

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

      !f1 表示 Alt+F1。对于我个人的喜好。把它改成你喜欢的。

      【讨论】:

        【解决方案6】:

        另一个解决方案来自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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-29
          • 2019-12-15
          相关资源
          最近更新 更多