4. Getting Help
When developing appscript-based scripts, there are several ways to get information about applications' scripting interfaces: the ASDictionary application, appscript's powerful built-in help
method, and introspection.
ASDictionary
ASDictionary, available from the appscript website's Tools page, provides a convenient GUI interface for exporting application terminology resources in plain text and HTML formats. ASDictionary can export HTML dictionaries in both single-file format and a frame-based format similar to rdoc's.
Built-in help
method
Appscript's Application
and Reference
classes include a powerful interactive help system that allows you to explore applications' scripting interfaces from within running scripts and Ruby's interactive irb
interpreter. The help
method can provide information on any suite, class, command or reference, and display the inheritance and relationships for a selected class or the entire application.
help
will simply result in a "No help available" message and the script will continue to run as normal.Built-in help is invoked by calling an app
object's help
method, optionally passing it a string indicating the type of information you want. The resulting help message is printed to stderr
and script execution continues as normal. For example, to view the help system's own help, call the help
method with '-h'
as its argument:
app('TextEdit').help('-h')
To print a description of any class or command or view inheritance and relationships, pass the help
method a help string containing one or more of the following options:
- -h
- show help
- -o
- overview of all suites, classes and commands
- -k
- list all built-in keywords (type names)
- -u [suite-name]
- summary of named suite or all suites
- -t [class-or-command-name]
- terminology for named class/command or current reference
- -i [class-name]
- inheritance tree for named class or all classes
- -r [class-name]
- one-to-one and one-to-many relationships for named class or current reference
- -s [property-or-element-name]
- values of properties and elements of object(s) currently referenced
Arguments (shown in brackets) are optional. Additional information on usage is available via help('-h')
.
Examples:
app('Finder').help('-o')
app('Finder').help('-t window')
app('Finder').files.help # same as help('-t')
app('Finder').help('-d item -r folder -i container')
app('Finder').files.help('-s')
To print detailed information on a specific reference, call its help
method without arguments. Examples:
app('TextEdit').help
app('TextEdit').version.help
app('TextEdit').documents.help
To print detailed information on a specific command, call the help
method on an application or reference object with the -t
option followed by the command's name. Examples:
app('TextEdit').help('-t open')
app('TextEdit').documents.help('-t count -t close')
Tip: help
calls can safely be inserted at any point in a reference without disrupting your script's execution:
c = app('TextEdit').help.help('-o -i -s').documents.help[1].help.count
Introspection
In addition to supporting Ruby's standard #methods
and #respond_to?
methods, appscript's Application
and Reference
classes also define several instance methods for obtaining information on the target application:
properties
- Returns a list of all property names.
elements
- Returns a list of all element names.
commands
- Returns a list of all command names.
keywords
- Returns a list of all class, enumerator, type and property names.
parameters(commandName)
- Takes a command's name as string and returns the names of its parameters.
As with Ruby's Object#methods
, all of these methods return a list of strings.
Examples
te = app('TextEdit')
p te.commands
# ["activate", "close", "count", "delete", "duplicate", "exists", ...]
p te.parameters('make')
# ["at", "new", "with_data", "with_properties"]
ASTranslate
ASTranslate, available from the appscript website's tools page, provides a simple tool for translating application commands from AppleScript to Ruby syntax - useful when help is needed in converting existing AppleScript code to Ruby. For example, the following AppleScript selects every file in the Home folder:
tell application "Finder"
select (every file of home whose name extension is in {"htm", "html"})
end tell
To obtain the appscript equivalent, paste this script into ASTranslate and select Execute
from the Document
menu. ASTranslate will intercept any Apple events sent by AppleScript and display them appscript format:
app("Finder").home.files[its.name_extension.is_in(["htm", "html"])].select
OK
See ASTranslate's documentation for more information.
Notes
While appscript's documentation systems try to be reasonably forgiving of flawed or faulty terminology resources, there may be a few particularly problematic applications on which they fail. Should this happen, use ASDictionary to export the application's terminology in plain text format instead.
Other sources of help
- The
sample
folder contains example scripts demonstrating a range of common tasks. - Refer to any scripting documentation and example scripts supplied by the application developer.
- Look for third-party scripts that provide examples of use (though many of these scripts will be written in AppleScript rather than Ruby).
- The appscript website has links to mailing lists and other general resources.