Kill Safari

I have been playing with a script for a couple of days. My god what a Mind F*** to to get it to work.

any way it seems to.

You can set the Hours in the day the young ones can use safari.
If they are out side these Hours then Safari will quit unless you grant more time.

They are also given a set duration to start with. something like 30 minutes.
This will start as soon as the app first kicks in. and finish regardless if Safari is running or not.
Again if they need more time or Its ok for them to have some time out side the ‘Hours’ you can grant this.

The granting of more time is offered when access time is up. Just enter the Minutes where indicated ie. 5 for five minutes, 120 for two hours.
The script will detect when a new day has started and in effect reset to the original allowance.
I use Safari to display all the dialogs. This stops it being used in the background also if the dialogs time out safari will quit.

There is still work to do, Like getting the reset to be more smart.
And I think the grant access is going to be the Main way to control when the young one can and can not use safari.
The Access code works, but it would be nice to use the ‘with administrators Privileges’ I got it to work and pop up b;ah, blah.
But it does not lock out safari, and puts the script on hold.

I will be testing this in a controlled account. and seeing about putting it into a Shell within Launchd ( using Lingon )


property Access_Time : {"17", "18", "19"} -- Set the hours of Access time here in 24s Clock style. do not enter minutes
property Day_ : ""
property text_returned : ""
property Get_Hr : ""
property Get_Day : ""
property time_span : 1 -- set time allowed for that day. here it is 1 minute for testing
property Timer : (time_span) as number
property Adm_check : "mycode" -- set the Access Code for adding more time after access time runs out
property timefromnow_time : (current date) + (Timer * minutes)
get timefromnow_time
set text_returned to ""
set Get_Hr to do shell script "date +'%H'"
set Get_Day to do shell script "date +'%u'"

on idle
	
	tell application "System Events"
		if exists (process "safari") then
			
			my access_InTime()
			
		else
			return 5
			
		end if
		
	end tell
	
	
end idle
get timefromnow_time
on access_InTime()
	tell application "Safari"
		if (current date) is less than (timefromnow_time) and Get_Hr is in Access_Time and Get_Day is equal to Day_ then
			
			--say "same day" --test
			
			--say "Safari access allowed"
		else if (current date) is greater than (timefromnow_time) then
			my access_outTime()
		else if Get_Hr is in Access_Time and Get_Day is not equal to Day_ then
			
			set Day_ to Get_Day
			--say "Safari access allowed"
			--say "new day" --test
			
			return 5
		else
			
			my access_outTime()
		end if
		
	end tell
end access_InTime


on access_outTime()
	say "out off access time"
	tell application "Safari" -- insures they can not use safari, until dialog is cleared.
		display dialog "Access Time at an End" buttons {"Quit", "More time"} default button 1 giving up after 10
		
	end tell
	if the button returned of the result is "Quit" then
		
		say "quit-ing Safari"
		
		my Quit_Safari()
	else if the button returned of the result is "More time" then
		try
			my more_time()
		on error
			beep
			say "more time not granted"
			tell application "Safari"
				display dialog "More Time  has not Been granted " & return & return & "This could happen if you do not enter a " & return & "Number for the minutes,  The Dialogue time out,  Or wrong access code entered" buttons {"OK"} default button 1 giving up after 10
			end tell
			my Quit_Safari()
		end try
		
		
	else
		say " More time not granted :Selection Timed out"
		my Quit_Safari()
	end if
	
end access_outTime
on more_time()
	say "more Time selected"
	--try
	tell application "Safari"
		display dialog "Enter More Access Time  - In Minutes " default answer "" buttons {"Cancel", "Ok"} default button 2 giving up after 10
		copy the result as list to {text_returned2, button_pressed}
		set returned_time to (text_returned2 * minutes)
		if text_returned2 is "" then
			
			my more_time()
		end if
		
	end tell
	
	(*
on error
		beep
		say "Please Enter Number of Minutes"
		my more_time()
		
	end try
*)
	if button_pressed is "Ok" then
		my _access(returned_time)
	else
		say "more time not granted"
		my Quit_Safari()
	end if
	
end more_time


on _access(returned_time)
	tell application "Safari"
		
		display dialog "Enter Code" default answer "" buttons {"Cancel", "Ok"} default button 2 giving up after 10 with hidden answer
		copy the result as list to {text_returned1, button_pressed}
		if button_pressed is "Ok" then
			if text_returned1 is equal to Adm_check then
				say "More Access Granted"
				---	say returned_time
				return returned_time
			else
				say "Wrong Code - More Access Not Granted"
				set Adm_check to ""
				my Quit_Safari()
			end if
			
		end if
		
	end tell
end _access

on Quit_Safari()
	tell application "Safari" to quit
	return 5
end Quit_Safari

** Edit need to find a way to quit safari if it is in the middle f Downloading.
ignoring application responses

	tell application process "Safari" to quit

end ignoring

does not work for that.

i thought i’d add a quick way to disallow access for a ‘Managed’ user:

set adminPass to "myAdminPassword"  ---put your password in here. 

set myChoice to button returned of (display dialog "Make your choice:  " buttons {"Kill Safari", "unKill Safari"} default button "Kill Safari")

if myChoice is "Kill Safari" then
	set myPerms to 770
else
	set myPerms to 775
end if

do shell script "/bin/chmod " & myPerms & " /Applications/Safari.app/Contents/MacOS/Safari" password adminPass with administrator privileges

this will still allow admin users to use Safari. although this doesn’t address all of the OP’s concerns, the concept could be used with a cron job to limit access to certain periods of the day…

cheers.

Ok I think I have a Smarter Reset Timer for Daily allowance.
The Script works the same but now it will only count the time Safari is up and running.

