Clear Safari Bookmarks and Replace them another Safari Bookmark file


I am running into a problem with students at my school either adding or deleting Safari bookmarks to suit their current whims of what is cool on the internet.

To combat this I would like to:

  1. create and export an “approved” bookmark file

  2. Delete old Safari bookmarks

  3. Import the “approved” bookmark file into Safari, this includes have the homepage set as well

My purpose with this applescript is to ensure that all students have the same user experience and do not get sent on to other websites that are not school related.

Thank you so much for your time and trouble.

The systems are all running 10.5.

Model: iMac
AppleScript: 2.2.1
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

Hi Paul,

the easist way to do this is to replace ~/Library/Safari/Bookmarks.plist and if necessary ~/Library/Safari/History.plist, while Safari is not open.

For example create a folder Safari in /Library/Application Support (main library) and save an approved bookmark file there.
This script copies the file into the folder Safari for every user on the machine and adjusts the access privileges.
user name and password of an admin account is required. There is an exception list where the copy task should not be applied to

property uName : "adminName"
property pWord : "¢¢¢¢"
property userExceptionList : {"user1", "user2"}

tell application "System Events" to set theUsers to name of users

set bookmarkLocation to "/Library/Safari/Bookmarks.plist"
set approvedBookmarks to quoted form of "/Library/Application Support/Safari/Bookmarks.plist"
repeat with oneUser in theUsers
	if contents of oneUser is not in userExceptionList then
			set bookmarkDestination to quoted form of ("/Users/" & oneUser & bookmarkLocation)
			do shell script "/bin/cp " & approvedBookmarks & space & bookmarkDestination user name uName password pWord with administrator privileges
			do shell script "/usr/sbin/chown " & oneUser & ":staff " & bookmarkDestination user name uName password pWord with administrator privileges
		on error
			display dialog "copy file failed for user " & oneUser
		end try
	end if
end repeat

So first I changed the homepage, then quit safari and tried your apple script.

I keep getting an error message “failure to transfer files”.

So after doing that, I manually transfered the files but the default homepage was not restored. I am wondering if safari saves more than one copy of its settings.

I am wondering if all of the files /Users/myschool/Library/Safari/ should be transfered too.

the default homepage could be changed with

do shell script "defaults write HomePage ''"

Note: this syntax affects only the current user

this is also no problem