Disable/re-enable screensaver when viewing videos

Hippo Man

Hippo Man
I'm running the latest MacOS X on my new MacBook Pro. What I'd like to do is have the screensaver get automatically disabled when I view videos, and then to have it re-enabled when I am finished viewing.

Under Linux, I accomplished this by wrapping the invocation dialog of my video viewer in a script which turns off the screensaver, runs the viewer, and then turns on the screensaver again when the viewer exits. Is there a way to do something similar under MacOS X? ... maybe with AppleScript?

Yes, I know that I can manually turn off the screensaver, view the video, and the turn the screensaver back on. And I also know that I can move the mouse or interact with the keyboard periodically when viewing. However, I don't want to have to do either of these things ... I'd like this to be fully automated, if possible.

Any ideas?

Thanks in advance.
 
'Disable/re-enable screensaver when viewing videos' ... 'Is there a way to do something similar ... with AppleScript?' - yes.

'... I'd like to do is have the screensaver get automatically disabled when I view videos, and then to have it re-enabled when I am finished viewing.' - set a 'property' value to the value returned by ...

'defaults read ByHost/com.apple.screensaver.xxxxxxxxxxxx idleTime'

..., where 'xxxxxxxxxxxx' is the MAC of the current Macintosh.

Then perform ...

'defaults write ByHost/com.apple.screensaver.xxxxxxxxxxxx idleTime 0'

..., where '0' is a value in seconds, which instructs the screen saver to never activate.

'... I'd like this to be fully automated, if possible.' ... 'Any ideas?' - include the above ('defaults read ...' and 'defaults write ...') suggestions in the 'on run () ... end run' segment of your AppleScript code. Then, incorporate 'on idle () ... end idle' with a 'return' value of 10 (in seconds) or so, checking to see whether or not the video player (activated by your code in the 'on run () ... end run' segment') is still active. If (the video player) is not active then perform ...

'defaults write ByHost/com.apple.screensaver.xxxxxxxxxxxx idleTime x

..., where 'x' is the (above stored [defaults read ByHost/com.apple.screensaver.xxxxxxxxxxxx idleTime]) 'property' value in seconds, which instructs the screen saver when to activate.
 
'Disable/re-enable screensaver when viewing videos' ... 'Is there a way to do something similar ... with AppleScript?' - yes.

[ ... etc. ... ]
This is quite useful and helpful, and exactly the kind of info I was looking for. Thank you! ... and happy New Year!
 
Here is some sample code:

Note: In this example, 'TextEdit' is used as the launched and monitored application.

-- Code starts here.

property tApp : "TextEdit" -- Application to be launched and monitored.
property ethernet_ID : 0 -- 'Ethernet ID' (MAC address).
property initial_IdleTime : 0 -- Initial 'idleTime' value.

on run {}
my convert_Ethernet_ID() -- Obtain converted 'Ethernet ID' value.

-- Obtain initial 'idleTime'.
set initial_IdleTime to do shell script ("defaults read ByHost/com.apple.screensaver." & ethernet_ID & " idleTime")
do shell script ("defaults write ByHost/com.apple.screensaver." & ethernet_ID & " idleTime 0") -- Set 'idleTime' to never.

--
-- Your code here.
tell application tApp to activate -- Activate application to be launched and monitored.
-- Your code here.
--
end run


on idle {}
-- If 'tApp' is in use, check again in 10 seconds.
tell application "System Events" to if ((name of processes) contains tApp) then return 10

-- -- If 'tApp' is not in use, return 'idleTime' value to initial 'idleTime' value.
do shell script ("defaults write ByHost/com.apple.screensaver." & ethernet_ID & " idleTime " & initial_IdleTime)
quit -- Quit this 'applet'.
end idle


on convert_Ethernet_ID()
-- Obtain 'raw' 'Ethernet ID' (MAC address) value.
set local_Ethernet_ID to do shell script "/sbin/ifconfig en0 | grep ether | cut -d' ' -f 2" -- Obtains Macs' MAC address.

-- Remove ':' from 'raw' 'Ethernet ID' (MAC address) value.
set {oAStID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set local_Ethernet_ID to (text items of local_Ethernet_ID)
set AppleScript's text item delimiters to oAStID

set ethernet_ID to (local_Ethernet_ID as string) -- Set 'ethernet_ID" property to converted 'Ethernet ID' value.
end convert_Ethernet_ID

-- Code ends here.
 
Thank you very much for taking the time to show me sample code. This is not only instructive and quite useful, but it's also "above and beyond the call of duty", and I very much appreciate it.
 
I've been trying to use this AppleScript methodology for a while (see below), but it doesn't seem to properly re-enable the screen saver after the video finishes, even though I know that the following line is indeed getting executed:
do shell script ("defaults write ByHost/com.apple.screensaver." & ethernet_ID & " idleTime " & initial_IdleTime)
I know it's getting executed, because if I go into Preferences->Desktop & Screen Saver after the video stops playing, the Start Screen Saver slider indeed shows the idle time that was specified. However, even with that, the screen saver doesn't start, and I have to go into the preferences and manually move the Start Screen Saver slider and then close preferences again before the screen saver gets re-enabled.

It's as if the idle time setting doesn't get re-read by the current screen saver simply upon being reset from within the Apple Script program.

Is there an extra command that I have to give to the Apple Script program to tell the screen saver to once again start paying attention to the Start Screen Saver delay once I reset the idle time?

Thanks in advance.


Here is some sample code:

Note: In this example, 'TextEdit' is used as the launched and monitored application.

-- Code starts here.

property tApp : "TextEdit" -- Application to be launched and monitored.
property ethernet_ID : 0 -- 'Ethernet ID' (MAC address).
property initial_IdleTime : 0 -- Initial 'idleTime' value.

on run {}
my convert_Ethernet_ID() -- Obtain converted 'Ethernet ID' value.

-- Obtain initial 'idleTime'.
set initial_IdleTime to do shell script ("defaults read ByHost/com.apple.screensaver." & ethernet_ID & " idleTime")
do shell script ("defaults write ByHost/com.apple.screensaver." & ethernet_ID & " idleTime 0") -- Set 'idleTime' to never.

--
-- Your code here.
tell application tApp to activate -- Activate application to be launched and monitored.
-- Your code here.
--
end run


on idle {}
-- If 'tApp' is in use, check again in 10 seconds.
tell application "System Events" to if ((name of processes) contains tApp) then return 10

-- -- If 'tApp' is not in use, return 'idleTime' value to initial 'idleTime' value.
do shell script ("defaults write ByHost/com.apple.screensaver." & ethernet_ID & " idleTime " & initial_IdleTime)
quit -- Quit this 'applet'.
end idle


on convert_Ethernet_ID()
-- Obtain 'raw' 'Ethernet ID' (MAC address) value.
set local_Ethernet_ID to do shell script "/sbin/ifconfig en0 | grep ether | cut -d' ' -f 2" -- Obtains Macs' MAC address.

-- Remove ':' from 'raw' 'Ethernet ID' (MAC address) value.
set {oAStID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set local_Ethernet_ID to (text items of local_Ethernet_ID)
set AppleScript's text item delimiters to oAStID

set ethernet_ID to (local_Ethernet_ID as string) -- Set 'ethernet_ID" property to converted 'Ethernet ID' value.
end convert_Ethernet_ID

-- Code ends here.
 
Back
Top