« I'm going to make this simple | Main | Stupid AppleScript Tricks »

Fun with Apple Remote Desktop 3 Scripting

Since I've posted, and recently updated my Apple Remote Desktop Script Collections, I thought it might be fun to walk through some of them. None are all that big, but they do make some common tasks easier. Obviously, these require Apple Remote Desktop 3 or later, since prior to 3, Apple Remote Desktop wasn't scriptable. I'm not going to do them all in one article either.

One of the things that annoys me is how, even though Apple Remote Desktop can install Apple Install Packages to hundreds of remote machines, you have to select the machines manually, and drag and drop the installer, set the options, etc.

That's monkey work, and smart administrators automate that. Instead of using Automator for this, you just use vanilla AppleScript and folder actions. (If you want to be really geeky, you could use launchd to handle the folder action part, but there's not a lot of advantage to this since you have to be running in a console session to use Apple Remote Desktop anyway.)

The simplest one is an action I use to update my Virex installs. First, I created a computer list called Virex Updates and populated it with the systems that are running Virex. Then I created the script and attached it to a folder on my desktop. Now, I just drop the Virex update package in the folder and Apple Remote Desktop handles it from there. I don't have to do anything else. Much simpler. If I really wanted to be slick, I could tie this into a launchd item that only ran when I was logged in that looked for new updates automatically, and did this all for me. This particular script doesn't use a task server, but you could easily use one with it. The code with line numbers follows:

  1. on adding folder items to theFolder after receiving theAddedItems --this starts the folder action
  2.      repeat with x in theAddedItems --theAddedItems is the list of items you dropped on the folder
  3.           set theFileInfo to info for x --get info for the downloading file(s)
  4.           set theBaseSize to size of theFileInfo --get initial size
  5.           delay 3 --wait 3 seconds
  6.           set theFileInfo to info for x --get info again
  7.           set theCompareSize to size of theFileInfo --get a newer size
  8.           repeat while theCompareSize ≠ theBaseSize --if they don't equal, loop until they do
  9.                set theBaseSize to theCompareSize --new base size
  10.                delay 3 --wait three seconds
  11.                set theFileInfo to info for x --get info
  12.                set theCompareSize to size of theFileInfo --get a newer size
  13.           end repeat --once the sizes match, the download is done
  14.      end repeat
  15.      tell application "Remote Desktop" --The actual Apple Remote Desktop part
  16.         set thePackageInstall to make new install package task with properties {delegating to task server:false, encrypting:true, packages:theAddedItems, stopping on error:false} --build the install task
  17.           set theTaskResult to execute thePackageInstall on computer list "Virex Updates" --execute the install task
  18.           display dialog (status of theTaskResult as text) giving up after 60 --OPTIONAL. A little indicator of what happened.
  19.      end tell
  20. end adding folder items to --end of script

Now, going through line by line:

Lines one and twenty are the folder action handler. In this case, "adding folder items to", since that's the action that kicks everything off.

Lines 2 through 14 are there to deal with the fact that there's not a great way of monitoring the status of a file in all situations, and the fact that the "adding items to" action will happily kick off, even though the item's not finished being moved or copied. The Finder Busy Flag doesn't always kick in for all transfer types, so I just use a three second delay and compare loop. If the size hasn't changed in three seconds, it must be done. If you need a longer delay, adjust as necessary.

One point of reference; no matter how many items you drop on a folder with this kind of action attached to it, they're always referenced as a list. This can be inconvenient sometimes, but as we'll see, for scripting Apple Remote Desktop installer actions, it's the perfect way to do this.

Now, lines 15 through 19 are the Apple Remote Desktop tell block.

Line 16 is where we build the install task. With Apple Remote Desktop, pretty much everything you do via scripting is a task, so the standard algorithm is build the task, execute the task. The task we're building is an install package task, so we set thePackageInstall to make new install package task. The specific properties are as follows:


Line 17 exectues the install package task we just built, and specifies which computer list we want to install it to, in this case "Virex Updates". You can specify any valid computer list here. You can also specify specific computers, or selected computers if you like.

Line 18 is totally optional, and one I don't really use, since it makes this be a background thing only, and that is a dialog with the final result of the task. But it's useful for testing.

That's the whole script. There's a lot more you can do with it, error checking, etc., but it's a good little shell. Note that if you have multiple packages in theAddedItems, they all go out as one task installing multiple packages, not multiple tasks each installing a separate package. You can change that by iterating through theAddedItems, and building each task separately if you prefer.

I'm going to be doing more of these little "Fun with Apple Remote Desktop 3 Scripting" articles in the coming days, so if you have any specific requests, put them in the comments here, and I'll see if I can help you out.

Technorati Tags: , ,

Categories:     Applescript, Mac Matters, Mac OS X Scripts, Productivity, Technology
Posted by John C. Welch at 15:59 | Permalink



Comments

Warning for Notes users: The commenting system uses HTML.
I know this will be scary for some of you, especially Notes fans. However, open standards, rah-rah.
If you want to use less-than or greater-than signs, or other similar charachters that HTML reserves,
you'll simply have to learn to do it the HTML way. Luckily, HTML is kind of popular, no matter what
your re-educators have told you, and you can easily find help on the intertubes.
digital.forest Where Internet solutions grow

There, a PayPal Button.

 
Use this code for your Macworld tickets!
Family
The Artwork of Melissa Findley
Diane Francis @ the National Post Eric Francis @ the Calgary Sun

Apple Amazon Links
Apple Mac OS X Server 10.5 [Unlimited]

Apple Mac OS X Server 10.5 [10-Client]

Apple Mac OS X 10.5 Leopard

Apple Mac OS X 10.5 Leopard [5-User Family Pack]

Amazon Book Links
Legacy of Ashes: The History of the CIA

The Donnas: Bitchin'

Wizards at War (The Young Wizards, Book 8)

The Demon's Sermon on the Martial Arts

The Collected Stories of Arthur C. Clarke

JavaScript and Ajax for the Web, Sixth Edition

Awakening Warrior: Revolution in the Ethics of Warfare

FOB Links

Mac Web Writers

Techie Links

Review Victims