PDA

View Full Version : Applescript, shell commands, pauses!




BileGhost
Nov 4, 2008, 09:16 PM
I'm trying to make an applescript that toggles the background screen saver thingy (to see what I mean, type this in terminal: "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background")

do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background" works fine to start it, the problem comes when I want to stop it. I tried making a dialog so you could choose start or stop, and take appropriate action. But if you click start on the first one, when you run the script again to try to stop it, no dialog box shows up. When I place the code in its own script, it works, but only from the script editor. If I run it from the menu bar list it doesn't work.

Heres the script:

display dialog "Set BG Screen Saver:" buttons {"ON", "OFF"} ¬
default button 2
copy the result as list to {buttonpressed}
try
if the buttonpressed is "ON" then do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background"
if the buttonpressed is "OFF" then do shell script "killall ScreenSaverEngine"
end try

It works to start it, but then the script editor freezes until I end the screensaverengine with the terminal.

-BG



satyam90
Nov 5, 2008, 03:41 AM
put an "&" at the end of command and try.
I don't know why -background is not working......

BileGhost
Nov 5, 2008, 04:26 AM
At the end of which one? -Background is working, it starts the background screensaver fine, it just can't stop it.

satyam90
Nov 5, 2008, 04:32 AM
/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background&

BileGhost
Nov 5, 2008, 06:36 PM
None, that didn't help. What's it supposed to do? Any other ideas?

HiRez
Nov 5, 2008, 11:56 PM
I think you need a space before the ampersand.

BileGhost
Nov 6, 2008, 06:37 PM
That didn't work either. What's the ampersand supposed to do?

HiRez
Nov 6, 2008, 07:35 PM
That didn't work either. What's the ampersand supposed to do?It tells the shell to run it as a background process, which is generally how you get an AppleScript to not block while waiting for "do shell script" processes to complete. The ampersand flag is part of the UNIX tools, it's not related to AppleScript, so you can use it from the command line as well. If that's not working in your case, though, I don't know what else to try.

BileGhost
Nov 6, 2008, 10:24 PM
Here's my adjusted code. Is this how I was supposed to do it?

display dialog "Set BG Screen Saver:" buttons {"ON", "OFF"} ¬
default button 2
copy the result as list to {buttonpressed}
try
if the buttonpressed is "ON" then do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &"
if the buttonpressed is "OFF" then do shell script "killall ScreenSaverEngine"
end try

HiRez
Nov 7, 2008, 02:48 PM
OK, I researched a little and in addition to the "&" background flag, there is another trick. You also have to redirect stdout and stderr pipes to /dev/null (nowhere) so your script doesn't hang. It's easy to do this, you just tack on " &> /dev/null &" to the end of your shell command (the first & is backgrounding the original process). I rewrote your script slightly with the mod (I also used a friendlier way to make the screensaver quit):

display dialog "Set BG Screen Saver:" buttons {"ON", "OFF"} default button 2
copy the result as list to {buttonpressed}
try
if the buttonpressed is "ON" then
do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &> /dev/null &"
else if the buttonpressed is "OFF" then
tell application "ScreenSaverEngine" to quit
end if
end tryI think that works, try it and see what happens.

BileGhost
Nov 9, 2008, 01:38 AM
That works brilliantly :), cheers for that.

I was telling it to quit like that, but it wasn't working, so I tried changing it to the way I had it there, and never changed it back even when it didn't work.

Thanks again,

-BG