Autohotkey script to open command prompt

前提是你 提交于 2019-12-07 05:46:30

问题


I've collected a script from the AutoHotKey forum which lets me open a command prompt at the location I'm open in windows explorer. If the current window is not a explorer window then the prompt opens at the location where the script is present. I would like to change this behavior and make it open from C:\ if the current window is not a explorer window. I've tried to edit the script but its not working as desired.

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

回答1:


The command to run cmd.exe in the c:\ path is

run, cmd.exe, c:\

A full script that would run the cmd window every time would look like this

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



回答2:


I realize this is an old question, but I was looking into this myself and have a better solution.

Windows has two in-built ways to start cmd at the path of a current explorer window. Shift+RightClick and then click Open Command Window Here (or press w). You can also press alt+d, type cmd, and press enter. So...

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

No magically grabbing the address directly from explorer! :)




回答3:


Here's a pretty sophisticated script from the 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

I tweaked the WIN_7 part a little, so that the code is independent of the unreliable Edit1 control, which doesn't always expose the current explorer location or an incorrect one. If ( InStr( Path , ":" ) ) makes sure that there's no custom path like Computer on Windows 7 or My Computer on Windows XP. I also added an alternative condition that additionally checks for the path to exist, if you want to hedge your bets.




回答4:


Couldn't get other answers to work (it has been a few years since they've been written).

I ended up writing this script:

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



回答5:


Keep it simple. Unless of course you need complexity.

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

!f1 means Alt+F1. For my personal preference. Change it to whatever you like.




回答6:


Another solution hacked together from here. Works for me on Windows 10, but I admit it's total copy-pasta. Posting in the hopes of saving someone else's eyes from the horror of AHK scripting.

;; 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


来源:https://stackoverflow.com/questions/18140240/autohotkey-script-to-open-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!