Delay?

Hey guys im trying to write a small script that will lock the screen every 2 hours, its just a small utility i want for my self but i was wondering how would i make a script delay for 2 hours before performing the task, then reset the delay for another 2 hours, etc

Screensaver? (Check the Security PrefPane for the lock screen saver option)

I have the locking already coded i need a way to delay it, the reason for not using the built in macosx option is that it also locks from a sleep which screws up the alarm clock script i have, as well as using the auto logout is just annoying

Put your code into an “on idle” handler and set the return for 60 * (number of minutes).

http://developer.apple.com/documentation/applescript/conceptual/applescriptlangguide/AppleScript.e7.html

You’ll want to make sure it doesn’t start the screensaver immediately after double-click, of course.

Cool never knew about that, so how would i make it so it doesnt run my locking code after i launch it

on idle

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
return 60
end idle
property fire : false

on idle
	if (fire) then
		do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
	end if
	set fire to true
	return your_return_time_here
end idle

You could evaluate fire to see if it’s false, then set it to true, but I don’t see a benefit to that in this case. You will need to evaluate your situation and make that determination, of course.

You also should add some error handling around the “do shell script” command. Wrap it in a try/error block and make sure you handle errors intelligently.

Well for some reason it still automaticly runs ths script

-- AutoScreenLock.applescript
-- AutoScreenLock

--  Created by Andrew James on 2/07/06.
--  Copyright 2006 semaja2.net All rights reserved.

property normalCommand : "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
property fire : false
property idletime : ""

on launched theObject
	
	try
		tell user defaults
			-- make defaults values if they don't exist
			make new default entry at end of default entries with properties {name:"autotimer", content:120}
		end tell
		set idletime to content of default entry "autotimer" of user defaults
	on error
		set idletime to "60"
		display dialog "Error " & errNum & ": " & errMsg
	end try
end launched

on idle
	if fire is true then
		try
			do shell script normalCommand
		on error
			display dialog "Error " & errNum & ": " & errMsg
		end try
	end if
	set fire to true
	return idletime
end idle

ok well i found the problem, the idletime variable is not being passed so it is erroring with the return and making it loop right away

and ideas why the variable isnt being passed

Hi all :slight_smile:


set idletime to "60"

The “idletime” variable must be an integer class, if not, the “idle” handler can not understand the return command…
Try this:


return (idletime as integer)

:wink:

thanks fredo

i tried ur suggestion and even added a fail safe to make sure its above a limit but it still screws up

-- AutoScreenLock.applescript
-- AutoScreenLock

--  Created by Andrew James on 2/07/06.
--  Copyright 2006 semaja2.net All rights reserved.

property normalCommand : "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"
property fire : false
property idletime : ""

on launched theObject
	
	try
		tell user defaults
			-- make defaults values if they don't exist
			make new default entry at end of default entries with properties {name:"autoTimer", content:120}
		end tell
		set idletime to content of default entry "autoTimer" of user defaults as integer
	on error
		set idletime to "3600" as integer
		display dialog "Error " & errNum & ": " & errMsg
	end try
	
end launched

on idle theObject
	if fire is true then
		try
			beep
			--do shell script normalCommand
		on error
			display dialog "Error " & errNum & ": " & errMsg
		end try
	end if
	set fire to true
	if idletime is greater than 3500 then
		return (idletime as integer)
	else
		beep 10
		return 3600
		
	end if
end idle