Kill Safari

I would like to be able to kill the use of Safari for a certain user untill a certain time, or like midnight where it will then start the script over again. So, what I need to know is how to kill Safari so the user can’t use it and then how to make it stop at a certain time, where I can then have it restart. Any help is appreciated! Thanks!

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

First, I’m going to want more information than that. Is the user the one currently logged in? Do you mean to really kill Safari, no questions asked, or could a nicer quit suffice, with some kind of message to show the user?
And would you be able to tell us what this is for; is there no other way to achieve what you want?

Second, requests like this don’t belong in the Code Exchange forums, but instead in the one named Mac OS X.

As already noted, the Code Exchange forum is for posting full snippets/solutions. In the future, please use the other forums for help requests. This post will be moved.

Sorry guys! Ok, so I want to create a sort of timer for Safari, as a way to put a time limit on the use of Safari. Here is the situation… The user is logged on. I will want a timer that, say set to 30 minutes, will then quit Safari after the 30 minutes and not allow the user to get back into Safari untill the next day, where the script will restart. However, I would like other users to still be able to use Safari. Does that make sense? Is that possible? Thank you all for the help!

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Why only Safari? There are lots of other browsers for OS X and the person whose web time you want to limit will probably download and install Shiira, Camino, Opera, Foxfire, Mozilla, Omniweb, iCab, or Internet Explorer and press on instead of doing their homework.

To prevent surfing from the machine, you’d have to block port 80 for that user or if you didn’t want iChat, email, etc to work either, you’d have to disable the ethernet connection for the machine. Those are possible, but we need details to suggest how to do it. What is the machine, is it networked, what OS X version are you using, etc.

You can limit the access to only safari where browsers are concerned in the parental controls ( finder & System ). So it does not matter what they download.

Given that my kids are 38, 40 and 43, I’ve never looked at parental controls - didn’t know that.

:lol:

So whats the trigger. when the user logs in or a certain time of day??

Note also that IMHO you will need to do most of this in shell and cron or Lingon ( to set up a launchd ) http://lingon.sourceforge.net/ , to stop the user from killing the timer app its self.

I’d been thinking about how to do someting like this, but haven’t come up with anything that works properly.

My older child is a few weeks shy of her fourth birthday; she has a very resricted user account and her timer is the battery on my wife’s iBook, which works for now - presently I’m more concerned with limiting computer time than web time. That will change in a few years.

If I may add my 2¢, log in seems a better trigger because it would allow some flexibility - once per day rather than at a specific time per day.

It might make sense to add a dialog (giving up after a minute or two) where a parent could enter their password to extend the time a bit.

j

Thanks for the input guys. So, I am running a Powerbook with 10.4.7 networked to a home Airport Base that then connects to cable. I belive that login would be a good choice for start time. As far as problems with quitting the app goes, I can just hide it in the background…make it a hidden startup item. THat will solve the problem of starting it. I think the best way is to make it a loop, at the end, I can incorperate a text field that will allow the loop to start over again, allowing more time. The problem I am having is getting Safari to not be allowed to be re-open after I quit it. Could I possibly use menu items or something to set Safari as a restricted application after a certain amount of time. I can always re-unrestrict it when I get home. Would any of this be possible? Thanks for the help!

Model: Powerbook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Assuming parental controls will lock in Safari as the only browser (I don’t know how to do it), and that this script saved as stay-open, in the background (don’t know how to do that either), and that there’s a good method for locking Safari down (haven’t explored that), I’ve come up with this so far, but don’t have time to pursue it further.

Comments? (I haven’t tested it, so it’s a “talking” script)


