use (application or script library)

A use statement can declare a required application or script library, and may import the declared items’s terms for use later in the script. There are two optional parameters available for a use statement targeting an application or script library: version, and importing

Syntax

use [identifier:] specifier ( script | application )
[version versionText]
[importing boolean (with importing|without importing)]

Placeholders

identifier
An optional identifier (property variable) for the imported resource.

specifier
The item whose terminology is to be imported. This is typically an application or script library name, as in use application "Finder" or use script "My Library", but may be any valid specifier form, such as by ID, as in use application id "com.apple.mail".

versionText
The required minimum version of the resource as version number, such as "2.3.2". This value is always text, not a number, and is compared as if considering numeric strings is in effect. For example, "2.10" is greater than "2.5", because 10 is greater than 5.

boolean
A boolean value, true or false. AppleScript will recompile this to with importing or without importing.

Examples

A use statement may refer to an application:

use application "Finder"

or a script library:

use script "Text Utilities"

If an optional identifier is given, it defines a property whose value is the required resource. This can make it more convenient to refer to the resource, as in the following example where the get statement uses the identifier Safari instead of the full specifier application "Safari".

use Safari: application "Safari"

get the name of Safari's front window

By using use statements with multiple applications, you can combine terms from different sources in ways impossible using standard tell statements or tell blocks, because the tell construct only makes one terminology source available at a time.

For example, the following script, in one statement, uses Mail and Safari to search the web for the sender of the currently selected mail message. The get event is sent to Mail because it defines Mail’s message viewer window, while the search the web event is sent to Safari.

use application "Mail"
use application "Safari"

search the web for the sender of the first item of ¬
(get selected messages of the front message viewer)

Discussion

In addition to declaring a script requirement, by default, the use command imports terminology and makes the terms available throughout the script without requiring the use of tell or using terms from. It does so by tracking where a term was imported from, and sends events that use those terms to that target, as shown in the example above.

Ordinarily, commands are sent to the current target. Imported terminology overrides this policy, if one of these conditions is met:

  1. the event identifier is imported
  2. the direct parameter is an imported class or enumeration identifier
  3. the direct parameter is an object specifier ending with an imported term

Under these conditions, the command is sent to the import source instead. This happens even if the command is inside a tell block for a different application.

You can also “use” objects without importing their terms, so as to declare requirements without making terms available globally. In the following example, AppleScript will ensure that Safari version 7.0 or later is available:

use application "Safari" version "7.0" without importing

NOTE: In the example above, because Safari’s terms are not imported, the script will need to use tell statements to send it events.

TOP | CONTINUE