Play random tracks from playlist

Hi found out about this macro recently and am enjoying using it to start some focus music playlists. I want it to start the full playlist on shuffle not just one song. It does this perfectly on imac, but on my macbook it only plays one song. Why is this happening and is there any way to make it always start shuffling the whole playlist rather than just one song?

1 Like

You will need to post the macro in order for us to help.
See

2 Likes

Thanks Michael. It's just using this default action, "Play random tracks from playlist", from Keyboard Maestro:

1 Like

This macro just started act weird on my imac in another way- it plays the first song of the playlist specified, but then it will (seemingly randomly) shuffle every song in my library (including songs not on the playlist).

This just started happening yesterday, it never did this before.

Anybody have any experience with this action? Am i doing something wrong? Or maybe its just buggy?

This action is literally just a wrapper around the AppleScript:

tell application "iTunes" to tell first source to tell playlist "1 focus" to play some track

The AppleScript is run through osascript as usual.

I just tried it on my Mac with a playlist with 10 items in it, and it worked every time, choosing a random song in an unpredictable pattern (including re-choosing the same song occasional).

If that is not working reliably for you then I’m not sure what might be going wrong unless either there is multiple copies of iTunes, or something is messing around with AppleScript or iTunes.

1 Like

Thank you! I think i got just the behavior i was looking for by modifying the applescript!

That's great! Would you mind posting your final solution so others can benefit?

2 Likes

Of course! The applescript is:

tell application "iTunes"
play user playlist "1 focus"
end tell

Curious. Presumably that does not play a random track though. I expect for you:

tell app "iTunes" to tell user playlist "1 focus" to play some track

would likely work well.

Which begs the question - what does iTunes consider a “source”, as that seems to be the obvious difference between your AppleScript and Keyboard Maestro’s.

For me, the first source is the library, then Internet Radio (?!), then the store, then my iOS devices.

Anyway, I’ve simplified the AppleScript in Keyboard Maestro to:

tell app "iTunes" to play track "whatever"
tell app "iTunes" to tell playlist "whatever" to play first track
tell app "iTunes" to play some track
tell app "iTunes" to tell playlist "whatever" to play some track

So hopefully that will resolve the issue for anyone else going forward.

Thanks.

1 Like

You are right, i didn’t realize it but the script i was using always started on the first track and did not shuffle the playlist. So i’m still trying to figure this out now.

When i use the one you recommended:“tell app “iTunes” to tell user playlist “1 focus” to play some track”, it only plays one random track from the playlist. I want to it play the whole playlist on shuffle, so that i dont have to hit the keyboard shortcut after every song.

I tried changing the script to “some tracks”, but that doesnt work. I tried using the action built into Keyboard Maestro again without success. Also tried pasting the 4 line applescript into Keyboard Maestro manually.

Do you know how to achieve what i’m looking for?

Thanks, Dan

Try:

tell application "iTunes"
	set song repeat to off
	set shuffle enabled to true
	play playlist "My Playlist"
end tell
1 Like

Or, from the department of general over-engineering (in JavaScript for Automation):

(Random track either from random playlist, or from playlist specified by (case-insensitive) name.

(function () {
    'use strict';


    // GENERIC FUNCTIONS -------------------------------------------------------

    // elemIndex :: Eq a => a -> [a] -> Maybe Int
    function elemIndex(x, xs) {
        var i = xs.indexOf(x);
        return {
            nothing: i === -1,
            just: i
        };
    };

    // toLower :: Text -> Text
    function toLower(s) {
        return s.toLowerCase();
    };

    // ITUNES ----------------------------------------------------------------

    // randomPlaylist :: () -> Playlist
    function randomPlaylist() {
        var ps = Application("iTunes")
            .playlists;
        return ps.at(Math.floor(Math.random() * (ps.length - 1)));
    };

    // playListByName :: String -> Maybe Playlist
    function playListByName(name) {
        var app = Application('iTunes'),
            mIndex = elemIndex(toLower(name), app.playlists.name()
                .map(toLower));
        return mIndex.nothing ? {
            nothing: true,
            msg: 'Playlist not found as spelled: "' + name + '"'
        } : {
            just: app.playlists.at(mIndex.just)
        };
    };

    // randomTrackFromPlaylist :: Playlist -> ITunes ()
    function randomTrack(maybePlaylist) {
        return maybePlaylist.nothing ? maybePlaylist : function () {
            var app = Application('iTunes');
            app.songRepeat = 'off';
            app.shuffleEnabled = true;
            app.play(maybePlaylist.just);
            return {
                just: app.currentTrack.name()
            };
        }();
    };

    // EITHER BY PLAYLIST NAME (CASE-INSENSITIVE) ----------------------------

    return randomTrack(playListByName('top 25 most played'));


    // OR JUST A RANDOM TRACK FROM A RANDOM PLAYLIST -------------------------

    return randomTrack({
        just: randomPlaylist()
    });
})();

1 Like

I know this is an old post, but I was just searching for an AppleScript that would play a named Playlist in shuffle and this came up in the Google search.
I'd like to report that in 2022 this simple script still works. (Change "iTunes" to "Music", natch.)
I'm brand new to KM, but super-excited that I've made three macros that were incredibly easy and solved long standing problems.
Cheers.

3 Likes