-- set to startup at login. No protection for it's removal from the startup items list, so this is the weak point. Launching it from launchd would be better.
on run
	-- Check that clock is ok
	set today to current date
	set {day:d, month:mm} to today
	repeat with aServer in {"time-a.nist.gov", "time-b.nist.gov", "time-a.timefreq.bldrdoc.gov", "time-b.timefreq.bldrdoc.gov"}
		try
			set text item delimiters to space
			set theDay to (words 1 thru 2 of (do shell script "ntpdate -q " & aServer)'s paragraph -1) as string
			set text item delimiters to ""
			set now to ("" & d & space & characters 1 thru 3 of (mm as string)) as string
			if now ≠ theDay then
				my killSafari()
				quit
				exit repeat
			else
				display dialog "Clock has been reset"
				my killSafari()
			end if
		on error e
			display dialog "Internet Clocks are not responding"
			my killSafari()
		end try
	end repeat
	-- set starting time in pref file
	try
		-- check that the file is there
		(do shell script "defaults read SafariRun theTime")
		-- SafariRun is the name of the variable
		-- theTime is the value
		-- Data not used, just a check that the file is still ok.
		set {hours:h, minutes:m} to today
		set start to 60 * h + m
		do shell script ("defaults write SafariRun theTime " & start as text)
	on error --> no such file!
		-- create it if not, but know the pref file has been removed
		set {hours:h, minutes:m} to today
		set start to 60 * h + m
		do shell script ("defaults write SafariRun theTime " & start as text)
		-- or my killSafari() ?
	end try
end run

on idle
	tell application "System Events" to if exists process "Safari" then
		set start to (do shell script "defaults read SafariRun theTime") as number
		set {hours:h, minutes:m} to current date
		set runTime to 60 * h + m
		if runTime - start > 120 then my killSafari()
	end if
	return 60 -- seconds
end idle

to killSafari()
	-- however you're going to do it.
end killSafari

Thanks, that is an awsome start! Now, just to figure out how to keep Safari from being relaunched.

Model: Powerbook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Adam.

Thanks for your “talking” script"

I only had time for on quick test (other things to do, you know) and 3 successive dialogs said the clock was reset, then Safari quit, then more dialog. I didn’t give it much time after that.

I didn’t try it on a managed account. I had trouble with that using a script I came up with. On a regular account I could stop Safari, but not keep it stopped.

I’ll experiment more later.

j

Theoretically, you don’t have to. When the on idle timer rolls around again 1 minute later, it’ll quit Safari again (if quitting is what you are doing). Set the Return 60 in the idle handler to return 15 or something short, and there won’t be time to do anything before.

When I have time I’ll change the write portion to check whether this is the first run of today and if so, set a new start time, but if not, to leave the time accumulating. This way, simply logging out and in again doesn’t reset the timer. (It’s partially written but doesn’t work yet, and I’ve got to go paint some windows while the sun is up)

A further talking piece (not tested):


-- set to startup at login. No protection for it's removal from the startup items list, so this is the weak point. Launching it from launchd would be better or possibly a shell script that runs when this account boots up.
on run
	-- Check that clock is ok
	set today to current date
	set {day:d, month:mm} to today
	repeat with aServer in {"time-a.nist.gov", "time-b.nist.gov", "time-a.timefreq.bldrdoc.gov", "time-b.timefreq.bldrdoc.gov"}
		try
			set text item delimiters to space
			set theDay to (words 1 thru 2 of (do shell script "ntpdate -q " & aServer)'s paragraph -1) as string
			set text item delimiters to ""
			set now to ("" & d & space & characters 1 thru 3 of (mm as string)) as string
			if now ≠ theDay then
				my killSafari()
				quit
				exit repeat
			else
				display dialog "Clock has been reset"
				my killSafari()
			end if
		on error e -- avoids the trick of unplugging the internet cable
			display dialog "Internet Clocks are not responding; Goodbye" giving up after 30 -- to avoid leaving it there.
			-- Otherwise, just leave the dialog box and open Safari
			my killSafari()
		end try
	end repeat
	
	-- set starting time in pref file
	try
		-- check that the file is there and whether this is a new start
		set {month:mth, day:d, hours:h, minutes:m} to today
		set thisRun to (mth * 1 & space & d & space & (60 * h) + m) as text -- looks like "8 6 590"
		tell thisRun to set moDay to word 1 & "/" & word 2 -- looks like "8/6"
		set test to (do shell script "defaults read SafariRun theTime")
		-- SafariRun is the name of the variable, theTime is the stored value
		tell test to set tDay to word 1 & "/" & word 2
		if (tDay ≠ thisRun) then do shell script ("defaults write SafariRun theTime " & thisRun)
		-- so if this is the second run, tDay = thisRun, the default is not set again.
	on error --> no such file!
		-- create it if not, but know the pref file has been tampered with. 
		-- We need some way to begin so creating the pref file in advance 
		-- would leave only the killSafari option with the shell script commented 
		-- out after a mom's first run to set it up.
		set {month:mth, day:d, hours:h, minutes:m} to today
		set thisRun to (mth * 1 & space & d & space & (60 * h) + m) as text
		do shell script ("defaults write SafariRun theTime " & thisRun) -- for setup only.
		my killSafari() -- after the pref file exists.
	end try
end run

on idle -- this is the part that keeps on ticking.
	tell application "System Events" to if exists process "Safari" then
		set thisRun to (do shell script "defaults read SafariRun theTime")
		set {hours:h, minutes:m} to current date
		set now to 60 * h + m
		set soFar to last word of thisRun as number
		if soFar - now > 120 then my killSafari()
	end if
	return 15 -- seconds to next test
end idle

to killSafari()
	-- however you're going to do it.
end killSafari

Thanks! I have thought about this…Does anybody know how to work with the GUI scripting? Can we navigate through system prefrences with GUI scripting, what about clicking buttons and check boxes. In order to turn off the Safari capability on a restricted account, you need to follow these steps, hopefully someone can script this…

Apple Menu > System Prefrences
System > Accounts
–You will need to click on the lock to unlock it, then enter a password in the text box–
Tab “Parental Controls”
In Box, the “Configure” button on the “Finder and System” selection. This will be the hardest part.
In the Application Allowed window:
Applications > Uncheck the Safari Checkbox

Not sure if this can be done, but hopefully it can be. What about with mouse clicks at certain (x,y) positions. Thanks!

Model: Powerbook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi.

I’m curious. Why would you want to script that instead of doing it manually?

We have family visiting, so I haven’t tried (or even closely read) that yet, but as soon as I can I will.

j

The clue (where possible) to doing this is to find the xxx.pref file where those choices are stored and script it explicitly. I haven’t set up a “managed” user to test, but given progress on painting windows (the only paintable surfaces on the exterior of a large home), I will.

It looks to me as it is all under the Accounts.prefPane prefrence pane. At least, that is the title of the Prefrence Pane in the menu bar.

Model: Powerbook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Great. Given time (or rain) I’ll set up a managed account and see what gives.