如何通过applescript在随机播放或重复模式下设置iTunes 11

时间:2013-02-03 16:16:06

标签: applescript itunes repeat shuffle

根据http://dougscripts.com/设置,在iTunes 11中打破了随机播放的重复模式。

根据此stackoverflow answer shuffle现在是一个独立于播放列表的设置。

因此,我尝试通过UI设置随机播放值,可以通过iTunes的LCD-ish显示或通过菜单栏。当我尝试在LCD区域或菜单栏中单击随机按钮/菜单项时,我得到的只是“未知的UI索引”错误。 (我是AppleScript的新手。)

如果你们中的一些人想出办法在iTunes 11 上切换随机播放模式,那就太棒了。另外我更喜欢基于菜单栏而不是LCD显示器的解决方案,因为后者的随机播放按钮并不总是可见。

理想情况下,我更喜欢基于语义的解决方案而不是基于UI的解决方案,但我不确定它是否可行(iTunes 11 applescript库似乎已经过时,因为它提到了“ shuffle“属性”播放列表“项目”。

6 个答案:

答案 0 :(得分:3)

当我看到iTunes应用程序的AppleScript属性current playlist时,我很乐观,但它不能正常工作。它可以获取和设置当前播放列表的名称,但它既不能用于属性shuffle,也不能用于song repeat。尝试设置任一属性时出错,并且shuffle总是返回'false',song repeat返回'off'。

我认为您唯一的选择是UI脚本。以下是如何通过菜单栏切换随机播放:

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

以下是设置重复的方法:

tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s 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

答案 1 :(得分:3)

我非常喜欢John Sauer的方法,我用自己的方法为自己写了一些吸气剂/固定剂。它运行良好,因为您在使用iTunes之前无需激活iTunes。无论如何,我以为我会发布它们以防万一他们对任何人都有帮助。您将使用“类型”(在菜单项名称后建模)获取或设置其值,如下所示:

重复类型为“关闭”,“全部”或“一个”。

随机播放类型为“关闭”,“按歌曲”,“按专辑”或“按分组”

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's menu bar item "Controls"'s menu 1's menu item "Repeat"'s 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"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s 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's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s 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"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s 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"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s 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"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s 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"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with "Groupings")
        end pressByGroupings
    end script

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

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

答案 2 :(得分:2)

对于iTu​​nes 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更改为SongsGroupingsOnOff

答案 3 :(得分:1)

这是另一种方法:

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

答案 4 :(得分: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

那个人通过Dock切换了洗牌。您可以在使用时观看Dock菜单的动画。

这一个通过菜单切换shuffle,无形地:

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处于后台,这两种方法都能正常工作。

答案 5 :(得分:0)

花了一些时间来解构这篇文章中的所有混淆解决方案(这似乎不再适用),这是一个更易读和可自定义的方法,适用于iTunes 12.1:

tell application "System Events"

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

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

    tell process "iTunes"
        click shuffleOnMenuItem
        click shuffleSongsMenuItem
    end tell

end tell

这将打开随机播放并将其设置为随机播放歌曲而不是专辑,并且如何更改它以执行其他操作应该非常明显。