iTunes QuicKey: Toggle Shuffle

I usually just play tracks from within my library, not make playlists. Sometimes I want to shuffle them up, sometimes I don’t.
I can’t figure out why this won’t work. I want to assign a QuicKey to toggle Shuffle off and on…If Shuffle is on, turn it off and Vice Versa.

tell application "iTunes"
	if shuffle of library playlist is true then
		set shuffle of library playlist to false
	else
		set shuffle of library playlist to true
	end if
end tell

Thanks - Slimjim5811

slim:

Since [shuffle] is a property of a playlist, you must identify which playlist you want to set the shuffle to true or false:

tell application "iTunes"
	if shuffle of first library playlist is true then
		set shuffle of first library playlist to false
	else
		set shuffle of first library playlist to true
	end if
end tell

This is your script, modified to set the shuffle of the first library playlist, which is the library, as you indicated that you typically use.

Hope this helps.

This does the same thing:

tell application "iTunes"
	get ((((shuffle of first library playlist) as integer) + 1) mod 2) as boolean
	set shuffle of first library playlist to result
end tell

Smooth logic, Bruce, I like that use of mod. I’ll have to remember that.

Here’s another one:

tell application “iTunes”
activate
tell first library playlist to set shuffle to not shuffle
end tell

I thought I might be missing something. Thanks kel. :cool:

Thanks to everyone for your help. I went Bruce’s route and then realized that I needed some visual feedback to let me know if Shuffle is Off or On (since the whole point of this is to not have to be in iTunes to toggle Shuffle). Here’s what I have…

tell application "iTunes"
	get ((((shuffle of first library playlist) as integer) + 1) mod 2) as boolean
	set ShuffleAnswer to result
	set shuffle of first library playlist to ShuffleAnswer
	if ShuffleAnswer is equal to false then
		display dialog "       Shuffle Off" buttons ("Sweet.") with icon 1 giving up after 3 ¬
			default button "Sweet."
	else
		display dialog "       Shuffle On" buttons ("Sweet.") with icon 1 giving up after 3 ¬
			default button "Sweet."
	end if
end tell


The only question I have…is there any way to display the iTunes icon within the Display Dialog instead of icon 1?
I found this thread on creating custom icons: http://bbs.applescript.net/viewtopic.php?id=5201

But doesn’t the iTunes icon already exist somewhere that can be used? I can’t find where.

Thanks - Slimjim5811

Hi,

You can create cicn resources with this freeware:

http://www.versiontracker.com/dyn/moreinfo/macosx/23349

gl,

This is an excellent thread and let me first thank you guys for answering the ‘How the heck did they DO that?!’ question about that toggle shuffle behavior.

Not to threadjack, but - I’m trying to do this, and all of the other iTunes behaviors (toggle play / pause) in a series of scripts that will work with netTunes from Shirt Pocket software, in order to control iTunes running on another machine (running a netTunes server instance) from multiple ‘client’ machines.

I’m implementing the excellent toggle shuffle script listed above but I wondered if anyone might suggest a way to improve on the ‘dialog’ display suggested above by slimjim5811… to display a Quartz-type notification on the screen when shuffle is turned on and off.

If you’re interested in seeing exactly what I mean, I suggest downloading the excellent app SizzlingKeys from YellowMug software. It’s free, with the exception of a few ‘Pro’ features – like (ironically enough!) the Toggle Shuffle command. Heh. Okay, so if you don’t want to pay $5 to see what I mean then try the ‘Volume Up’ and ‘Volume Down’ hotkeys instead. They show the little grey transparent rounded rectangle on the screen, with the white volume bar symbol in it. The toggle shuffle just has the intertwined arrows instead.

So, anyone know how I could replicate this?

      • Nevermind, I just answered my own question, heh. Here’s how:

Step 1: I downloaded Growl and installed the prefPane. I picked the ‘Bezel’ notification style.

Step 2: I installed the ‘growlnotify’ shell script from the Extras folder. But that’s just for purposes of this example, you’ll probably want to use (duh) AppleScript, and that’s left as an exercise for the reader.

