9

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
Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125

6 Answers6

16

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
576i
  • 7,579
  • 12
  • 55
  • 92
  • What is the difference between `#Ifwinactive` and `Ifwinactive`? – Soham Dasgupta Aug 09 '13 at 07:14
  • I found one caveat though. When I'm in the `My Computer` window the prompt is starting from where the script is running. – Soham Dasgupta Aug 09 '13 at 11:04
  • The difference is that you can define hotkeys within a "#ifwinactive" block that are only active when the winodw is active. This is the purpose of the "#ifwinactive command". – 576i Aug 10 '13 at 11:15
  • for the "My Computer" problem. The "IfWinActive" command also lets you exclude certain windows. Please lookup the autohotkey help file on "IfWinActive / IfWinNotActive" where this is described in detail. – 576i Aug 10 '13 at 11:17
  • I've looked up the `ExcludeTitle` property but I'm not sure how to pass this `My Computer` to the this exclusion list. – Soham Dasgupta Aug 12 '13 at 04:33
  • I edited the script to exclude the windows "My Computer" and "My Documents". I'm testing this on a non-English windows, so if it doesn't work for you please look at the caption of the window and write "My computer" and "my documents" exactly as they appear on your system. Hope this helps. – 576i Aug 13 '13 at 12:56
7

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

Mator
  • 71
  • 1
  • 1
2

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
Tom
  • 964
  • 9
  • 25
1

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.

MCL
  • 3,985
  • 3
  • 27
  • 39
  • @SohamDasgupta Well, that's not very specific at all. You have to press `WIN+C` to trigger the functionality. – MCL Aug 13 '13 at 10:09
  • @SohamDasgupta I only posted the hotkey itself, of course you'll have to call `SetTitleMatchMode, RegEx` as described in the linked thread. I added the auto-execute section, too. – MCL Aug 13 '13 at 10:19
1

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.

John DeBord
  • 638
  • 1
  • 5
  • 17
1

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
Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67