views:

149

answers:

3

Possible Duplicate:
Import AppleScript methods in another AppleScript?

Is there anything in AppleScript that can be used like the #include directive in C?

For instance:

INCLUDE_DIRECTIVE "Path/To/Applescript.scpt"

//Some AppleScript code here
+1  A: 

Use something like this to load the script

set scriptLibraryPath to (path to scripts folder from user domain as text) & "myScript.scpt"
set scriptLibrary to load script scriptLibraryPath as alias

Then to access a subroutine in that script do this...

set myValue to someMethod() of scriptLibrary
regulus6633
+3  A: 

Absolutely you can do this, and there are two variations. The first loads the entire script:

Script Foo.scpt

set theBar to "path:to:Bar.scpt" as alias
run script (theBar)

Script Bar.scpt

display dialog "Bar"
--Result: A window that displays "Bar"

The second allows you load a script and call specific methods within that script:

Foo.scpt

property OopLib : load script POSIX file "/Users/philipr/Desktop/OopLib.app"
tell OopLib
    set theResult to Oop(1)
    display dialog theResult
end tell
--> result: Window displaying "Eek: 1"

OopLib.scpt

on Oop(Eek)
    display dialog Eek
    return "Eek: " & Eek
end Oop
Philip Regan
A: 

To add to what other posters have said, load script is the only built-in option; it's very primitive, but may be sufficient if your needs are modest.

Late Night Software's Script Debugger editor provides an #include-style library mechanism that can merge multiple AppleScript files when compiling a script. The downside of Script Debugger is that it's a couple hundred bucks to buy, though many regular AppleScript users will tell you it's well worth the investment.

There are a couple of third-party module loaders, Loader and ModuleLoader, that implement more sophisticated import mechanisms on top of the basic load script command, and are worth looking into if your requirements are more complex. I've not used ModuleLoader, but Loader (which I wrote) can import modules at compile- or run-time from various standard and user-specified locations, and will automatically resolve complex (even circular) dependencies between modules.

The downsides of Loader and ModuleLoader is that they rely on scripting additions to do some of the heavy lifting, which might be an issue when distributing scripts (in Loader's case, the osax is only needed to compile scripts, not to run them), plus you need to add some boilerplate code to your script to perform the actual import.

has