1.21 gigawatts
1.21 gigawatts

Reputation: 17746

Check if screen saver is running in Mac using AppleScript

Is there a way to check if the screen saver is running using AppleScript?

Currently I'm using the following AppleScript to start the screen saver:

tell application "System Events" 
    start current screen saver
end tell

I found this script but I don't know if that's correct or how to write that into my script.

What I want is something like this:

tell application "System Events" 
    if screen saver is not activated then
        start current screen saver
    end if
end tell

Upvotes: 1

Views: 914

Answers (2)

Jack Lippold
Jack Lippold

Reputation: 51

I know it's a bit late now but for others trying similar things.

I used the following recently in a shell script linked to a cronjob.

# check if screen saver is running
saverActive=$(osascript -e "tell application \"System Events\"" -e "get running of screen saver preferences" -e "end tell")
if [ "$saverActive" = "true" ]; then
  echo "Screen saver is running"
  osascript -e "tell application \"System Events\" to stop current screen saver"
  osascript -e "tell application \"System Events\" to start current screen saver"
else
  echo "Screen saver is not running"
fi

This script was used in the context of periodically changing the screensaver.

Upvotes: 1

regulus6633
regulus6633

Reputation: 19032

Try this...

tell application "System Events"
    if not (running of screen saver preferences) then start current screen saver
end tell

Upvotes: 0

Related Questions