Get working directory of a window's process?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Klaus2
Posts: 6
Joined: 11 Jan 2016, 04:16

Get working directory of a window's process?

17 Oct 2018, 03:34

We have A_WorkingDir for accessing the working directory of the current script.
But is there also some method for querying the working directory associated with the active window?

Purpose

I have written a script that opens one of several programs in the directory of the file opened in the current window (cmd.exe, explorer, Cygwin bash), or opens the opened file in Emacs.

Technical background

For programs, where this is viable, I extract the path of the file from the window title. Often programs have patterns such as "E:\Dropbox\CurrentProject\manual.lyx - LyX", which can easily be used for this. Some can be configured to allow this, e.g. by setting (setq frame-title-format "%f") in Emacs or setting FullPathInTitle = true in SumatraPDF.
In other programs however, this isn't possible.

I know, that programs can set a current working directory, that is then used as the default root directory for resolving relative file paths and executing subprocesses (in cmd.exe/powershell/bash the cd command,in Python os.chdir()). On the C++ level, it seems to be possible, but hackish. From within AutoHotkey (or Python3 for that matter), I have no idea how to do it.

Changes are though, that the programs internally don't set the working directory to the parent of the opened file; Would still be interesting as a possible fallback, IF they do.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Get working directory of a window's process?

17 Oct 2018, 04:19

- There's this script:
[SOLVED]get other process's working dir - Page 3 - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/8530 ... ntry544650
- You can use regular AutoHotkey v1.1, however, you need a 32-bit/64-bit dll (see the link below), and you need to run 32-bit/64-bit AutoHotkey to match the process.
- I wrote a version of the function that doesn't require including scripts.
how to dll Inject - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 80#p171580

- Here I list various techniques for getting the path of the active file from different programs.
Get path of active window's directory - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=38680
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DiegoSouto, Rohwedder, Sniperman and 397 guests