Where before if the Initial daily ( time_span) allowance was 30 minutes, on a new day it would just get wiped out by 00.30 hundred hours regardless if Safari was running.

Now when Safari is not running the Timer will pause. And continue when Safari is Running again.
This gives the user all of the daily time in chunks if they need it. ( so long as they quit Safari when not using it)
You can still allow extra time after the Initial daily ( time_span) allowance has run out.
If the user tries to use safari out side the Hour’s ( Access_Time ) Allowed they will lose 5 seconds of their daily time (symptom of the setup, but I like the idea)

Come the new day the Initial daily ( time_span) allowance will be reset for that new day.

*NOTE: I have left a couple of Speech Checks for testing purposes - when you are ready to switch them off (comment out) they will be the ones marked - ( speech for testing purpose *)


property Access_Time : {"17", "18", "19"} -- Set the hours of Access time here. do not enter minutes
property Day_ : ""
property text_returned : ""
property Get_Hr : ""
property Get_Day : ""
property time_span : (1 as integer) * minutes -- set time allowed for that day. here it is 1 minute for testing
property Timer : (time_span)
property Adm_check : "mycode" -- set the Access Code for adding more time after access time runs out
property timefromnow_time : (current date) + (time_span * minutes)
get timefromnow_time
set text_returned to ""
set Get_Hr to do shell script "date +'%H'"
set Get_Day to do shell script "date +'%u'"

on idle
	
	tell application "System Events"
		if exists (process "safari") then
			set Timer to (Timer) - 5 as integer
			say Timer (* speech for testing purpose *)
			
			my access_InTime()
			
		else
			return 5
			
		end if
		
	end tell
	
	
end idle
get timefromnow_time
on access_InTime()
	tell application "Safari"
		if (current date) is less than (timefromnow_time) and Get_Hr is in Access_Time and Get_Day is equal to Day_ and Timer is greater than (0 as integer) then
			
			
			
			--say "same day" --test (* speech for testing purpose *)
			
			--say "Safari access allowed" (* speech for testing purpose *)
		else if (current date) is greater than (timefromnow_time) then
			my access_outTime()
		else if Get_Hr is in Access_Time and Get_Day is not equal to Day_ then
			
			set Day_ to Get_Day
			set Timer to time_span
			say "new day" (* speech for testing purpose *)
			say "Reset Timer to " & Timer (* speech for testing purpose *)
			--say "Safari access allowed" (* speech for testing purpose *)
			
			
			return 5
		else
			
			my access_outTime()
		end if
		
	end tell
end access_InTime


on access_outTime()
	say "out off access time"
	tell application "Safari" -- insures they can not use safari, until dialog is cleared.
activate
		display dialog "Access Time at an End" buttons {"Quit", "More time"} default button 1 giving up after 10
		
	end tell
	if the button returned of the result is "Quit" then
		
		say "quit-ing Safari"
		
		my Quit_Safari()
	else if the button returned of the result is "More time" then
		try
			my more_time()
		on error
			beep
			say "more time not granted"
			tell application "Safari"
activate
				display dialog "More Time  has not Been granted " & return & return & "This could happen if you do not enter a " & return & "Number for the minutes,  The Dialogue timed Out,  Or wrong access code entered" buttons {"OK"} default button 1 giving up after 10
			end tell
			my Quit_Safari()
		end try
		
		
	else
		say " More time not granted :Selection Timed out"
		my Quit_Safari()
	end if
	
end access_outTime
on more_time()
	say "more Time selected"
	--try
	tell application "Safari"
activate
		display dialog "Enter More Access Time  - In Minutes " default answer "" buttons {"Cancel", "Ok"} default button 2 giving up after 10
		copy the result as list to {text_returned2, button_pressed}
		if button_pressed is "Ok" and text_returned2 is "" then
			
			my more_time()
		end if
		
		set returned_time to (text_returned2 * minutes)
	end tell
	
	(*
on error
		beep
		say "Please Enter Number of Minutes"
		my more_time()
		
	end try
*)
	if button_pressed is "Ok" then
		my _access(returned_time)
	else
		say "more time not granted"
		my Quit_Safari()
	end if
	
end more_time


on _access(returned_time)
	tell application "Safari"
activate
		
		display dialog "Enter Code" default answer "" buttons {"Cancel", "Ok"} default button 2 giving up after 10 with hidden answer
		copy the result as list to {text_returned1, button_pressed}
		if button_pressed is "Ok" then
			if text_returned1 is equal to Adm_check then
				say "More Access Granted"
				---	say returned_time (* speech for testing purpose *)
				return returned_time
			else
				say "Wrong Code - More Access Not Granted"
				set Adm_check to ""
				my Quit_Safari()
			end if
			
		end if
		
	end tell
end _access

on Quit_Safari()
	tell application "Safari" 
activate
quit
end tell
	return 5
end Quit_Safari

Excellent idea, thanks.

j

Thanks,
Let me know how it works for you. Cheers.

Still need to test setting it up with the Controlled user…

Extremely impressive Mark. :smiley:

Thanks Adam. :smiley:

I just came a back and found a application Timeout of the applescript app…

Realized that if Safari displays a dialog but is not the frontmost, the applescript app will timeout and not quit safari.
Fixed this and updated the script in my last post to make sure Safari is activated when displaying dialog and also nano seconds
before the quit instruction is sent.

The thing I found about this script is it the need to soak test, But getting there.

Typical of any event-driven script; until you’ve tested for quite a while, you don’t know what combinations of events might occur. I built a controller one time that simply crashed if you pressed what amounted to “Go” and “Stop” at the same time, but whoda thunk?