I would like to identify the path of a secondary script when that script is called from a primary script. The problem I experience is that path to me when called in the second script returns the path to me of the primary script.
Secondary.scpt:
my getPath()
on getPath()
set myPath to POSIX path of (path to me)
end getPath
yields =>…/Desktop/Secondary.scpt
Primary.scpt:
set targetScriptPSX to (getThisParentPSX() & "Secondary.scpt")
set targetScript to load script targetScriptPSX
set TargetScriptName to targetScript's getPath()
on getThisParentPSX()
set myPath to POSIX path of (path to me)
set myName to name of me
set AppleScript's text item delimiters to myName
set myParent to text item 1 of myPath
set AppleScript's text item delimiters to ""
return myParent
end getThisParentPSX
yields =>…/Desktop/Primary.scpt
The primary script’s call of the secondary script returns the path to the primary script, and not to the secondary script.
As I want these two scripts to be portable, and not restricted to absolute paths, how can I get return the value of the path to the secondary script when it is called from the primary script?
Well. It’s an interesting phenomenon from an inheritance and scope angle. But practically, since the primary script has to know the path to the secondary in order to load it, wouldn’t it be easier to go with that?
load script command loads a copy of the script object stored in the file, like it was embedded within the code of the primary script. Therefore me is exactly the same object in both primary and secondary script. Because me is in all child scripts the same object.
If you want to run the secondary script on it’s own, you could use run script command instead of load script. A new AppleScript instance will load the script file as the root script object and path of me is now the path to itself. You could use the run handler as some kind of constructor handler, to set op the right properties before it will return itself.
The secondary script would look like:
property ScriptLocation : missing value
on run argv
-- initialize
set ScriptLocation to my getPath()
return me
end run
the primary script will not use load script but run script command to load the script into your script.
set theScript to run script file "path:to:secondary.scpt"
return theScript's ScriptLocation
DJ Bazzie Wazzie’s solution worked wonderfully and returned the path of the secondary script from the property value of ScriptLocation. Thank you for your insight.
This secondary script successfully returns the property ScriptLocation to the primary script
property ScriptLocation : missing value
on run argv
set ScriptLocation to POSIX path of (path to me)
return me
end run
This secondary script fails.
property ScriptLocation : missing value
on run argv
set ScriptLocation to POSIX path of (path to me)
return ScriptLocation
end run
I do not understand why the secondary script that returns
me from the run function successfully returns the property ScriptLocation to the primary script.
ScriptLocation from the run function fails to return that same property to the primary script.
In your second script you return the script location. You have to return me because like load script you want to assign a copy of the script object to a variable in the primary script. If you insist on keeping an handler like getPath() just return the property like this:
property ScriptLocation : missing value
on run argv
-- initialize
set ScriptLocation to POSIX path of (path to me)
return me
end run
on getPath()
return my ScriptLocation
end getPath
and your primary script will look like:
set theScript to run script file "path:to:secondary.scpt"
return theScript's getPath()
NOTE: Like your initial post the script stores the posix path of me. The variable is no longer an alias and moving the script while this script is running you’ll lose reference. I would recommend to store the alias in property ScriptLocation so when the file is moved after the secondary script is loaded and getPath() is called afterwards it still returns the correct path.
Thank you for the great explanation.
DJ Bazzie Wazzie wrote:
If I understand your comment, then me represents a script object and not solely a path. For that reason, returning me from the secondary script to the primary script returns the script object and its various properties.