Possible to make a applescript that works like a screensaver?

look at this pages
http://www.macosxhints.com/article.php?story=20040330161158532
http://bbs.applescript.net/viewtopic.php?id=16030

Also I run this in one of my scripts to run part of my script if there is no keys pressed for a set period of time.
i.e I am not using the mac

I can not remember where I found my part but it may help you do a search.
The part of AS below is put with in a idle handler


-- idle handler and other parts of script here which runs every 30 seconds or so -----

set idletime to do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'"
	set idletime to idletime as string
	set idletime to idletime as integer
	if idletime is greater than 110 then  -- 110 = 1 minute and 50 seconds without a key being pressed
---- your action here -----
else
---- your other action here -----

you could try and google

“ioreg -c IOHIDSystem applescript”

Phistoprince & Mark thank both off you for your time responding…

Mark will test your suggestion, let you know when it’s ready.

Greetings

Bob

The idletime sollutions was a liitle bit to much asked for my limited Applescript skils :wink:

so searched a little bit further on this forum and found some stuff that could work (in this case not)

tell application “System Events”
set ScreenSaverEngine_exists to (exists process “ScreenSaverEngine”)
end tell
if ScreenSaverEngine_exists then
activate application “C4D Client”
end if

This one doesn’t work

also tried the following:

tell application “System Events”
if not (exists process “ScreenSaverEngine”) then
tell application “C4D Client”
activate
end tell
end if
end tell

But that script triggers it directly… guess it must be the IF NOT … that should be IF YES, but that does not work.

Anyone has a clue?

Thanks

save this as a application and stay open ticked.

change the process to your process name.
and the seconds to what you want.

on idle
	
	set idletime to do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'"
	set idletime to idletime as string
	set idletime to idletime as integer
	tell application "System Events"
		
		if idletime is greater than 120 then (* 120 is 120 seconds. If a key was NOT tapped 
		within the 120 seconds it then checks to see if the app was already open. *)
			if not (exists process "chess") then
				launch application "Chess"
				return 30 -- checks again in 30 seconds
			else
				return 30 -- a key was tapped within the 120 seconds so checks again in 30 seconds
				
			end if
		end if
	end tell
end idle

Hi guys.

Here’s another way of doing it. (If the target app is already up, the launch command won’t have any effect - so we can avoid an if/then statement):

property max_idle : 30 (* the number of seconds the system must be idle before your routine kicks in: modify as required *)

