#include as a directive in AppleScript

Possible duplicate:
Import AppleScript methods into another AppleScript?

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

For example:

 INCLUDE_DIRECTIVE "Path/To/Applescript.scpt" //Some AppleScript code here 
+6
include applescript
source share
3 answers

You can absolutely do this, and there are two options. 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 to load the script and call special methods in this 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 
+10
source share

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 the routine in this script do this ...

 set myValue to someMethod() of scriptLibrary 
+2
source share

To add to other posters, load script is the only built-in option; this is very primitive, but may be enough if your needs are modest.

Late Night Script software The debug editor provides a # include-style library engine that can combine multiple AppleScript files when compiling a script. The disadvantage of Script Debugger is that it buys a couple of hundred dollars, although many regular AppleScript users will tell you that it's worth the investment.

There are several third-party module loaders, Loader and ModuleLoader , which implement more sophisticated import mechanisms on top of the main load script command, and it's worth a look if your requirements are more complex. I did not use ModuleLoader, but the Loader (which I wrote) can import modules at compile time or run time from different standard and user-defined places and automatically resolve complex (even circular) dependencies between modules.

The disadvantages of Loader and ModuleLoader are that they rely on adding scripts to do the hard work, which can be a problem when distributing scripts (in the case of Loader, osax is only needed to compile scripts, not run them), plus you need to add the template code into your Script for actual import.

+1
source share

All Articles