Jump to content


Photo

confusing! included library's relative include paths


  • Please log in to reply
4 replies to this topic

#1 thesoupmeat

thesoupmeat
  • Members
  • 188 posts

Posted 19 July 2012 - 01:08 AM

So, i sync my ahk programs across two computers using dropbox. The directories are different based on home vs work compuer.
For example:
C:\users\[color=#0000FF]home[/color]\dropbox\ahk_stuff\libraries\[color=#008000]mylibrary.ahk[/color]
C:\users\[color=#0000FF]home[/color]\dropbox\ahk_stuff\libraries\[color=#008000][color=#4000FF]mylibrary1.ahk[/color][/color]
C:\users\[color=#0000FF]home[/color]\dropbox\ahk_stuff\libraries\[color=#008000][color=#4040FF]mylibrary2.ahk[/color][/color]
C:\users\[color=#0000FF]home[/color]\dropbox\ahk_stuff\apps\[color=#FF00FF]myapp.ahk[/color]

C:\users\[color=#4000FF]work[/color]\dropbox\ahk_stuff\libraries\[color=#008000]mylibrary.ahk[/color]
C:\users\[color=#4000FF]work[/color]\dropbox\ahk_stuff\libraries\[color=#008000][color=#4000FF]mylibrary1.ahk[/color][/color]
C:\users\[color=#4000FF]work[/color]\dropbox\ahk_stuff\libraries\[color=#008000][color=#4040FF]mylibrary2.ahk[/color][/color]
C:\users\[color=#4000FF]work[/color]\dropbox\ahk_stuff\apps\[color=#FF00FF]myapp.ahk[/color]

So, myapp.ahk includes the mylibrary.ahk file relatively using the "..\" things
#Include ..\libraries\[color=#008000]mylibrary.ahk[/color]
Now it gets a little tricky.
mylibrary.ahk #includes %A_ScriptDir%\mylibrary1.ahk and %A_ScriptDir%\mylibrary2.ahk which are in it's folder.
however, when myapp.ahk #included mylibrary.ahk, mylibrary.ahk starts acting like its under myapp.ahk's folder, so "%A_ScriptDir%\mylibrary1.ahk" doesnt exist.
How do i make mylibrary.ahk #include %A_ScriptDir%\mylibrary1.ahk so that %A_ScriptDir% doesnt change to the file that's #including it?

#2 MasterFocus

MasterFocus
  • Moderators
  • 4126 posts

Posted 19 July 2012 - 02:05 AM

Maybe you should use #Include file (without %A_ScriptDir%\) in mylibrary.ahk and then specify a directory in myapp.ahk? (I'm not sure about it, just guessing)

#3 Lexikos

Lexikos
  • Administrators
  • 8839 posts

Posted 19 July 2012 - 08:11 AM

A_ScriptDir contains the directory of the main script, not the directory of the current file. By default, #Include in AutoHotkey v1.x is relative to the startup working directory, which is typically the script directory, but depends on what launched the script. The working directory is the same for all files, but can be changed by using #Include new-working-directory.

mylibrary.ahk could include the other files the same way that the main script includes mylibrary.ahk:
#Include ..\libraries\mylibrary1.ahk
#Include ..\libraries\mylibrary2.ahk
Alternatively, the main script can set the working directory for subsequent #include directives:
#Include ..\libraries
#Include mylibrary.ahk

[color=green]; in mylibrary.ahk:[/color]
#Include mylibrary1.ahk
#Include mylibrary2.ahk
Another alternative is to put your libraries in one of the function library folders and "auto-include" the file by calling a function which has the same name as the file. AutoHotkey_L (any version) supports a Lib folder in the same directory as the script, and also supports #Include <mylibrary> for including mylibrary.ahk from any of the Lib folders. When a file is included using the function library mechanism, the working directory for #include is the Lib directory which contains the file.

AutoHotkey_L v2 (available but incomplete) uses the directory of the current file by default, which makes things much simpler and more intuitive.

#4 MasterFocus

MasterFocus
  • Moderators
  • 4126 posts

Posted 19 July 2012 - 08:22 AM

...the main script can set the working directory for subsequent #include directives

That's what I meant, but you explained it much better. 8-)
Thanks for the clarification about the other alternatives as well. :)

#5 thesoupmeat

thesoupmeat
  • Members
  • 188 posts

Posted 19 July 2012 - 01:51 PM

awesome!