Step 3: I ransacked SizzlingKeys for it’s Shuffle state images (Apologies to Jay Teo!) by going to /Applications/Utilities/SizzlingKeys4iTunes.app and doing ‘Show Package Contents’, then navigating to Contents/Resources and grabbing the files shuffle_on.png and shuffle_off.png. I put them in a personal projects directory.

Step 4:

> echo "Shuffle On" | growlnotify --image /Users/thom/Projects/personal/home_AV/netTunes_applescript_SizzlingKeys/shuffle_on.png

> echo "Shuffle Off" | growlnotify --image /Users/thom/Projects/personal/home_AV/netTunes_applescript_SizzlingKeys/shuffle_off.png

…et voila, you have a working quartz-like notification.

So, as you can see, I’m trying to have my scripts replace as much of the functionality of SizzlingKeys as possible, because I need them to do double duty: BOTH control the local iTunes instance… and the remote iTunes instance via netTunes. (Which one they do would depend on a boolean I’d set somewhere and toggle on/off with another hotkey.)

Speaking of which… anyone have any experience setting custom user defaults via Applescript? I’m thinking of com.unithom.iTunesRemoteLocalControl as my ‘defaults’ path, to make it unique.

Thanks! Hope this is helpful.

Okay, I lied. Here.


tell application "iTunes"
	activate
	set foo to not shuffle of first library playlist
	
	tell first library playlist to set shuffle to foo
end tell

tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)

if GrowlRunning ≥ 1 then
	
	set appName to "iTunes Toggle Shuffle"
	set notificationName to "toggleShuffle"
	set notifs to {notificationName}
	tell application "GrowlHelperApp"
		register as application appName all notifications notifs default notifications notifs icon of application "iTunes"
	end tell
	
	set theDescription to "Shuffle "
	if (foo) then
		set theDescription to theDescription & "On"
		set fileBit to "on"
	else
		set theDescription to theDescription & "Off"
		set fileBit to "off"
	end if
	
	set theFileName to "file:///Users/thom/Projects/personal/home_AV/netTunes_applescript_SizzlingKeys/shuffle_" & fileBit & ".png"
	
	tell application "GrowlHelperApp"
		notify with name notificationName title "Shuffle State" application name appName description theDescription image from location theFileName
	end tell
	
end if


I saved this file as ‘Toggle Shuffle.scpt’ in the /Library/iTunes/Scripts folder. It is accessible from within iTunes, and works fine when I pick it from the Script menu, but when I assigned it a keyboard shortcut (F6) from the Keyboard & Mouse prefPane, ‘Keyboard shortcuts’ tab, nothing happens except I see the script menu flash briefly. Bummer. I tried adding a modifier (shift key) and again, it shows the proper cmd label next to the script name, and briefly flashes the menu, but does not execute when I hit the hotkey;

I got pretty close though! Just wish I knew why the keyboard shortcut didn’t work. Anyone have any ideas?

OK, so I want to toggle shuffle of the playlist that’s playing. How to I specify the current playlist? I tried using “current playlist”, which didn’t produce an error, but didn’t work either. I also tried to get the index of the current playlist, but applescript doesn’t seem to recognize the phrase “current playlist”.

I’m sure it’s something really easy, but I haven’t found it yet. Please help!

Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)

‘current playlist’ is the playlist that contains a playing track. If no track is playing, then it errors.

gl,

Applescript properties keep their value so you don’t really need to keep preferences.

If you want to, you could use the Properties List Tools scripting addition.

http://www.latenightsw.com/freeware/PListTools/index.html

You could also

do shell script “defaults com.unithom.iTunesRemoteLocalControl write …”
set theResults to do shell script “defaults com.unithom.iTunesRemoteLocalControl read …”

And then parse the text you get back.

This is what I ended up with for the reading/writing:

set theResponse to do shell script "defaults read com.unithom.iTunesRemoteLocalControl isRemote"

if theResponse contains "does not exist" then
	-- initialize it with this statement
	set theState to 1
else if theResponse contains "1" or theResponse contains "0" then
	set theState to (((theResponse as integer) + 1) mod 2)
	
end if

set theCmd to "defaults write com.unithom.iTunesRemoteLocalControl isRemote " & theState

do shell script theCmd