Applescript, shell commands, pauses!

Discussion in 'Mac Programming' started by BileGhost, Nov 4, 2008.

  1. BileGhost macrumors member

    Joined:
    Jul 29, 2008
    #1
    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:
    Code:
    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
     
  2. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #2
    put an "&" at the end of command and try.
    I don't know why -background is not working......
     
  3. BileGhost thread starter macrumors member

    Joined:
    Jul 29, 2008
    #3
    At the end of which one? -Background is working, it starts the background screensaver fine, it just can't stop it.
     
  4. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #4
    /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background&
     
  5. BileGhost thread starter macrumors member

    Joined:
    Jul 29, 2008
    #5
    None, that didn't help. What's it supposed to do? Any other ideas?
     
  6. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    I think you need a space before the ampersand.
     
  7. BileGhost thread starter macrumors member

    Joined:
    Jul 29, 2008
    #7
    That didn't work either. What's the ampersand supposed to do?
     
  8. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #8
    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.
     
  9. BileGhost thread starter macrumors member

    Joined:
    Jul 29, 2008
    #9
    Here's my adjusted code. Is this how I was supposed to do it?

    Code:
    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
     
  10. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #10
    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):

    Code:
    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 try
    I think that works, try it and see what happens.
     
  11. BileGhost thread starter macrumors member

    Joined:
    Jul 29, 2008
    #11
    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
     
  12. denniesmit macrumors newbie

    Joined:
    Oct 5, 2012
    #12
    Hi.
    I realize it is 4 years later, but I hope somebody will read this.

    I am trying to do something similar. Except I do not want a button that says on or off. I want the applescript to start the background screensaver, wait 5 minutes, then stop.
    Then after 1 second I want it to start from the top. Then reapeat endlessly.
    So :
    The screensaver kicks in for 5 minutes, then it stops itself, a second later it starts again for 5 minutes, then it stops itself, etc..

    I tried this:
    repeat
    do shell script
    "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &> /dev/null &"
    delay 300
    tell application "ScreenSaverEngine" to quit
    delay 1
    end repeat

    I get the error: Can’t make current application into type string.

    Please help! Thank you,
    Danielle
     
  13. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #13
    Paste the code into ScriptEditor.

    Code:
    repeat
    	do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &> /dev/null &"
    	delay 300
    	tell application "ScreenSaverEngine" to quit
    	delay 1
    end repeat
     
  14. denniesmit macrumors newbie

    Joined:
    Oct 5, 2012
  15. iownafordprobe macrumors newbie

    Joined:
    Oct 25, 2013
    #15
    Hi, I'm digging up this old thread a year later because I have a similar question.

    I'm trying set an applescript variable to the text returned on a shell script command, but every time i do it through the "Do Shell Script" method the applescript variable remains blank. I'm running SoX on a audio file with the "stat" effect, which outputs a text analysis of the audio file. Here's what my command looks like:

    Code:
    set soxOut do shell script "sox " & quoted form of fullPath & " -n stat"
    No matter what I do soxOut is always blank.

    I've gotten around this by using terminal like this:

    Code:
    tell application "Terminal"
    	close every window
    	activate
    	do script "sox " & quoted form of fullPath & " -n stat"
    	delay 5
    	set soxOut to contents of window 1
    	quit
    end tell
    The problem with this is that it mucks up the workspace, as the processes is supposed to run in the background when a user does something else, and I don't want terminal windows popping up and confusing them.

    Is there anything I can do?
    Thanks!
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    This isn't valid AppleScript; it won't compile. Please copy and paste exactly what's in your code. I can guess what you have, but it's better to be accurate than to guess.


    The way do shell script works is that the standard output of the shell command is stored in the AppleScript variable. So you first need to find out exactly what the sox command is doing.

    Enter the following into a Terminal window, then copy and paste the complete output into a reply post here:
    Code:
    sox YOUR_PATH_HERE -n stat | wc
    
    Replace YOUR_PATH_HERE with the actual pathname of an actual file that 'sox' can operate on.

    If there are error messages in the output, be sure to copy & paste and post them.

    If you post the actual pathname of a usable file, I can post specific commands to copy and paste into a Terminal window. Without an actual pathname, all I can do is give you command with placeholders.
     
  17. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #17
    From the SoX manual :

    stat [−s scale] [−rms] [−freq] [−v] [−d]
    Display time and frequency domain statistical information about the audio. Audio is passed unmodified through the SoX processing chain.
    The information is output to the ‘standard error’ (stderr) stream and is calculated, where n is the duration of the audio in samples, c is the number of audio channels, r is the audio sample rate, and x k represents the PCM value (in the range −1 to +1 by default) of each successive sample in the audio, as follows:

    Example :

    Code:
    set fullPath to "/System/Library/Sounds/Funk.aiff"
    set soxOut to do shell script "/Users/kryten/Downloads/sox-14.4.1/sox " & quoted form of fullPath & " -n stat 2>&1"
     

    Attached Files:

  18. iownafordprobe macrumors newbie

    Joined:
    Oct 25, 2013
    #18
    Wow, thanks so much. That is exactly what I was looking for, you're amazing!
     

Share This Page