How can I open Windows Linux Subsystem in file location? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
caracalgrey
Posts: 2
Joined: 23 Jun 2017, 16:43

How can I open Windows Linux Subsystem in file location?

23 Jun 2017, 17:11

I have the windows linux subsystem installed on my computer and I want to be able to open bash and have it navigate to the folder I am in. I currently have this code.

Code: Select all

#IfWinActive ahk_class CabinetWClass
#b::
ClipSaved := ClipboardAll
Send !d
Sleep 10
Send ^c
Run C:\Windows\System32\bash.exe, Max
Sleep 100
send cd %clipboard% {enter}
Clipboard := ClipSaved
ClipSaved =
return
#IfWinActive
It works sometimes but not reliably. Sometimes it pastes in bash and other times it pastes in the file explorer. I tried a bunch of different things but couldn't get them to work. I'm thinking maybe if I can get the name of the newly opened bash window I could make sure it pastes in that but I couldn't seem to figure out how to get the name. Basically I just want to be able to open bash and then have it run the command "cd filelocation". Does anyone know how I can make this code work or have a better way of doing this? Thanks!
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: How can I open Windows Linux Subsystem in file location?  Topic is solved

24 Jun 2017, 06:26

caracalgrey wrote:Does anyone know how I can make this code work or have a better way of doing this?
Here's something that works without using the clipboard:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input

#If ((expWnd := WinExist("ahk_class CabinetWClass ahk_id " . WinExist("A"))))
#b::
{
	if ((path := getExplorerWindowPath(expWnd)))
		Run C:\Windows\System32\bash.exe, %path%, Max
}
#If
return

getExplorerWindowPath(hwnd)
{
	static IID_IShellFolder, STRRET, path, SIGDN_FILESYSPATH := 0x80058000
	if (!VarSetCapacity(IID_IShellFolder))
		VarSetCapacity(IID_IShellFolder, 16), DllCall("ole32\CLSIDFromString", "WStr", "{000214E6-0000-0000-C000-000000000046}", "Ptr", &IID_IShellFolder)
		,VarSetCapacity(STRRET, 272), VarSetCapacity(path, 262 * (!!A_IsUnicode + 1))

	shellWindows := ComObjCreate("Shell.Application").Windows
	for window in shellWindows {
		if (window.hwnd == hwnd) {
			try {
				isp := ComObjQuery(window, "{6d5140c1-7436-11ce-8034-00aa006009fa}")
				tlb := ComObjQuery(isp, "{4C96BE40-915C-11CF-99D3-00AA004AE837}", "{000214E2-0000-0000-C000-000000000046}")
				if (DllCall(NumGet(NumGet(tlb+0)+15*A_PtrSize), "Ptr", tlb, "Ptr*", isv) < 0)
					throw
				ifv := ComObjQuery(isv, "{cde725b0-ccc9-4519-917e-325d72fab4ce}")
				if (DllCall(NumGet(NumGet(ifv+0)+5*A_PtrSize), "Ptr", ifv, "Ptr", &IID_IShellFolder, "Ptr*", isf) < 0)
					throw
				if (DllCall(NumGet(NumGet(isf+0)+11*A_PtrSize), "Ptr", isf, "Ptr", 0, "UInt", SIGDN_FILESYSPATH, "Ptr", &STRRET) < 0)
					throw
				if (DllCall("shlwapi\StrRetToBuf", "Ptr", &STRRET, "Ptr", 0, "Str", path, "UInt", 260))
					throw
				return path
			} catch {
				return 0
			} finally {
				for _, obj in [isf, ifv, isv, tlb, isp]
					if (obj)
						ObjRelease(obj)
			}
		}
	}
	
	return 0
}
caracalgrey
Posts: 2
Joined: 23 Jun 2017, 16:43

Re: How can I open Windows Linux Subsystem in file location?

26 Jun 2017, 13:47

Thank you so much! That works perfectly for me. :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 170 guests