Add home button to Safari toolbar with Applescript?

I have a question that might sound dumb and simple but I am doing it for a reason. I am ultimately trying to create an applescript that adds the home button into the safari toolbar (I know the manual way of doing it in Safari/View/Customize Toolbar/Drag Home Button… but this is going to be a small part of one big applescript). I’ve been researching for days and just cant figure it out. I think it has something to do with javascript but im not sure. The only possible solution that I can think of is to set up safari the way that we want it (with the home folder in the toolbar, etc.), embed all of safari’s .plist files into the “contents/resources” folder, and then tell the applescript to copy those files back into the Library on the new computer that I’m running the script on. I think this sounds like a good plan but I just am so confused on how to point the Applescript to the resources folder. I thought you guys might be able to help me out. Thanks so much!

You can do it your way. To get the path the Contents/Resources folder of your applescript is easy. “path to me” will get the path to your script, so you could do the following…

set myPath to path to me as text
set myResources to myPath & “Contents:Resources:”

But you can write to the plist file directly. The following will add the home button to Sarafi’s plist file, in this case I make it the 2nd button in the toolbar.

set plistPath to (path to preferences folder from user domain as text) & "com.apple.Safari.plist"
set homeButtonPosition to 2 -- the button position in the toolbar
set homeButtonIdentifier to "HomeToolbarIdentifier"

-- make sure Safari isn't running
tell application "Safari" to quit

-- get the current toolbar items from Safari's plist file
tell application "System Events"
	-- get the names of the properties in the plist file
	set plistFile to property list file plistPath
	set toolbarPLI to property list item "TB Item Identifiers" of property list item "NSToolbar Configuration BrowserWindowToolbarIdentifier" of plistFile
	set toolbarIdentifiers to value of property list items of toolbarPLI
end tell

-- if the toolbar contains the home button we remove it
if toolbarIdentifiers contains homeButtonIdentifier then
	set tempList to {}
	repeat with i from 1 to count of toolbarIdentifiers
		if not (item i of toolbarIdentifiers) is homeButtonIdentifier then
			set end of tempList to item i of toolbarIdentifiers
		end if
	end repeat
else
	set tempList to toolbarIdentifiers
end if

-- insert the home button as the second item
set finalToolbarList to {}
repeat with i from 1 to count of tempList
	if i is homeButtonPosition then set end of finalToolbarList to homeButtonIdentifier
	set end of finalToolbarList to item i of tempList
end repeat

-- insert this finalToolbarList into the plist file
tell application "System Events" to set value of toolbarPLI to finalToolbarList

-- start safari
tell application "Safari" to activate

YES! That last script worked beautifully! Thank you so much. You have helped me a ton.

Actually… I bumped into a snag. I tried creating a new test user and then running the script and it gives me an error message saying "System Events got an error: Can’t get property list item “NSToolbar Configuration BrowserWindowToolbarIdentifier” of property list file “Macintosh:HD:Users:test:Library:Preferences:com.apple.Safari.plist”. I did this because my department will be running this script on brand new machines. I think that it might be that if you have never added anything to the toolbar manually, the “NSToolbar Configuration BrowserWindowToolbarIdentifier” property is hidden. Once you do it manually, take it back out and close safari, it appears in the .plist file and then the script runs fine. Do you know how to add that hidden property to the .plist file maybe through terminal? Thanks so much!

If it’s new machines there may not be a plist file at all, let alone that property list item. As such your best bet would be to create a Safari plist file manually, include it in the resources folder, and copy it to the preferences folder as you initially suggested.

The best bet would be to do both. In other words try the first way, and if there’s an error then as a backup just copy the file over. It’s simple to do it. Just put the first code inside a try block and the new code inside the “on error” section like this…

try
– the first code goes here
on error
– copy the file over
end try