How to set iTunes 11 in shuffle or repeat mode using applescript

According to http://dougscripts.com/, setting the shuffle and repeat mode through applescript is broken in iTunes 11.

In line with this, https://stackoverflow.com/a/412832/2326 is now a playlist-independent parameter.

Thus, I tried to set the shuffle value through the user interface, either on the iTunes LCD or through the menu bar. All I could get was β€œunknown UI index errors” when I tried to click a button / menu item in random order, either in the LCD area or in the menu bar. (I'm new to applescript).

If some of you might come up with a way to switch the shuffle mode on iTunes 11 , that would be great. I would also prefer a solution based on the menu bar rather than the LCD , since the shuffle button is not always visible in the latter.

Ideally, I would prefer a semantic solution over a UI based solution , but I'm not sure if this is possible (the Apple iTunes 11 application library seems outdated because it mentions β€œshuffle” for playlist elements).

+5
source share
7 answers

, / , . , iTunes . , , , . "" ( ) :

: ".", "" "".

: "Off", "By Songs", "By Albums" "By Groupings"

on getRepeatType() -- the return value is a string: Off/All/One
    tell application "System Events"
        tell process "iTunes"
            set menuItems to menu items of menu bar 1 menu bar item "Controls" menu 1 menu item "Repeat" menu 1
            set currentChoice to "unknown"
            repeat with anItem in menuItems
                try
                    set theResult to value of attribute "AXMenuItemMarkChar" of anItem
                    if theResult is not "" then
                        set currentChoice to name of anItem
                        exit repeat
                    end if
                end try
            end repeat
        end tell
    end tell
    return currentChoice
end getRepeatType

on setRepeatType(repeatType) -- repeatType is a string: Off/All/One
    set currentValue to my getRepeatType()
    ignoring case
        if currentValue is not repeatType then
            tell application "System Events" to tell process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Repeat" menu 1
                if repeatType is "all" then
                    perform action "AXPress" of menu item "All"
                else if repeatType is "one" then
                    perform action "AXPress" of menu item "One"
                else
                    perform action "AXPress" of menu item "Off"
                end if
            end tell
        end if
    end ignoring
end setRepeatType

on getShuffleType() -- the return value is a string: Off/By Songs/By Albums/By Groupings
    tell application "System Events"
        tell process "iTunes"
            set menuItems to menu items of menu bar 1 menu bar item "Controls" menu 1 menu item "Shuffle" menu 1
            set onOffItemName to name of item 1 of menuItems
        end tell
    end tell

    -- is shuffle off
    ignoring case
        if onOffItemName contains " on " then return "Off"
    end ignoring

    -- shuffle is on so find how we are shuffling
    set currentChoice to "Unknown"
    tell application "System Events"
        tell process "iTunes"
            repeat with i from 2 to count of menuItems
                set anItem to item i of menuItems
                try
                    set theResult to value of attribute "AXMenuItemMarkChar" of anItem
                    if theResult is not "" then
                        set currentChoice to name of anItem
                        exit repeat
                    end if
                end try
            end repeat
        end tell
    end tell
    return currentChoice
end getShuffleType

on setShuffleType(shuffleType) -- shuffleType is a string:  Off/By Songs/By Albums/By Groupings
    set currentValue to my getShuffleType()

    script subs
        on toggleShuffleOnOff()
            tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Shuffle" menu 1 whose name ends with "Shuffle")
        end toggleShuffleOnOff

        on pressBySongs()
            tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Shuffle" menu 1 whose name ends with "Songs")
        end pressBySongs

        on pressByAlbums()
            tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Shuffle" menu 1 whose name ends with "Albums")
        end pressByAlbums

        on pressByGroupings()
            tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Shuffle" menu 1 whose name ends with "Groupings")
        end pressByGroupings
    end script

    ignoring case
        if shuffleType contains "off" then -- we have to make sure it off
            if currentValue does not contain "off" then subs toggleShuffleOnOff()
        else
            -- make sure it on
            if currentValue contains "off" then subs toggleShuffleOnOff()

            -- select the shuffle menu item for the type
            if shuffleType contains "song" and currentValue does not contain "song" then
                subs pressBySongs()
            else if shuffleType contains "album" and currentValue does not contain "album" then
                subs pressByAlbums()
            else if shuffleType contains "group" and currentValue does not contain "group" then
                subs pressByGroupings()
            end if
        end if
    end ignoring
end setShuffleType
+3

, AppleScript current playlist iTunes, . , shuffle, song repeat. , "false" shuffle "off" song repeat.

, - UI Scripting. , :

tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Shuffle" menu 1 whose name ends with "Shuffle")

:

tell application "System Events" to tell process "iTunes" menu bar 1 menu bar item "Controls" menu 1 menu item "Repeat" menu 1
    perform action "AXPress" of menu item "Off"
    perform action "AXPress" of menu item "All"
    perform action "AXPress" of menu item "One"
end tell
+3

iTunes 12.6 :

. . .

tell application "iTunes"
set song repeat to off
end

Shuffle True False.

tell application "iTunes"
set shuffle enabled to true
end

Dougscripts

+3

iTunes 12

tell application "System Events"  
    tell process "iTunes" to if exists then  
        click menu item "Albums" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1  
    end if  
end tell

Albums Songs, Groupings, On Off .

+2

:

activate application "iTunes"
tell application "System Events"
    tell process "iTunes"
        click menu item 1 of menu 1 of menu item "Shuffle" of menu 1 of menu bar item "Controls" of menu bar 1
    end tell
end tell
+1

, iTunes. :

tell application "System Events" to tell UI element "iTunes" of list 1 of process "Dock"
    if not (exists) then return
    perform action "AXShowMenu"
    click menu item "Shuffle" of menu 1
end tell

-. "", .

, :

tell application "System Events"
    tell application process "iTunes"
        tell menu 1 of menu item "Shuffle" of menu "Controls" of menu bar 1
            if (value of attribute "AXMenuItemMarkChar" of item 1 of menu items as string) = "βœ“" then
                click menu item 2
            else
                click menu item 1
            end if
        end tell
    end tell
end tell

iTunes .

+1

Some time passed, deconstructed all the confusing solutions in this post (which seem to no longer work), here is a more readable and customizable approach that works with iTunes 12.1:

tell application "System Events"

    set itunesMenuBar to process "iTunes" first menu bar
    set controlsMenu to itunesMenuBar menu bar item "Controls" first menu
    set shuffleMenu to controlsMenu menu item "Shuffle" first menu

    set shuffleOnMenuItem to shuffleMenu menu item "On"
    set shuffleSongsMenuItem to shuffleMenu menu item "Songs"

    tell process "iTunes"
        click shuffleOnMenuItem
        click shuffleSongsMenuItem
    end tell

end tell

This will result in shuffling and setting up shuffling songs instead of albums, and it should be pretty obvious how to change it to do other things.

0
source

All Articles