29 October 2006

How to check for UI scripting

How embarrassing. This blog gets a link from MacDevCenter and I have a (minor) bug in the script. I have had UI Scripting enabled for son long that I always forget to check for it in the script. Obviously if somebody else runs the script, nothing happens.

To enable UI scripting go to the Universal Access Preference Pane and enable the checkbox at the bottom of the screen. However good script should check for this and give you a dialog, prompting you to enable it. Here is the code:

(Works with Tiger and up.)

tell application "System Events"
if not UI elements enabled then
display dialog "GUI Scripting is not enabled. This script requires GUI scripting, click \"OK\" to enable."
set UI elements enabled to true
end if
end tell


Note to self: paste this in front of every script that uses UI scripting.

No comments: