iTunes Shuffle

Hi all, I've tried everything I can to get this to work, but I can't seem to figure it out (especially as I'm not too savvy when it comes to macros). I'm sure this is day 1 simple for most of you, but I'm trying to get it so one keyboard shortcut will toggle shuffle either on or off in iTunes. The kicker is there's not just a menu button that says shuffle that either enables or disables – there's an "On" or "Off" button.

So I've been trying a lot of If/Then statements to no avail. You can see what I currently have below:

Would anyone be able to assist? I'd just like it to turn it on if it's off & off if it's on when I press ⌘ + S. Any help would be greatly appreciated! Thanks!

Hey Aaron,

This sort of task can be quite mystifying until you know how various aspects of Keyboard Maestro work.

As you become more familiar with KM's features figuring out how to do things gets easier.

You have to realize that you can test for a menu condition — and based on that you can branch to selecting Shuffle (ON or OFF).

NOTE — See how I'm using the path to the menu item? This is the fastest way to address it. Otherwise KM has to search for some menu item named “whatever”, and that slows things down.

-Chris


Toggle Shuffle On-Off.kmmacros (6.2 KB)

3 Likes

Thank you so much, Chris! I really tried before asking for help. I truly appreciate the support & the macro!

I prefer this AppleScript method over a menu based solution, because Apple has already at least once changed the menu arrangement of the Shuffle feature. Paste this script in an AppleScript action, works great for me:

tell application "iTunes"
	if shuffle enabled is false then
		set shuffle enabled to true
		say "Shuffle"
	else
		set shuffle enabled to false
		say "Unshuffle"
	end if
end tell

Cheers --Mike

1 Like

or even:

tell application "iTunes"
    set shuffle enabled to not shuffle enabled
    say "Shuffled " & (shuffle enabled as string)
end tell
1 Like

Nice!

Cheers --Mike

2 Likes

Thanks to all of you! These are great. Really appreciate the support.

As a very minor footnote, just in case a macro continues in any way that depends on the outcome of an action, it’s always good to return a value, for example, here:

AppleScript

tell application "iTunes"
    -- EFFECT ----------------------------------------------------------------
    set shuffle enabled to not shuffle enabled
    set newState to shuffle enabled
    say "Shuffled " & newState
    -- VALUE ------------------------------------------------------------------
    newState
end tell

JavaScript

(() => {

    // standardAdditions :: () -> Library Object
    const standardAdditions = () => {
        const a = Application.currentApplication();
        return (a.includeStandardAdditions = true, a);
    };

    const itun = Application('iTunes');
    return (newState => (
        // EFFECT ------------------------------------------------------------
        standardAdditions()
        .say('Shuffled ' + newState),
        // VALUE -------------------------------------------------------------
        newState
    ))(itun.shuffleEnabled = !itun.shuffleEnabled());
})();

Thanks for the script, Mike. I wanted to turn shuffle on or off, depending on the playlist. The select menu option was turning shuffle off for me, but I couldn't get it to turn shuffle back on. The script, which I should have thought of myself, solved it for me. (I removed the "say" part.)

1 Like

Any idea how to get this to work for the newer Music app? I can't seem to get it to work.

1 Like

Hi @Jai_1008, in the script you just have to replace "iTunes" with "Music" and then it works under Big Sur and Catalina.

tell application "Music"
	-- EFFECT ----------------------------------------------------------------
	set shuffle enabled to not shuffle enabled
	set newState to shuffle enabled
	say "Shuffled " & newState
	-- VALUE ------------------------------------------------------------------
	newState
end tell