Jordan Smith
Jordan Smith

Reputation: 10378

Apple Script: How do I get a reference to the next track in iTunes?

What I'm trying to achieve is this:

tell application "iTunes"
    play next track with once
end tell

Of course, 'next track' is a command, and not a reference to the next track - so this doesn't work. I've been through the documentation and am really stuck on this - how would I code this so that it works?

If the solution involves making a playlist or anything that's fine - all I need is a script that plays the next song and then stops.

-

Any help is much appreciated :) I've already wasted a lot of time trying to get this to work!

Upvotes: 0

Views: 1279

Answers (2)

Jordan Smith
Jordan Smith

Reputation: 10378

After much messing around, this is how I'm doing it. If you can see a better way then I'd like to know!

tell application "iTunes"

    set is_playing to false

    if player state is playing then
        set is_playing to true
    end if

    tell playlist "Cinderella"

        set track_number to 1
        set should_repeat to true
        repeat while should_repeat is true

            if track track_number exists then
                tell track track_number
                    set play_count to (get played count)
                    if play_count is 0 then
                        set should_repeat to false
                    else
                        set track_number to track_number + 1
                    end if
                end tell
            else
                repeat with track_ref in tracks
                    tell track_ref
                        set played count to 0

                    end tell
                end repeat
                set track_number to 1
            end if
        end repeat

        if is_playing is true then
            set played count of track track_number to 1
            tell application "iTunes"
                stop
            end tell
        else
            play track track_number with once
        end if

    end tell

end tell

Upvotes: 0

fireshadow52
fireshadow52

Reputation: 6516

This script is hacky but it works...

tell application "iTunes"
    next track
    set this_track to the name of the current track
    previous track
end tell

Upvotes: 1

Related Questions