Soham Dasgupta
Soham Dasgupta

Reputation: 5199

Autohotkey script to open command prompt

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

Upvotes: 9

Views: 19974

Answers (6)

Lorem Ipsum
Lorem Ipsum

Reputation: 4534

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

Upvotes: 1

John DeBord
John DeBord

Reputation: 705

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.

Upvotes: 1

Tom
Tom

Reputation: 1052

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

Upvotes: 2

Mator
Mator

Reputation: 71

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! :)

Upvotes: 7

576i
576i

Reputation: 8362

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

Upvotes: 16

MCL
MCL

Reputation: 4065

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.

Upvotes: 1

Related Questions