Guides and Sample Code

Developer

Mac Automation Scripting Guide

On This Page

Using Script Libraries

A script library is a collection of handlers, which can be loaded and used by other scripts. For example, a scripter might compile a set of commonly-used text-processing handlers into a text library. This library could then be shared by multiple scripts that need to perform text processing operations.

Writing Script Libraries

To write a script library, create a Script Editor document that contains one or more handlers, such as the one shown in Listing 14-1 and Listing 14-2, and save it in script format, as shown in Figure 14-1.

Figure 14-1Saving a script library image: ../Art/scripteditor_savescriptlibrary_2x.png

APPLESCRIPT

Open in Script Editor

Listing 14-1AppleScript: Handler that converts text to uppercase or lowercase
  1. on changeCaseOfText(theText, theCaseToSwitchTo)
  2. if theCaseToSwitchTo contains "lower" then
  3. set theComparisonCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4. set theSourceCharacters to "abcdefghijklmnopqrstuvwxyz"
  5. else if theCaseToSwitchTo contains "upper" then
  6. set theComparisonCharacters to "abcdefghijklmnopqrstuvwxyz"
  7. set theSourceCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  8. else
  9. return theText
  10. end if
  11. set theAlteredText to ""
  12. repeat with aCharacter in theText
  13. set theOffset to offset of aCharacter in theComparisonCharacters
  14. if theOffset is not 0 then
  15. set theAlteredText to (theAlteredText & character theOffset of theSourceCharacters) as string
  16. else
  17. set theAlteredText to (theAlteredText & aCharacter) as string
  18. end if
  19. end repeat
  20. return theAlteredText
  21. end changeCaseOfText

JAVASCRIPT

Open in Script Editor

Listing 14-2JavaScript: Function that converts text to uppercase or lowercase
  1. function changeCaseOfText(text, caseToSwitchTo) {
  2. var alteredText = text
  3. if (caseToSwitchTo === "lower") {
  4. alteredText = alteredText.toLowerCase()
  5. }
  6. else if (caseToSwitchTo === "upper") {
  7. alteredText = alteredText.toUpperCase()
  8. }
  9. return alteredText
  10. }

Move the saved script to one of the following folders on your Mac, creating the folder if it doesn’t already exist:

  • ~/Library/Script Libraries/

  • /Library/Script Libraries/

  • /Resources/ folder inside a script or app bundle.

For additional information about writing script libraries, see Creating a Library in AppleScript Language Guide and Libraries in JavaScript for Automation Release Notes.

Using Script Libraries

Once a script library is installed, your other scripts can target its handlers at any time.

To target a script library in AppleScript, use a tell statement, as shown in Listing 14-3.

APPLESCRIPT

Open in Script Editor

Listing 14-3AppleScript: Targeting a handler in a script library
  1. tell script "My Text Processor"
  2. changeCaseOfText("scripting is awesome!", "upper")
  3. end tell
  4. --> Result: "SCRIPTING IS AWESOME!"

To target a script library in JavaScript, use the Library command to reference the library. Then, you can target handlers in the referenced library, as shown in Listing 14-4.

JAVASCRIPT

Open in Script Editor

Listing 14-4JavaScript: Targeting a function in a script library
  1. textProcessor = Library("My Text Processor")
  2. textProcessor.changeCaseOfText("scripting is awesome!", "upper")
  3. // Result: "SCRIPTING IS AWESOME!"