Make Safari relaunch if quit?

Total n00b. I have searched for a ready-made solution to my needs, including on this forum, but with no luck. Perhaps you genii can help? (I last programmed seriously with BBC Basic, although I’ve tweaked some bits of Perl etc. on web sites - nothing adventurous and certainly nothing from scratch.)

Situation: I have two iMacs in a corridor at work, for general use by our volunteer staff. They’re primarily used to check web mail, surf the 'Net etc.

Problem: some folks tend to quit the Safari application when they walk away from the computer. But I would like it to be available immediately for the next user.

Objective: an Applescript, I suppose, that will detect when Safari has been quit and, after a short delay, tell Finder to start it again and display the appropriate home page as set in Safari’s preferences.

Additional: I don’t want to prevent the user from quitting the application. I don’t want an ‘unquittable’ kiosk effect. I want folks to be able to enjoy the Mac experience - up to a point.

If anyone has a moment to either point me in the direction of resources that can help me make this happen, or simply tell me how to do it, you’d make me very happy.


Model: iMac
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

This will check if Safari is open every 60 seconds.
Save the script as an ‘application bundle’ on Tiger or Leopard or an ‘application’ on Snow Leopard.
When you Save the script, make sure you check-mark ‘Stay Open’.
You can set it as a login script in your ‘System Preferences’ > ‘Accounts’ > ‘Login Items’ pane.
If you want it to run in the background (not display in the Dock) - go here.

on idle
	tell application "System Events" to set foo to (exists process "Safari")
	if foo then -- Safari is running
		return 60 -- seconds -- adjust to your liking
		tell application "Safari" to activate
	end if
end idle


Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)


It probably doesn’t make much difference here, but ideally, an idle handler should always return a specified result:

on idle
	tell application "System Events" to set foo to (exists process "Safari")
	if not foo then -- Safari is not running
		tell application "Safari" to activate
	end if
	return 60 -- seconds -- adjust to your liking
end idle


the most elegant (and inexpensive) way is a launchd agent.
It restarts Safari immediately after it has been quit

[code]<?xml version="1.0" encoding="UTF-8"?>

Label ProgramArguments /Applications/ KeepAlive [/code] save the code as plain text UTF-8 encoded in ~/Library/LaunchAgents/ and activate the agent with [code]launchctl -w load ~/Library/LaunchAgents/[/code]


This may help if you are not familiar with Terminal…
It uses Stefan code, but just does all the steps for you.

set appString to "/Applications/"
set LabelString to ""
set plistString to LabelString & ".plist"
set code to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"\">
<plist version=\">
<string>" & LabelString & "</string>
<string>" & appString & "</string>
do shell script "echo " & quoted form of code & "> ~/Library/LaunchAgents/" & plistString
delay 1
do shell script "launchctl -w load ~/Library/LaunchAgents/" & plistString

Thank you Stefan, it’s an elegant solution, and it works like a charm. I even wrote a blog post about your plist.