Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BileGhost

macrumors member
Original poster
Jul 29, 2008
80
0
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
 
At the end of which one? -Background is working, it starts the background screensaver fine, it just can't stop it.
 
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.
 
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
 
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.
 
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
 
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
 
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
 
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!
 
Code:
set soxOut do shell script "sox " & quoted form of fullPath & " -n stat"
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.
 
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"
 

Attachments

  • Screen Shot 2013-10-26 at 04.05.27.png
    Screen Shot 2013-10-26 at 04.05.27.png
    140.2 KB · Views: 185
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.