2

I would like to be able to reference a model file that is located in the same directory as the controller.

Initially, they are located in the project's root folder, but when they are compiled (using osacompile) they both will be located in Controller.scptd/Contents/Resources/Scripts directory.

Controller.applescript:

property path :  (container of (path to me) as text) <-- Error: Can’t make container of alias "Macintosh HD:Users:craig:Projects:AppleScript:Foobar:Controller.applescript" into type text.
property model : load script file (path & "Model.scpt")

I can't get the syntax working correctly; I've been unable to find a workable solution. Is there a way to get this working?

** edit **

As noted by @dj-bazzie-wazzie and @mklement0, path to me in the context of an application bundle refers to the script bundle (Controller.scptd in my example), not to the script itself (Controller.scptd/Contents/Resources/Scripts/main.scpt).

Assuming that Model.scpt has been bundled with main.scpt in the /Contents/Resources/Scripts directory, this syntax works:

set Model to load script (path to resource "Model.scpt" in directory "Scripts")

Unfortunately, it doesn't work with non-bundled scripts; @Michele-Percich's solution will work, however.

Is there a single syntax that work work in both situations?

1
  • It is implied by @dj bazzie wazzie's answer, but to make it explicit: in a script bundle, path to me refers to the bundle folder, not main.scpt inside the bundle. Commented Jun 22, 2014 at 3:41

3 Answers 3

4

Properties are initialized (set) at compile time and their values are persistent. So even if you have dynamic values in there, the script will only keep the value of the command/class when it the script was compiled for the last time. Here an example of how properties work:

property a : current date
return a

You can keep clicking the run button in AppleScript-Editor but you'll see that the date isn't updated. This is because when the script compiles, the compiler noticed current date and uses its value. It's not a reference to current date that is stored into the property, but the value returned by current date at compile time, which is just a date value.

You just need to load the script every time when the script is launched.

property model : missing value
set model to load script file ((path to me as string) & "Contents:Resources:Scripts:Model.scpt")

EDIT: Update my answer after the question's has beeing updated

If you want to make use of libraries inside and outside library bundles I would take a look at script libraries, if you're running Mavericks. When you're using the use statement to load a script library and the running script is a bundle (saved as application or script bundle) it will first tries to load the library from there. If fails or the script is just an file and not a bundle it will load the script library from the 4 library folders (user, computer, network, system library and in that order).

I have written a tutorial about how to write script libraries

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the explanation of property initialization. The script-loading command could be simplified to: set model to load script file (path to resource "Model.scpt" in directory "Scripts") as string
3

To address the requirement added later that the code should work in both the following scenarios:

  • When running as simple *.scpt file: load the other script from same folder (as the running script itself)
  • When running as *.scptd bundle: load the other script from the bundle (technically, the same folder as the main script inside the bundle).

This solution builds on the helpful answers provided by @Michele Percich and @dj bazzie wazzie; @dj bazzie wazzie's great tutorial on script libraries is well worth a read.

set otherScript to "Model.scpt"
if (path to me as string) ends with ":" then # Running as bundle?
    set otherScript to ¬
        (path to resource otherScript in directory "Scripts") as string
else
    tell application "Finder" to set otherScript to ¬
        (container of (path to me) as string) & otherScript
end if

set model to load script file otherScript

path to me as stringending in : implies that the path is a folder. Since path to me returns the bundle folder when running as a script bundle (AppleScript-based *.app bundles do the same), we can deduce that the running code is part of a bundle.

Comments

3

container is a Finder's element. Also, why are you using properties?

Anyway, this should work:

tell application "Finder"
   set myPath to container of (path to me) as text
end tell
set model to load script file (myPath & "Model.scpt")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.