Apple Remote Desktop - Using AppleScript with Remote Desktop

background image

Using AppleScript with Remote Desktop

AppleScript is a powerful and versatile scripting language that is built into Mac OS X.
You can use AppleScript to create shortcuts, automate repetitive tasks, or even make
custom applications that save you a great amount of time. AppleScript is an English-
like language you can use to write scripts that contain commands. Scripts can make
decisions based on user interaction, or by parsing and analyzing data, documents, or
situations. Remote Desktop is scriptable, as are many other Mac OS X applications, and
it can be controlled with AppleScript commands. AppleScript is a complete language
with conditional statements, comparison and arithmetic operations, and the ability to
store variables.

This documentation doesn’t teach AppleScript language syntax or programming
practices. For information about learning how to program with AppleScript, see the
AppleScript online help.

This section provides a brief description of AppleScript, a brief discussion of using the
Remote Desktop AppleScript Dictionary, and a sample script.

Remote Desktop’s AppleScript Basics

AppleScript scripts consist of commands that are sent to objects. Objects can be a wide
variety of things, including applications, scripts, windows, settings, or the Finder. These
objects can receive a specific set of commands and respond with the desired actions.
Essentially, a script tells an application (Remote Desktop in this case) to either complete
a certain task or retrieve information. You can give the script decision-making
capabilities by using conditional statements; you can give the script a memory by
defining variables.

Remote Desktop has made all of its fundamental functions scriptable. The tasks that
you perform as an administrator by pointing and clicking the mouse can all be
accomplished by running an AppleScript. For example, you can:

 Get information on or rename a computer
 Add computers to a list
 Copy or install items
 Execute a report task

background image

Chapter 9

Automating Tasks

173

Using the Remote Desktop AppleScript Dictionary

Each scriptable application contains an AppleScript dictionary—the list of objects and
messages that an application can understand. For example, in Remote Desktop’s
dictionary there is an object named “computer list” that has this entry:

A “computer list” is an object which contains other objects (“computers” in this case)
and has properties like its “id” and its “name.” When queried, this object can return the
values for the properties (in Unicode text as indicated), but you can’t change “id” from
within the script (it’s labeled r/o for read-only). This object can be acted upon by the
“verbs,” or messages, in a script.

The dictionary also contains “verbs,” or messages. These verbs are commands that act
on the objects in the dictionary. For example, in Remote Desktop’s dictionary there is a
verb named “add,” and this is its entry:

This entry tells you what the verb can act on and how. This entry says that Remote
Desktop can add a specified computer to a computer list. The objects “computer” and
“computer list” are being acted upon by “add.”

To access the full AppleScript dictionary for Remote Desktop:

1

Launch Script Editor in the /Applications/AppleScript/ folder.

2

Select File > Open Dictionary.

3

Choose Remote Desktop.

4

Click OK.

The AppleScript Dictionary for Remote Desktop is also available in Appendix C,
“AppleScript Remote Desktop Suite.”

computer list n [inh. item] : A list which holds computers.
ELEMENTS
contains computers; contained by application.
PROPERTIES
id (Unicode text, r/o) : The unique identifier (UUID) of the computer list.
name (Unicode text) : The name of the computer list.

add v : Add a computer to a task.
add computer : The computer.

to computer list : The computer list (or task) to add the computer to.

background image

174

Chapter 9

Automating Tasks

Sample AppleScript

This AppleScript is one that could be used to do a quick cleanup of a group of
computers. First, it locks the computer screens to prevent interference. Second, it
deletes all items left on the currently active desktops of the client computers. Finally, it
finishes by emptying the clients’ trash and unlocking the screens.

This script is for educational use only and no warranty is explicit or implied as to the
suitability of this script for your computing environment. Additionally, this sample
script deletes items on the target computers. Use at your own risk.

-- Start commanding the local copy of Remote Desktop

tell application "Remote Desktop"

-- decide which list to perform this on, in this case it's called

"Classroom"

set these_computers to computer list "Classroom"

-- decide what locked screen text you want displayed

set screen_message to "Please wait" as Unicode text

-- make a UNIX script which executes an AppleScript on the remote

computers

set the UNIX_script to "osascript -e 'tell application \"Finder\" to

delete every item of the desktop whose class is not disk'"

-- set the lock task parameters

set lock_task to make new lock screen task with properties {name:"Lock

Classroom", message:screen_message}

-- perform the task

execute lock_task on these_computers

-- set the UNIX script parameters

set clean_task to make new send unix command task with properties

{name:"Clean Desktop", showing output:false, script:UNIX_script}

-- perform the task

execute clean_task on these_computers

-- empty the trash afterward

execute (make new empty trash task) on these_computers

-- unlock the screen when finished

execute (make new unlock screen task) on these_computers

end tell

background image

Chapter 9

Automating Tasks

175