How to open command prompt from inside a folder w/Autohotkey?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dubs
Posts: 23
Joined: 19 May 2016, 19:55

How to open command prompt from inside a folder w/Autohotkey?

09 Mar 2017, 10:17

How do i open the command prompt from inside a folder, and have it point to that same directory? I've exhausted the shift+right-click in folder trick, doesn't work for me. I need this for ffmpeg.

I use a third party file explorer (xplorer2), so i need a method that will work regardless of the file explorer being used.
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: How to open command prompt from inside a folder w/Autohotkey?

09 Mar 2017, 10:42

If it is not Explorer then I think you may be limited to a kludge where you send a key sequence to go up one folder(such as Alt Up Arrow), sleep 250 ms or so, then send ^c to get the directory into the clipboard. Then send Enter to go back where you started. Not smooth. But if you always use the same file explorer it may be neater to use the Spy to find out the ClassNN of the edit control that has the folder name in it. See ControlGet n the help.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: How to open command prompt from inside a folder w/Autohotkey?

09 Mar 2017, 10:55

script from user tmplinshi , start this script , open a folder and use (example) ctrl+alt+a

Code: Select all

;DOSCommandHere   ctrl+alt+a
;-------- https://autohotkey.com/boards/viewtopic.php?t=5796&p=33858#p33858  user tmplinshi   ---
^!a::cmdHere()
cmdHere() {
    If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {
        WinHWND := WinActive()
        For win in ComObjCreate("Shell.Application").Windows
            If (win.HWND = WinHWND) {
                dir := SubStr(win.LocationURL, 9) ; remove "file:///"
                dir := RegExReplace(dir, "%20", " ")
                Break
            }
    }
    Run, cmd, % dir ? dir : A_Desktop
}
return

example with ffmpeg ( used shortpath ) create a helpfile

Code: Select all

setworkingdir,%a_scriptdir%
pr=C:\M_SmallPrograms\FFMPEG\ffmpeg.exe
f1=%a_desktop%\%a_now%_FFMPEG_HELP.txt
ifnotexist,%pr%
   {
   msgbox,Download first ffmpeg.exe
   run,https://ffmpeg.zeranoe.com/builds/
   exitapp
   }
Loop,%pr%                               ;- create shortpath from path above
   SP2= %A_loopFileShortPath%
runwait,%comspec% /c %sp2% -h>"%f1%",,hide
run,%f1%
return
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: How to open command prompt from inside a folder w/Autohotkey?

09 Mar 2017, 11:19

Skan's code to get path from URL should work in most cases.

Code: Select all

; function by SKAN on AHK forums
;
PathCreateFromURL( URL )
{
  if (! URL)
  return ""
 VarSetCapacity( fPath, Sz := 2084, 0 )
 DllCall( "shlwapi\PathCreateFromUrl" ( A_IsUnicode ? "W" : "A" )
     , Str,URL, Str,fPath, UIntP,Sz, UInt,0 )
 return fPath
}
Plus he still have the issue it will be used in a third party file manager.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
Dubs
Posts: 23
Joined: 19 May 2016, 19:55

Re: How to open command prompt from inside a folder w/Autohotkey?

09 Mar 2017, 11:48

I can stick with the third party explorer if that will make it easier
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: How to open command prompt from inside a folder w/Autohotkey?

09 Mar 2017, 12:45

Dubs wrote:I can stick with the third party explorer if that will make it easier
It should be pretty simple. There should be a class name you can see with the Spy for the pane you are in, and most often they name the class of the edit line that holds the folder path "Edit1". In all cases I have seen it is a single line edit control. Just use ControlGet with the Line command to get the path to pass to ffmpeg.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
Dubs
Posts: 23
Joined: 19 May 2016, 19:55

Re: How to open command prompt from inside a folder w/Autohotkey?

10 Mar 2017, 03:41

Forgive me I'm not fully understanding of Autohotkey, I have managed to grasp the basics, and the basics to using Radial Menu with it, but I'm still quite a noob when it comes to creating new scripts and stuff. My end goal is to open the cmd via Radial Menu while inside the file explorer, and have it point to the current folder, so that I can do conversions with ffmpeg. Apologies for not making this clear earlier.

I remember setting up a key to check for the technical name of the current window, but I'm not sure if this is what you are looking for:
Image
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to open command prompt from inside a folder w/Autohotkey?

10 Mar 2017, 04:47

I use a third party file explorer (xplorer2), so i need a method that will work regardless of the file explorer being used.
Impossible.

There are a few ways to do this (and in general, to automate things):
  1. Take whatever the user does (you do), and emulate it - Send, Click, whatever.
  2. Use an interface designed for use by other applications; e.g. create a COM object and call some methods.
  3. Scrape what information you need from the program, then use that to perform the action in AutoHotkey.
#1 requires that the user interface is sufficiently similar between file explorers; that is, that sending the same keystrokes and mouse clicks to any file explorer will get the same results. But obviously, each one will be different.

#2 requires that each file explorer has such an interface. Windows Explorer has one (accessed via the Shell.Application object). Other file explorers probably don't even have one, let alone one the same as Explorer.

#3 requires that you can get the information from the program. For example, if it shows the full path of the directory in the title bar, you can easily get it with WinGetTitle and open a command prompt with Run. You could probably make this work with many file explorers (or even text editors), but it still requires that they show the path in the window title. Some file explorers don't by default, or just don't. You'd also need a generic way to extract just the path, without the other parts of the title (like the program's name).

If none of that gives you a usable idea, I'd suggest reading the manual. ;)

I would think that any alternative file explorer that can't be customised to open a command prompt when you press a hotkey (without AutoHotkey) isn't worth its salt. Apparently you can customise the keyboard shortcuts in xplorer2; maybe you can bind custom commands to keyboard shortcuts?

I've never used xplorer2, but assuming I've found the right program, the manual indicates that you can run commands in the address bar. That being the case, another method could be to focus the address bar and just run the command $ cmd or $ start cmd (the $ prefix is explained in the manual).

Of course, if the path of the folder is in the window title, WinGetTitle + Run should be much more reliable.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Kawlinz, mebelantikjaya, NinjoOnline and 299 guests