Script works on run, not on idle

Greetings scripters,

I wrote a little script to make up for not having a pause button on my keyboard. It seems like there’s no way for AppleScript to watch a specific key, but I figured out a workaround. Basically, I turned my Mute key into a pause button by checking the state of the system volume. It works fine in Script Editor (on run), but not as a standalone application (on idle). I’m sure I’m missing something obvious. Here’s the code:

on run
	my pauseButton()
end run

on idle
	my pauseButton()
end idle

on pauseButton()
	tell application "Finder"
		set mutePause to output muted of (get volume settings)
		if mutePause is true then
			tell application "iTunes"
				pause
			end tell
		else
			tell application "iTunes"
				play
			end tell
		end if
	end tell
end pauseButton

Thanks for the help,

marcintosh

Hi,

have you saved your application as Stay Open?

Yes, I saved it as Stay Open with no Startup Screen. I was thinking that it had something to do with telling the script to repeat, but I guess the idle handler takes care of that.

marcintosh

Model: Dual 1.8 GHz G5 Tower
AppleScript: 1.10.7
Browser: Firefox 3.0.7
Operating System: Mac OS X (10.4)

You can detect if the Caps Lock key is up or down.

read this Macosx Hint

you can omit the Finder tell block, volume settings is part of Standard Additions, the Finder is not required.
It’s also recommended to avoid nested application tell blocks

Okay, so I omitted the Finder tell block, still no joy. Then I fiddled with the syntax of the (get volume settings) command, still nothing. Then further testing revealed that the script was working on idle! It was just taking too long. I’d mute the sound and it would take the script up to 30 seconds to catch up (the standard on idle interval from what I understand). I added a simple return 0.5 and that did the trick. Then I cleaned up the code to get rid of the pauseButton subroutine and the mutePause variable. Here’s the code I ended up with:

on idle
	if (get volume settings) is output muted then
		tell application "iTunes"
			pause
		end tell
	else
		tell application "iTunes"
			play
		end tell
	end if
	return 0.5
end idle

Now when someone interrupts me at my desk I just mute my system without losing my place in whatever podcast or music I’m listening to. Thanks for all the help,

marc

It’s me again,

Slight miscalculation… if I pause iTunes by clicking the play/pause button or hitting the spacebar to listen to something else, the script detects that the system isn’t muted and starts playing. So by enabling the pausing with a keystroke I inadvertently disabled pausing all together. D’oh! Modded the script to make it pause only, which still lets me be interrupted at my desk without missing anything. Here’s the code:

on idle
	if (get volume settings) is output muted then
		tell application "iTunes"
			pause
		end tell
	end if
	return 0.5
end idle

marcintosh

I can understand not wanting to hear this after all the effort you’ve made on the script (I’m guilty of that myself), but there’s a utility that exists to do this and more. It’s called Breakaway, and its main purpose is to pause iTunes when you unplug your headphones. However, it works equally well at pausing when you mute the keyboard, and you can disable the headphone functionality if you want.

I hope this helps!

On my G4 12’’ powerbook, I use the F3 key to play/pause iTunes using the free Keyboard Hotkey app Spark

Spark biggest feature is, it has a Applescript plugin so you can paste or write code within it and then set a hotkey to run the script.

But it also has a iTunes Plugin, so you do not even have to code for the play/pause, you just set your hotkey, in my case the F3 button. Which still works as a mute key if I hold down the fn key +f3.

Breakaway duplicates my script’s functionality and Spark does exactly what I want to do in the first place; pause/play iTunes with a keystroke just like my iMac at home. My script was really a clever workaround for not being able to watch any key on the keyboard with AppleScript, but muting the system was not part of my plan. However, this is my work machine and they’re kind of finicky about installing third-party apps. AppleScript gets the job done “under the radar” so to speak. Thanks for the tips guys, at the very least I like to be well informed.

marcintosh