Configure a new account with AppleScript (well - partly)

Thanks to (mostly) Luther Fuller I can now configure many things in a new user account with AppleScript (not to mention the creation of a template user for a restorable image).

See http://lists.apple.com/archives/applescript-users/2010/Mar/msg00497.html
and http://lists.apple.com/archives/applescript-users/2009/Oct/msg00124.html

Here’s an example script to configure the menu bar:

(*
	Configure the menubar in a new account
	There are 4 plists for this:
		
		~/Preferences/com.apple.systemuiserver.plist
		lists the nonstandard extras that WILL be loaded
		exists in a new account
		
		~/Preferences/ByHost/com.apple.systemuiserver.<machineID>.plist
		lists the standard extras that will NOT be loaded
		does not exist in a new account
				
		~/Preferences/com.apple.menuextra.clock.plist
		determines the clock display
		(whether it is shown or not is in systemuiserver.plist)
		does not exist in a new account
		
		~/Preferences/ByHost/com.apple.HIToolbox.<machineID>.plist
		contains settings for keyboard palette
		(whether it is shown or not is in systemuiserver.plist)
		does not exist in a new account
	
	System Events cannot create functional plist files
	use 'defaults write' to create them with dummy content
	then overwrite with System Events
	http://lists.apple.com/archives/applescript-users/2010/Mar/msg00498.html
	using M.O. from a post by Luther Fuller in this thread
*)
-- get path to prefs folder
set plistFolder to (path to preferences from user domain) as text
--
-- menu extra's: 2 plists
-- standard plist
set plistName to "com.apple.systemuiserver"
set plist_record to {__NSEnableTSMDocumentWindowLevel:true, menuExtras:{"/System/Library/CoreServices/Menu Extras/Clock.menu", "/System/Library/CoreServices/Menu Extras/TextInput.menu"}}
writePlist(plistFolder, plistName, plist_record)
--
-- ByHost plist
-- we need to add the machine id to the plist name
--	http://www.afp548.com/article.php?story=leopard_byhost_changes
-- first get hardware UUID
set UUID to do shell script "ioreg -rd1 -c IOPlatformExpertDevice | grep -i 'UUID' | cut -c27-62 | awk {'print tolower()'}"
-- determine machineID
if text 1 thru 24 of UUID is "00000000-0000-1000-8000-" then -- PPC or early Intel
	set machineID to text 25 thru end of UUID
else -- newer Intel
	set machineID to UUID
end if
-- adjusted plist name
set plistName to plistName & "." & machineID
set plist_record to {dontAutoLoad:{"/System/Library/CoreServices/Menu Extras/TimeMachine.menu", "/System/Library/CoreServices/Menu Extras/Bluetooth.menu", "/System/Library/CoreServices/Menu Extras/AirPort.menu", "/System/Library/CoreServices/Menu Extras/Volume.menu"}}
writePlist(plistFolder & "ByHost:", plistName, plist_record) -- folder path adjusted too
--
-- menu bar clock
set plistName to "com.apple.menuextra.clock"
set plist_record to {DateFormat:"E d MMM  H:mm", FlashDateSeparators:true, IsAnalog:false}
writePlist(plistFolder, plistName, plist_record)
--
-- display of keyboard palette
set plistName to "com.apple.HIToolbox" & "." & machineID
set plist_record to {AppleEnabledInputSources:{{InputSourceKind:"Keyboard Layout", |keyboardlayout name|:"Dutch", |keyboardlayout id|:26}, {InputSourceKind:"Non Keyboard Input Method", |bundle id|:"com.apple.CharacterPaletteIM"}, {InputSourceKind:"Non Keyboard Input Method", |bundle id|:"com.apple.KeyboardViewer"}}}
writePlist(plistFolder & "ByHost:", plistName, plist_record) -- folder path adjusted too
--
-- refresh menu bar
do shell script "killall SystemUIServer"
-----------------------------------------------------------------------------
-- Write to a plist
-- first check if it exists
-- create it with dummy content if not
-- then overwrite
-- parameters:	<text>		path to folder to search
--				<text>		name to look for, without extension
--				<record>	entire content to write
-- returns:					nothing
-----------------------------------------------------------------------------
on writePlist(plistFolder, plistName, plist_record)
	try -- does it exist?
		set plist to (plistFolder & plistName & ".plist") as alias
	on error -- no, create it with dummy content
		set posixPath to quoted form of (POSIX path of plistFolder)
		do shell script "defaults write " & posixPath & plistName & " x y"
		set plist to (plistFolder & plistName & ".plist") as alias
	end try
	-- fill with desired content
	tell application "System Events" to set value of property list file (plist as text) to plist_record -- save the data
end writePlist

Read any plist, to steal its content:

tell application "System Events" to set myPrefsFolder to path to preferences folder of user domain

set prefsFile to choose file default location myPrefsFolder

tell application "System Events"
	set pfPath to prefsFile as text
	set pfRec to value of property list file pfPath
end tell

Simple, hmmm?
But useful to the small shop/spare time/home sysadmin, I think.

New version has these changes:

  • adjust clock display
  • add keyboard & character viewer menulet
  • writing of plist moved into subroutine