I am just starting out with applescript and applescript studio. I am having problems creating a common script file and using it in my other applescripts, which handle buttons and other interface elements. If this was in a FAQ somewhere I am sorry I missed it (I looked!)
I basically want to be able to say the same things as one would in C / C++ of #include “commonLibrary.scpt”
or in say Java or Python by using the import command.
I gather in applescript this is meant to be something like
set myCommonLibrary to (load script alias path...)
Yet I am having problems with this inside my Applescript studio environment. I of course don’t want to put a hard path in there since then the built product won’t be able to be moved. To make this more concrete. Here is the file common.scpt which is inside my applescript studio project.
on doPython(localScriptName, argumentsString)
set scriptPathName to quoted form of ((POSIX path of (path to me)) & "Contents/Resources/" & localScriptName)
do shell script "/usr/bin/python " & scriptPathName & " " & argumentsString
end doPython
(This script works if I have just include it directly in each of my files that need it.) In another file thePathField.applescript I want to add something like the following to the beginning of thePathField.applescript :
set myCommonLibrary to (load script POSIX file (path for main bundle script "common" extension "scpt"))
(I lifted this partly from some stuff I saw on the apple-studio list on apple after searching. However the Doyle applescript studio app which I lifted this from seemed to have to do a check that the library was loaded every time it wanted to use it. This seemed kind of crazy but perhaps necessary…?)
Then later in thePathField.applescript I want to do something like:
on drop theObject drag info dragInfo
...
set theDirectory to tell myCommonLibrary to doPython("getDirectory.py", (quoted form of filePath))
set string value of theObject to theDirectory
...
end drop
Can someone tell me the best way to do this while still keeping a nice organization to the files.
Check the Standard Additions Scripting Commands Suite for commands to read/write scripts from/to a file… When you load the script, assign it to a variable, and you use it essentially like it’s in a namespace named like the variable
set myCommonLibrary to (load script POSIX file (path for main bundle script "common" extension "scpt"))
Which I mentioned in my original post?
My question is what wrapper do I need to put around this. If I just have this at the top of thePathField.applescript, then I recive the message Applescript Error the variable myCommonLibrary is not defined (-2753)
How do I “load” this otherwise? Sorry but I am a new to applescript so would really appreciate a code fragment here…
Sorry I didn’t realize that you had already gotten that far… (read the whole post and then reply… ah yes… sorry, been spending too much time reading Slashdot ^_^)
This is in the context of a normal scripting application but you should be able to see some bits of relevant… (one thought for debugging is to seperate the lines a little more and log… are you sure that path you are passing is resolving properly? note that “POSIX file” doesn’t return a file but a string path… maybe you want “load script file POSIX file …”)
set the_path to (path to "favs" as text) & "data:preferences.scpt"
script my_preferences
property |data| : {}
end script
try
set my_preferences to load script file the_path
get favorite_colour of |data| of my_preferences
on error
set |data| of my_preferences to (|data| of my_preferences) & {favorite_colour:""}
end try
set ok to "OK"
set buttons to {ok}
set prompt to "Favourite Colour?"
set answer to favorite_colour of |data| of my_preferences
display dialog prompt default answer answer buttons |buttons| default button ok
set the favorite_colour of |data| of my_preferences to the text returned of the result
store script my_preferences in file the_path replacing yes
set fav to favorite_colour of |data| of my_preferences
display dialog "Your fave is " & fav
I am confidant the path is getting resolved o.k. For instance if I change thePathField.applescript from
set myCommonLibrary to (load script POSIX file (path for main bundle script "common" extension "scpt"))
on drop theObject drag info dragInfo
...
set theDirectory to tell myCommonLibrary to doPython("getDirectory.py", (quoted form of filePath))
set string value of theObject to theDirectory
...
end drop
to
on drop theObject drag info dragInfo
set myCommonLibrary to (load script POSIX file (path for main bundle script "common" extension "scpt"))
...
set theDirectory to tell myCommonLibrary to doPython("getDirectory.py", (quoted form of filePath))
set string value of theObject to theDirectory
...
end drop
Then the script works fine. But doing this would mean I would have to load / set myCommonLibrary every time I wanted to use a function in it. This is very bad style, and surely there must be a better way, which is the whole point of my question.
I am new to this so this following reasoning might not be quite right, but in applescript studio I gathered that there was no implicit run handlers around scripts? so just including a set myCommonLibrary to (load script … ) at the top of thePathField.applescript won’t work since it is not “run” before applescript-studio hits the other handlers in thePathField.applescript. I might need some onAwakeFromNib, or on willOpen or some other handler or make this a property and initialize it somehow. This is the question.
So the summary of the question is how do I nicely import common scripts in applscript-studio so they are accessible to the entire contents of an applescript-studio file.
Actually if you look at the apple documentation here: ADC Home > Reference Library > Documentation > AppleScript > Tools > AppleScript Studio Terminology Reference > Application Suite > Overview of Application Suite > Classes > Bundle
Anyway at the end of that they show an example of how to load a script from a different bundle. From this I adapted one way which will work. This is the following. Add the following to the files where I want to do importing:
property myLibrary : missing value
on will finish launching theObject
set myLibrary to load script POSIX file (path for main bundle script "common" extension "scpt")
end will finish launching
I had tried this before but I had forgotten to set the corresponding handler in interface builder. Thus you must confirm that the handler “will finish launching” is turned on for the script this is added to. (This is under “File’s owner” → Applescript → → “will finish launching” in interface builder.)
Now is this the sanctioned way, or is there a better way? BTW I thought this would likey be some kind of FAQ item for apple-script studio…