phil294 / AHK_X11

AutoHotkey for Linux (X11-based systems)
GNU General Public License v2.0
770 stars 11 forks source link

SetWorkingDir has no effect on #Include #77

Open jmbeach opened 3 months ago

jmbeach commented 3 months ago

Hello, thanks so much for your work on this project! I've just started using it and it's already invaluable to me.

I've noticed that even though I run SetWorkingDir %A_ScriptDir% at the top of my script, #Include commands are being ran from the location that I started the script and not the location that the script is in.

Here is the file in question. If I add MsgBox's to see the values of %A_ScriptDir% and %A_WorkingDir% before the #Include's:

MsgBox, %A_ScriptDir%
MsgBox, %A_WorkingDir%
#Include src/layout-manager.ahk

The MsgBox's never pop up and the script fails saying Error opening file with mode 'r': '/home/jared/src/layout-manager.ahk': No such file or directory.

If I comment out the #Include lines to see the MsgBox's, the values actually look correct. They both popup with /home/jared/code/github/jmbeach/autohotkey-x11-scripts even when I run from /home/jared.

phil294 commented 3 months ago

Hi! Thanks for the kind words. #Include is a directive and as such isn't part of the normal execution flow. You can't use conditions https://phil294.github.io/AHK_X11/#h_Include.htm and in the mainstream windows ahk 1.1. docs (which we don't aim to implement, but it's 100% backwards compatible) it even says here https://www.autohotkey.com/docs/v1/lib/_Include.htm

SetWorkingDir has no effect on #Include

which is 1:1 the title of this very issue :-)

So it seems to me everything is (not) working exactly as intended.

phil294 commented 3 months ago

I guess you could use %A_ScriptDir% as part of the #Include statement, but I don't think it will work. That's something that should be fixed, if this is the case, as the docs claim it works

jmbeach commented 3 months ago

Ah, haha. I didn't catch that in the docs about #Include. That's awesome. In that case, yes, what I'm trying to solve is being able to use %A_ScriptDir% in the #Include statement. That would solve my problem.

jmbeach commented 3 months ago

If I try to use %A_ScriptDir in my #Include statement like this:

#Include %A_ScriptDir%/src/layout-manager.ahk

I get the error:

Error opening file with mode 'r':'/home/jared/code/github/jmbeach/autohotkey-x11-scripts/%A_ScriptDir%/src/layout-manager.ahk':No such file or directory.

Line content was: '#Include %A_ScriptDir%/src/layout-manager.ahk'

The program will exit.
phil294 commented 3 months ago

Yeah pretty much as expected. Unsure when I'll fix that, so for now it seems you will have to specify full paths. Or you just cd into the actual directory before running. Or perhaps, you wrap the script into another script (bash, python, even another ahk_x11 script should do it) which sets the working dir to the script dir and then runs the actual script, whose includes will then work as expected.

jmbeach commented 3 months ago

Ah yeah, good idea. I'll just do that for now. Thanks!