Scriptable System Preferences
Mac OS X Leopard introduces a number of newly scriptable system preference panes, including: Accounts, Appearance, CD & DVD, Desktop, Dock, Expose and Spaces, Network, and Security.
System Preferences Application
It is not necessary to script the System Preferences application to get or set the parameters for scriptable system preferences. Scripting of the preferences is done directly by using the preferences suites included in the System Events application dictionary.
However, occasionally you may wish to launch the System Preferences application and switch to a specific pane or tab for user review or interaction. Here are sample scripts for controlling the System Preference application:
-- QUIT SYSTEM PREFERENCCES APPLICATION
if application "System Preferences" is running then
tell application "System Preferences" to quit
end if
-- OPEN SYSTEM PREFERENCES TO THE SPACES PANE
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.expose"
get the name of every anchor of pane id "com.apple.preference.expose"
--> returns: {"Main", "Spaces"}
reveal anchor "Spaces" of pane id "com.apple.preference.expose"
end tell
Accounts Preferences
The scripting support in the Accounts Preferences is read-only, meaning you cannot use scripting to generate new users. You can use scripting to set the image used as the current user's picture.
tell application "System Events"
-- ACCOUNTS (Read-Only except picture of current user)
get the properties of the current user
--> returns: {class:user, picture path:file "Mac OS X:Library:User Pictures:Animals:Cat.tif", home directory:file "Mac OS X:Users:sal:", name:"sal", full name:"Sal Soghoian"}
get the properties of every user
--> returns: {{class:user, picture path:file "Mac OS X:Library:User Pictures:Flowers:Yellow Daisy.tif", home directory:file "Mac OS X:Users:sal:", name:"sal", full name:"Sal Soghoian"}}
set the picture path of current user to alias "Mac OS X:Library:User Pictures:Flowers:Yellow Daisy.tif"
end tell
tell application "System Events"
-- LOGIN ITEMS
get the properties of every login item
-- {{class:login item, path:"/Users/sal/Desktop/View Remote Screen.app", hidden:false, kind:"Application", name:"View Remote Screen"}}
-- Adding a login item for the current user
make new login item at end of login items with properties {path:"/Applications/Dictionary.app", hidden:false}
end tell
Appearance Preferences
You can now script the look and functionality of basic user-interface controls.
tell application "System Events"
tell appearance preferences
get properties
--> returns: {scroll arrow placement:together, font smoothing limit:4, recent applications limit:10, scroll bar action:jump to next page, double click minimizes:true, recent servers limit:10, appearance:blue, recent documents limit:10, highlight color:{46516, 54741, 65535}, class:appearance preferences object, smooth scrolling:false, font smoothing style:automatic}
set properties to {scroll arrow placement:together at top and bottom, font smoothing limit:4, recent applications limit:10, scroll bar action:jump to here, double click minimizes:true, recent servers limit:20, appearance:blue, recent documents limit:20, highlight color:{0, 0, 32000}, smooth scrolling:true, font smoothing style:light}
end tell
end tell
CD & DVD Preferences
Using scripts, you can determine the actions performed when various discs are mounted by the computer.
tell application "System Events"
tell CD and DVD preferences
get the properties of video DVD -- Also: blank CD, blank DVD, music CD, picture CD, video CD
-- > returns: {class:insertion preference, custom script:missing value, insertion action:open application, custom application:file "Mac OS X:System:Library:CoreServices:Front Row.app:"}
-- OPEN APPLICATION
set properties of video DVD to {insertion action:open application, custom application:"/System/Lirabry/CoreServices/Front Row.app:"}
-- RUN A SCRIPT
set properties of picture CD to {insertion action:run a script, custom script:file "Mac OS X:Users:sal:Library:Scripts:Import Photo CD.scpt"}
end tell
end tell
Desktop Preferences
By manipulating the scriptable Desktop Preferences, you can control how images are used as desktop backgrounds.
tell application "System Events"
-- GET DESKTOP PICTURE SETTINGS
tell current desktop
get properties
--> returns: {display name:"Color LCD", random order:false, pictures folder:file "Mac OS X:Library:Desktop Pictures:", picture rotation:0, class:desktop, change interval:60.0, picture:file "Mac OS X:Library:Desktop Pictures:Aqua Blue.jpg"}
end tell
end tell
tell application "System Events"
-- RANDOM ROTATION OF A FOLDER OF IMAGES
tell current desktop
set picture rotation to 1 -- (0=off, 1=interval, 2=login, 3=sleep)
set random order to true
set pictures folder to file "Mac OS X:Library:Desktop Pictures:Plants:"
set change interval to 5.0 -- seconds
end tell
end tell
tell application "System Events"
-- SET DESKTOP TO SPECIFIC PICTURE
tell current desktop
set picture rotation to 0 -- (0=off, 1=interval, 2=login, 3=sleep)
set picture to file "Mac OS X:Library:Desktop Pictures:Plants:Agave.jpg"
end tell
end tell
Although scripting support is not integrated into the Screen Saver preference, you can use scripting to start the current Screen Saver:
-- LAUNCH SCREENSAVER
try
tell application id "com.apple.ScreenSaver.Engine" to launch
end try
Dock Preferences
You can use AppleScript to control the way the Dock appears in Mac OS X:
tell application "System Events"
tell dock preferences
get properties
--> returns: {minimize effect:genie, springing delay:1.0, dock size:0.428571432829, magnification:false, springing:false, location:bottom, class:dock preferences object, magnification size:1.0, animate:true, autohide:false}
set properties to {minimize effect:scale, location:right, autohide:true, magnification:false, magnification size:0.5, dock size:1.0}
end tell
end tell
Expose and Spaces Preferences
Using scripts, you can read and set the values of the Expose and Spaces parameters.
tell application "System Events"
tell expose preferences
-- SCREEN CORNERS (top right screen corner, bottom left screen corner, bottom right screen corner, top right screen corner)
get the properties of the top right screen corner
--> returns: {activity:show desktop, class:screen corner, modifiers:{}}
set properties of the top right screen corner to {activity:show desktop, modifiers:{control, option}}
-- EXPOSE SHORTCUTS
get the properties of the all windows shortcut
-- {class:shortcut, mouse button:4, function key:F9, function key modifiers:{}, mouse button modifiers:{}}
get the properties of the application windows shortcut
-- {class:shortcut, mouse button:0, function key:none, function key modifiers:{}, mouse button modifiers:{}}
get the properties of the show desktop shortcut
-- {class:shortcut, mouse button:0, function key:F11, function key modifiers:{}, mouse button modifiers:{}}
-- DASHBOARD SHORTCUT
get the properties of the dashboard shortcut
-- {class:shortcut, mouse button:0, function key:none, function key modifiers:{}, mouse button modifiers:{}}
-- SETTING A SHORTCUT
set the properties of the application windows shortcut to {mouse button:3, function key:left control, function key modifiers:{none}, mouse button modifiers:{command}}
end tell
end tell
-- LAUNCH EXPOSE
try
tell application id "com.apple.exposelauncher" to launch
end try
You can use scripts to determine how Spaces is implemented.
-- GET AND SET PROPERTIES
tell application "System Events"
tell expose preferences
tell spaces preferences
set spaces enabled to true
get properties
-- {spaces enabled:true, spaces rows:1, arrow key modifiers:«class
-- SET THE PROPERTIES AND APPLICATION ASSIGNMENTS
set properties to {spaces rows:1, spaces columns:3}
set application bindings to {|com.apple.Mail|:1, |com.apple.AddressBook|:1, |com.apple.Safari|:2, |com.apple.Xcode|:3}
-- SET MODIFIERS OF SWITCHING KEYS
properties of arrow key modifiers
-- {class:spaces shortcut, key modifiers:{control}}
set key modifiers of arrow key modifiers to {command}
properties of numbers key modifiers
-- {class:spaces shortcut, key modifiers:{control}}
set key modifiers of numbers key modifiers to {command}
end tell
end tell
end tell
-- LAUNCH SPACES
try
tell application id "com.apple.spaceslauncher" to launch
end try
Network Preferences
Script the Network preferences is focused on getting and changing the current location setting:
tell application "System Events"
tell network preferences
get properties
--> returns: {current location:location id "51BD3FB7-50D1-4859-9649-9138E7FF1ECA" of network preferences, class:network preferences object}
get the name of every location
--> returns: {"Automatic", "Sprint Card"}
set current location to location "Automatic"
end tell
end tell
Security Preferences
Basic security preferences can now be read and set with scripts.
tell application "System Events"
tell security preferences
get properties
--> returns: {require password to wake:false, class:security preferences object, secure virtual memory:false, require password to unlock:false, automatic login:false, log out when inactive:false, log out when inactive interval:60}
set properties to {require password to wake:false, secure virtual memory:false, require password to unlock:false, automatic login:false, log out when inactive:false, log out when inactive interval:60}
end tell
end tell
Launching Spaces, Expose, Dashboard, and the Screen Saver
In Leopard, you can use scripts to launch Spaces, Expose, Dashboard, or the current screen saver.
-- LAUNCH SPACES
try
tell application id "com.apple.spaceslauncher" to launch
end try
-- LAUNCH EXPOSE
try
tell application id "com.apple.exposelauncher" to launch
end try
-- LAUNCH DASHBOARD
try
tell application id "com.apple.dashboardlauncher" to launch
end try
-- LAUNCH SCREENSAVER
try
tell application id "com.apple.ScreenSaver.Engine" to launch
end try