on idle
	tell max_idle - (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'") to if it > 0 then return it
	launch application "Chess" (* or whatever: modify as required *)
	max_idle
end idle

Hello Mark & Kai,

Tried it like you discribed but it both didn’t work.

ps have system 10.4.6 and have Enabled acces for assistive devices (for using System Events)
Further have the Energy Saver settings to Restored Defaults.
Perhaps it can be caused due the fact thats a little bit hot overhere in the Netherlands so the ventilator is working hard too cool the machine down?

Think we are close thanks to your effort…

Bob

You don’t say how the scripts don’t work, Bob. Is there an error message?

If not, perhaps we should just make sure that some of the fundamentals are in place. For an idle handler to work correctly, the script must be saved as a stay-open application. Specifically, that means:

¢ Select the "File/Save as..." (Archief/Bewaar als...") menu
¢ In the resulting Save dialog, click on the menu button labelled "File Format:" ("Structuur:")
¢ In the menu that appears, select the "application" ("Programma:) option
¢ From the 3 check boxes labelled "Options:" ("Opties:") check only the box marked "Stay Open" ("Niet sluiten")
¢ Click the "Save" ("Bewaar") button

Then try running the script application by double-clicking its icon in Finder.

If you’ve already tried this, then I apologise. Perhaps you could give us a little more detail about what does or what doesn’t happen. :slight_smile:

Hallo Kai,

I did it exactly like you discribed it (sorry that I was not clear on that part). So when starting the script the chess program (and tested the Cinema 4D render client but that worked neither) does not start, not even after waiting 5 minutes and doing nothing woth the machine (and hard to do not to work for 5 minutes, get a little bit of neurotic :wink: )

Mmmm strange isnt it? Maybee there is a bacground program running I’m not aware off…need to investigate that I think…

Thanks

Bob

Curious, Bob. Works perfectly here (Mac OS X 10.4.6, too). If I leave my machine alone for 30 seconds (not easy, but there you go) - the Chess application pops up without fail… :confused:

How about a shorter test? What result does this give you? (Just run from Script Editor):

delay 10
do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'"
--> "10" (* The result I get here *)

the result when I press on run within the scripteditor is

“7”

Seems like there might be something jumping in there, Bob.

The following routine, which aims to take a sample roughly every second, should give you a better indication. At this level of granularity, the shell introduces a certain amount of delay - but you can see from my results that the value never returns to “0”. I suspect that, with your results, it might - but it could also give you an indication of how frequently that occurs:

set l to {}
repeat 100 times
	set l's end to do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'"
	delay 1
end repeat
l --> {"0", "1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12", "13", "14", "15", "17", "18", "19", "20", "21", "22", "23", "25", "26", "27", "28", "29", "30", "31", "33", "34", "35", "36", "37", "38", "39", "40", "42", "43", "44", "45", "46", "47", "48", "50", "51", "52", "53", "54", "55", "56", "58", "59", "60", "61", "62", "63", "64", "66", "67", "68", "69", "70", "71", "72", "74", "75", "76", "77", "78", "79", "80", "81", "83", "84", "85", "86", "87", "88", "89", "91", "92", "93", "94", "95", "96", "97", "98", "100", "101", "102", "103", "104", "105", "106", "108", "109", "110", "111", "112"}

{“0”, “0”, “0”, “0”, “1”, “0”, “1”, “2”, “3”, “4”, “5”, “6”, “8”, “9”, “0”, “1”, “3”, “4”, “5”, “6”, “7”, “9”, “0”, “1”, “2”, “3”, “0”, “1”, “2”, “4”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “0”, “1”, “2”, “3”, “4”, “5”, “0”, “1”, “2”, “3”, “0”, “1”, “2”, “4”, “5”, “0”, “1”, “3”, “4”, “5”, “6”, “7”, “8”, “0”, “0”, “2”, “3”, “4”, “5”, “6”, “8”, “9”, “0”, “1”, “2”, “0”, “1”, “2”, “3”, “4”, “0”, “1”, “2”, “3”, “4”, “5”, “6”, “8”, “0”, “0”, “1”, “2”, “3”, “4”, “5”, “7”, “8”, “9”, “0”}

These results are “slightly” different then yours… for now have to quit because need to get up very early…

Will do the following tomorow: shut the machine down. Then reboot and not have any application open. Then try to run your script again… Thinking right now maybee Little snitch can cause troubles?

Anyway Kai many thanks for your time to help. Will let you know tomorrow night if the test worked better.

Greetings

Bob

Oh yes, Bob. Looks like you have something something activating every 10 seconds, there. (I’ll leave you to investigate further…) Sleep tight. :slight_smile:

Hi Bob,

Your ScreenSaverEngine script should work.

Scripts with idle handlers need to be saved as “stay open” applications. When the following script is run, it starts beeping when the screen saver comes on.


on idle
	tell application "System Events"
		set f to (exists process "ScreenSaverEngine")
	end tell
	if f then
		beep 1
	end if
	return 1
end idle

Save as “stay open” application. You can activate the screen saver app by using a hot corner, in the System Preferences, for testing.

gl,

I just wonder if whatever is stopping Bob’s machine from idling for more than ten seconds would also prevent the ScreenSaver from launching. I’d be very interested to know if ioreg detects activity that the ScreenSaver launcher doesn’t - since I’d have expected such data to come from the same source. However, if his ScreenSaver still activates successfully, then something like this should also do the trick:

on idle
	tell application "System Events" to if (exists process "ScreenSaverEngine") then launch application "Chess"
	30
end idle

Doh I changed it from activate to launch at the last minute and forgot that launch takes care of what I was trying to avoid with the if/then statement.

Thanks…

Hello all :slight_smile:

For some still unclear reasons my screensaver doesn’t start without force… but a big step further because tried kel’s

on idle
tell application “System Events”
set f to (exists process “ScreenSaverEngine”)
end tell
if f then
beep 1
end if
return 1
end idle

That one worked when I used the hotcorner from the screensaver app.

Also Kai your script worked in forced screensaver mode;

on idle
tell application “System Events” to if (exists process “ScreenSaverEngine”) then launch application “Chess”
30
end idle

Think maybee try to get rid off the pref settings from the screensaver but need to dig in it alittle bit further…

Great help really appriciate it!

Thanks

Bob

ps will let you know the follow up

After several day of finding what could cause the problem that the screensaver does not start by itself (and visited a lot off sites with possible tricks) almos giving up…but have a new idea; Is it perhaps possible that there is some code like this;

if no mouse movement after 10 minutes start screensaver and start also chess

please note this is off course Applescript but think you understand what I suggest.

ps bytheway did make a completely new account on my system and the the screensaver worked… but moving all my stuff to the new account will take me too much time.

Thanks (again)

Bob

Model: minimac PPC 1.4
Browser: Safari 417.9.3
Operating System: Mac OS X (10.4)

Hi Bob,

The mouse thing used to happen on my other computer. What I did was turn the mouse upside down. I eventually got a wireless.

gl,

Wraagh… feel like an idiot… found what caused the problem preventing my machine falling in sleep mode…it was Salling Clicker Device Preferences…had the option “keep computer awake” on…

Now it works fantastic… thank you all for your help.

Greetings

Bob