Does anyone here know how to “include” separate scripts into a single script?
for example, i have 4 scripts named main.applescript ,a.applescript, b.applescript, and c.applescript.
i want to call functions located in a.applescript, b.applescript, and c.applescript from main.applescript. how do i do that? in PHP, i simply need to include “filename.php” and it will be included as if it was a part of the main script.
hope i can get the syntax and steps on how to make it. thanks.
AppleScript offers the «load script»-command to include and use external libraries. But afaik it does only accepts compiled scripts (*.scpt) to be loaded. Here is an example:
set mypath to ((path to me) as Unicode text)
set scriptpath to mypath & "Contents:Resources:mylib.scpt"
set mylib to load script (scriptpath as alias)
set x to mylibs's specialfunction()
I guess you are worried about the scope of globals and properties, right? But AppleScript is not all that bad at that
Here is an example:
Let’s assume that we have a script named «import.scpt» on our desktop. This script contains just one line:
property scriptname : "Imported script"
Now we load this script into our main script with this code:
property scriptname : "Parent script"
set scriptpath to ((path to desktop) as Unicode text) & "import.scpt"
set mylib to load script (scriptpath as alias)
display dialog (scriptname & " calling " & mylib's scriptname & "!")
Our main script also features a property named «scriptname», but it does not clash with the property of the loaded script. You can just access and manipulate them.