Benches macOS

7

AppleScript Ignoring "with timeout" Comma…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi all. I'm experimenting with the idea of using my screensaver as my Desktop Background. In order to continue using a script in the macosxhints site that locks the screen via the keychain menubar item (so I'm not forced to have it require a password when it normally accesses the screensaver), I need to modify it so that it turns the said feature off and displays a dialog enabling me to turn it back on when I return.

Here's the problem: By the time I do return, the script times out on me, preventing me from accessing its dialog. I've applied the "with timeout" command, but it doesn't seem to have any effect on the script at all. Even a "with timeout of 1 second" command does not change its behavior from the default timeout time. Any help here? Here's what I've been able to come up with thus far:

with timeout of 86400 seconds

tell application "System Events"

if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit

end tell

activate application "SystemUIServer"

tell application "System Events"

tell process "SystemUIServer"

repeat with i from 1 to number of menu bar items of menu bar 1

tell menu bar item i of menu bar 1

click

try

if name of menu item 1 of front menu is "Lock Screen" then

click menu item "Lock Screen" of front menu

exit repeat

end if

end try

end tell

end repeat

end tell

end tell

display dialog "Select the desktop background type:" buttons {"Normal", "ScreenSaver"} default button 2

if button returned of result = "ScreenSaver" then

do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &"

else

tell application "System Events"

if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit

end tell

end if

end timeout

Thanks in advanced!

7 replies

