applescript - modular or embedded applescript

Does applescript have this capabilities?

i have some line of code that i want to reuse in other script. is there a way to modularize this piece of code so that i can i have script that calls another script?

this way i can just update 1 script and it would be updated in all the other scripts that uses that bit of code?


There are two ways to do this:

Script libraries
Script libraries can be used by storing scripts in the folder ‘~/Libsrary/Script Libraries’ and include them in your script by the use statement. This will make one instance of the script like a true library.

When you store a script file named StringHandlers.scptd for in ‘~/Library/Script Libraries’ you can use it as follow:

use StrLib : script "StringHandlers" 

-- to call its handler
StrLib's makeUpperCase(str)

When the script has it’s own terminology rather that user defined (sdef defined) and you insert it’s terminology in the global namespace, you should consider wrapping the user terms from block around the call to avoid conflicts with other things.

load script
When you want to instantiate your script file multiple times you can use load script. Another feature is that the library can be stored anywhere you want.

-- load an instance of the object and store it in a variable
set StrLib to load script (path to desktop folder as string) & "StringHandles.scpt"

-- call the handler from the instance
StrLib's makeUpperCase(str)

NOTE: You can wrap a big handler around script objects using script libraries. This enables you to let the handler behave like you have loaded the script using load script.

If only :wink:

A simple use script statement is enough if the script uses defined terminology, but if you want to call handlers you still need to supply a target.

Thank you Shane, I haven’t been writing AppleScript in months. It seems that I’ll get rusty very quickly :wink: