Applescript to adjust settings on login, without having to re-log.

Hello everyone!

I’ve been lurking these forums for a while now and am unable to come up with a solution to this dilemma.

I need to write a script that runs as a standard user which does the following upon login (And you cannot log out and back in to apply and of these, it has to occur after login without a reboot):

-Copies all settings from a usb backup. (The USB has a complete copy of the user’s preferences folder)

-Applies them all to the current login.

-Refreshes it all to apply them.

I currently use a script to copy-paste them all over to the user’s preferences folder, however no matter how much I kill Finder and the Dock, most changes either don’t apply or get overridden.

The changes that need to be done are:

Firstly, it needs to gather the name of the USB plugged in, currently it does so like this:


tell application "Finder"
	tell me to set usbname to POSIX file ((POSIX path of (path to me)) & "/..") as text
	tell me to set usbname to text 1 thru ((offset of ":" in usbname) - 1) of usbname
	tell me to set usb to "/Volumes/" & (usbname as text)
end tell

Is there a better way to get the name of the USB plugged in? (The script will eventually be on the machine, not on the USB which will break this).

  1. Add all the text replacements from the usb plist to the system preferences. (I got this working manually with GUI scripting, but it’s extremely prone to breaking)

I have found that there is a .GlobalPreferences.plist that contains these, inside the user preferences folder; But if I edit this, the change is not applied and I cannot work out how to make system preferences to refresh this data or clear the cache. I would much rather do it all in the background then use this script to do it all in the foreground.

Currently it copies them from a numbers spreadsheet:


tell application "Numbers"
	activate
	delay 5
	
	open usb & "/Login Script/settings.numbers"
	delay 3
	
	tell front document
		tell table 1 of sheet 1
			
			-- i is the beginning cell of 20, where our replacements begin.
			set i to 20
			
			--Create lists for the data
			set ShortcutArray to {(value of cell i of column "A" as text)}
			set ReplacementArray to {(value of cell i of column "B" as text)}
			
			--Set the first cell value to the replacement array.
			set CellValueA to value of cell i of column "A" as text
			set CellValueB to value of cell i of column "B" as text
			
			--Loop until we hit an empty cell.
			repeat until (CellValueA = "0.0")
				set CellValueA to value of cell (i + 1) of column "A" as text
				set CellValueB to value of cell (i + 1) of column "B" as text
				
				-- Check if the value of the cell is empty.
				if (CellValueA = "0.0") then
					
					-- If not, do this.
				else
					-- Adds the value of the shortcut to a list.
					copy (CellValueA as text) to the end of ShortcutArray
					-- Adds the value of the replacement to another list.
					copy (CellValueB as text) to the end of ReplacementArray
					
					set i to (i + 1)
				end if
			end repeat
			
			--Gather the total number of replacements to iterate through later.
			set TotalReplacements to (i - 19)
		end tell
	end tell
	
	--close numbers after we gather settings.
	quit
	--tell process "Numbers"
	--click button "OK" of sheet 1 window "settings.numbers"
	--end tell
end tell

And adds them by this: (However, it bugs out sometimes as when it enters them, it fails at the click text field part, it doesn’t have the window in the front sometimes…


tell application "System Events"
	tell process "System Preferences"
		if ((get item 1 of ShortcutArray as text) is equal to "0.0") then
			
		else
			click menu item "Language & Text" of menu "View" of menu bar 1
			
			tell window "Language & Text"
				delay 1
				click radio button "Text" of tab group 1
				delay 2
				
				click text field of row 1 of table 1 of scroll area 1 of tab group 1
				
				set a to 1
				click button 1 of tab group 1
				
				
				
				repeat TotalReplacements times
					
					set shortcut0 to (get item a of ShortcutArray)
					set replacement0 to (get item a of ReplacementArray)
					
					set the clipboard to shortcut0
					keystroke "v" using {command down}
					
					keystroke tab
					
					set the clipboard to replacement0
					keystroke "v" using {command down}
					
					keystroke return
					click button 1 of tab group 1
					
					set a to a + 1
					display dialog shortcut0 & return & replacement0
					
					
				end repeat
			end tell
		end if
	end tell
end tell

  1. I desperately need it to save all the desktops used in Mission Control, and preferably all the windows. If they can’t be saved/restored I’d be happy with simply using AppleScript to re-create the desktops.

  2. Change Date&Time to show seconds & date. (Is there a way to do this with direct plist editing rather than using system preferences?)


tell application "System Events"
	tell process "System Preferences"
		activate
		click menu item "Date & Time" of menu "View" of menu bar 1
		tell window "Date & Time"
			delay 2
			click radio button "Clock" of tab group 1
			click checkbox "Display the time with seconds" of tab group 1
			click checkbox "Show date" of tab group 1
		end tell
		
	end tell
end tell

  1. Apply a background according to a picture in the usb, which I have working, but it needs to not care what image format, currently it NEEDS to be a jpg. but I need it to accept PNG, GIF, JPEG, TIFF, etc.

tell application "Finder"
	set desktop picture to {"Volumes:" & usbname & ":Background.jpg"} as alias
end tell

  1. Move the dock position & icon layout according to the preferences plist.

  2. Make Preview open a bunch of PDF files in order, and resize them and position them to certain sizes (I cannot get this working at all, no matter how I do it it just doesn’t acknowledge commands)

If there is someone out there able to fix any or all of this, I would be eternally grateful and if you fix it and have PayPal, i’ll send some cash your way too!