While path to me does a perfectly good job in getting the path to the script at runtime, I would like to get the path to the script at compile time, and with osacompile. The reason behind that is to be able to set the parent property of a script to a script loaded from a different file.
In AppleScript Editor, it is possible to achieve the above goal using the following trick:
property currentFolder : folder of file (front document's path as POSIX file) of application "Finder"
property parent : load script file ((currentFolder as text) & "ParentScript.scpt") ASUnit, for example, uses this technique to set up a Test Loader. But the above of course fails if the script is compiled with osacompile. Is there a similar “dirty trick” in the latter case?
It seems that I have found an answer to my own question
The code below, albeit involved, compiles also with osacompile. The idea is to move the code into an inner script, whose parent property is set to (a reference to) a script loaded by the outer script at runtime. That is, instead of
property currentFolder : folder of file (front document's path as POSIX file) of application "Finder"
property parent : load script file ((currentFolder as text) & "ParentScript.scpt")
-- Code of the script, calling handlers in ParentScript
one can write:
[code]set currentFolder to (the folder of file (path to me) of application “Finder”)
set PS to load script file ((currentFolder as text) & “ParentScript.scpt”)
script InnerScript
global currentFolder – make currentFolder accessible to the code in this script
property parent : a reference to its PS – set this script as a child of ParentScript.scpt
– Code of the script, calling handlers in ParentScript
end script
run InnerScript[/code]
Cleaner/simpler solutions or comments about the above are welcome
PS: this doesn’t really get the path of the script at compile time, but it allows a script to inherit from a different script in the same folder, which is what I wanted.
I have found a way to get the path to the script’s folder at compile time from osacompile. It is actually pretty simple, though unsatisfactory under several respects. Here is my code:
[code]on loadParent()
set myPath to do shell script “pwd”
load script ((myPath as text) & “/Parent.scpt”)
end loadLib
property parent : loadParent()[/code]
The handler loadParent() is executed at compile time and what it does is getting the path to the current directory and loading the Parent script in it. For this to work, you must run osacompile from within the script’s directory, that is,
is ok, but
will fail. Besides, the script must be run the first time with osascript from within the script’s directory.
And, of course, the above does not compile or run in AppleScript Editor (pwd returns “/”). Too bad.
Note that you cannot use path to me in loadParent() since, being executed at compile time, it would return the path to the program used to compile the script rather than the path to the directory from which the script is compiled.
It is a pity that making an AppleScript project modular by distributing scripts in different files within the project folder is so complicated (if possible at all).
Your root problem is that you’re trying to make a loaded script the parent of the script into which it’s loaded, which isn’t the natural order of things in AppleScript, even though it can be done. The normal way is is to load a script at run time and then tell it to do whatever it does that you want.
To make the loaded script the parent, you’re making it a compiled property of the host script, which makes it a permanent feature of the host script, which in turn means the arrangement’s not modular. The file of the loaded script won’t be accessed again until the host script’s recompiled, so any changes in the loaded script will have no effect in the host script until then. Unless you’re expecting users to compile the scripts themselves, there’ll be no point in distributing the loaded-script file at all.
You’re also (the subject of this thread) trying to get the path to an uncompiled script, which doesn’t have a path until it’s saved, so you’re apparently saving it as an .applescript text file first. If you’re going to do that and save the .applescript file in a specific location, you may as well use the time you spend choosing the specific location to choose the script you want to load instead:
property parent : (load script (choose file of type "osas"))
I agree, but that’s the way ASUnit does it: each test script must inherit from ASUnit. As long as I keep ASUnit.scpt in a default folder, everything is fine. For example, now I initialize a test script with
property parent : load script file ((path to scripts folder from user domain as text) & "ASUnit.scpt")
My problem originates from an attempt at keeping ASUnit in the same folder as my test scripts (for version control, local customization and a fulfilling sense of tidiness ). Of course, I do not absolutely need to have it there, but I was interested in whether it would be achievable or not. Using the technique I have explained in a previous reply I am able to run a single test script that loads ASUnit from its own folder. Unfortunately, using a nested script breaks test suites.
ASUnit is exactly an instance of such a use case, where users of the loaded script are developers themselves.
Correct. And I would like to compile (and eventually run) my test scripts in an automated fashion, without user interaction. That’s why my question was about osacompile.