Sign in or join Beachball Help to reply.

  1. 0

    Andrew99

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    This modified version hasn't timed out on me. Note the changes where indicated in the script comments:

    *with timeout of 86400 seconds*

    *tell application "System Events"*

    *if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit*

    *-- 'end tell' that was here has been moved to the end of the script*

    *activate application "SystemUIServer"*

    *tell application "System Events"*

    *tell process "SystemUIServer"*

    *repeat with i from 1 to number of menu bar items of menu bar 1*

    *tell menu bar item i of menu bar 1*

    click

    try

    *if name of menu item 1 of front menu is "Lock Screen" then*

    *click menu item "Lock Screen" of front menu*

    *exit repeat*

    *end if*

    *end try*

    *end tell*

    *end repeat*

    *end tell*

    *end tell*

    *delay 1 -- added to prevent the dialog from appearing briefly before screen saver kicks in*

    *display dialog "Select the desktop background type:" buttons {"Normal", "ScreenSaver"} default button 2*

    *if button returned of result = "ScreenSaver" then*

    *do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &"*

    else

    *tell application "System Events"*

    *if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit*

    *end tell*

    *end if*

    *-- 'end timeout' that was here has been moved to the end*

    *end tell -- new location for 'end tell' that was moved from the beginning of the script*

    *end timeout -- new location*

    Good luck.

  2. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I think you misunderstand the purpose of
    with timeout.

    with timeout tell AppleScript how long it should wait for an application to respond to any command it's given. It is designed to catch stuck processes and allow your script to continue gracefully. It has no impact on how quickly the script itself will run, not will it have any impact if each command executes in a timely manner.

    In other words, walking through your script with its
    with timeout of 86400 seconds it will wait up to one day for 'ScreenSaverEngine to quit', one day for SystemUIServer to activate, one day for each click of the menu items, etc. If these events happen quickly (which they typically will) then the timeout isn't having any effect.

    That said, it isn't clear to me what you're expecting the timeout to achieve. Maybe if you could explain that someone can suggest a better method.

  3. 0

    Viewer07

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I wish to have the dialog in the script to be waiting for me when I return to my MacBook and enter my password to unlock the screen. Instead, I get the message "AppleEvent timed out.".

    I was expecting that the "with timeout" command would be able to solve this issue, but it has had no effect no matter how I try to use it. Is there something I'm doing wrong?

  4. 0

    Andrew99

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    This modified version hasn't timed out on me. Note the changes where indicated in the script comments:

    *with timeout of 86400 seconds*

    *tell application "System Events"*

    *if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit*

    *-- 'end tell' that was here has been moved to the end of the script*

    *activate application "SystemUIServer"*

    *tell application "System Events"*

    *tell process "SystemUIServer"*

    *repeat with i from 1 to number of menu bar items of menu bar 1*

    *tell menu bar item i of menu bar 1*

    click

    try

    *if name of menu item 1 of front menu is "Lock Screen" then*

    *click menu item "Lock Screen" of front menu*

    *exit repeat*

    *end if*

    *end try*

    *end tell*

    *end repeat*

    *end tell*

    *end tell*

    *delay 1 -- added to prevent the dialog from appearing briefly before screen saver kicks in*

    *display dialog "Select the desktop background type:" buttons {"Normal", "ScreenSaver"} default button 2*

    *if button returned of result = "ScreenSaver" then*

    *do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &"*

    else

    *tell application "System Events"*

    *if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit*

    *end tell*

    *end if*

    *-- 'end timeout' that was here has been moved to the end*

    *end tell -- new location for 'end tell' that was moved from the beginning of the script*

    *end timeout -- new location*

    Good luck.

  5. 0

    Viewer07

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks Andrew99. Your modified script works perfectly.

    However, I came up with my own solution just as you posted yours. You see, my intention has been to activate this behavior with a keyboard shortcut via a Quicksilver trigger, which I prefer to any mouse-related actions (sorry for not clarifying this sooner). In the process of making a script that gui scripts the Security Pane of System Preferences to eliminate the need for the Keychain menubar item (I need all the space I can get up there), I somehow eliminated the timeout issue all together. For anyone interested Here's my code:

    tell application "System Events"

    if process "ScreenSaverEngine" exists then tell application "ScreenSaverEngine" to quit

    end tell

    --removes the screensaver wallpaper so the screen can be locked.

    do shell script "'/Users/Dan/Library/Application Support/Quicksilver/PlugIns/Extra Scripts.qsplugin/Contents/Resources/ExtraScripts/Processes/LockScreen'; exit"

    (* uses Quicksilver's LockScreen plugin to lock the screen. There's a more direct method that doesn't involve anything from Quicksilver, but I can't be certain that it behaves the same way as this. Could anyone familiar with this plugin enlighten me on exactly how it does what it does? It appears to involve either AppleScript or Unix shell script. *)

    display dialog "Welcome Back!"

    tell application "System Events"

    if (name of processes) contains "System Preferences" then

    tell application "System Preferences"

    end tell

    (* If System Preferences is already open, the LockScreen plugin does not change any settings, so nothing is needed to be done to counter-act it. I think this part could use some clean-up. *)

    else

    tell application "System Preferences"

    set current pane to pane "Security"

    tell application "System Events"

    tell application process "System Preferences"

    click checkbox 1 of window 1

    end tell

    end tell

    quit

    (* If System Preferences is not already running, the plugin turns on the checkbox for "Require password to wake this computer from sleep or screen saver" in the Security pane. The script responds to this by turning it back off via gui scripting when the user returns. If this is done any earlier, the screen saver will persist, but a password will no longer be needed to access the computer. BE CAREFUL WHEN MODIFYING.*)

    end tell

    end if

    do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &"

    end tell

    --Restores the screevsaver as the Desktop image.

    While I'm quite happy with this, as a person who's just beginning to tap into the power of AppleScript I'm beginning to wonder if it can be further improved upon. It could most likely use a little neatening up in certain parts. Is there by anychance a way to by-pass the need for the dialog considering the script's behavior discussed in its comments? Thanks in advanced!

  6. 0

    Andrew99

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    If I understand correctly, you need the "Require password..." checkbox to be checked +only for the duration of the script.+ When the script exits, you want the checkbox unchecked. If this is correct, you might try the script below. It doesn't require using the Quicksilver plugin (although you should be able to assign a keyboard shortcut to it).

    +The script:+

    *on check_checkbox1()*

    *tell application "System Preferences"*

    activate

    *reveal anchor of pane "Security"*

    *delay 0.5*

    *tell application "System Events"*

    *tell process "System Preferences"*

    *tell window 1*

    *if value of checkbox 1 is not 1 then -- checks checkbox 1 even if it's already checked; otherwise 'click checkbox 1' by itself would act as a toggle and might shut it off*

    *click checkbox 1*

    *end if*

    *end tell*

    *end tell*

    *end tell*

    *quit -- quits System Preferences*

    *end tell*

    *end check_checkbox1*

    *on uncheck_checkbox1()*

    *tell application "System Preferences"*

    activate

    *reveal anchor of pane "Security"*

    *delay 0.5*

    *tell application "System Events"*

    *tell process "System Preferences"*

    *tell window 1*

    *click checkbox 1 -- unchecks checkbox 1*

    *end tell*

    *end tell*

    *end tell*

    *quit -- quits System Preferences*

    *end tell*

    *end uncheck_checkbox1*

    *tell application "System Events"*

    *with timeout of 86400 seconds*

    *my check_checkbox1() -- first handler called; checks the checkbox*

    *if process "ScreenSaverEngine" exists then*

    *tell application "ScreenSaverEngine" to quit*

    *do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine &"*

    else

    *if not (exists process "ScreenSaverEngine") then*

    *do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine &"*

    *end if*

    *end if*

    *display dialog " Welcome Back!" & return & return & " Select the desktop background type:" buttons {"Normal", "ScreenSaver"} default button 2*

    *if button returned of result = "ScreenSaver" then*

    *do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.a pp/Contents/MacOS/ScreenSaverEngine -background > /dev/null 2>&1 &"*

    *end if*

    *my uncheck_checkbox1() -- second handler called; unchecks the checkbox*

    *end timeout*

    *end tell*

    Of course, a much simpler script could be written if you left the "Require password..." checkbox checked at all times. It would run a lot faster too.

    Good luck!

  7. 0

    Viewer07

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Well, I was just wondering if it was possible for me to create a script that would perform all of this without a dialog involved (and thus, no additional keyboard or mouse input needed after entering my password). Good point about the security of a definite switch vs. a toggling command, though.