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

如何通过applescript将iTunes 11设置为随机播放或重复播放模式,applescript,itunes,repeat,shuffle,Applescript,Itunes,Repeat,Shuffle,根据iTunes 11中的设置,通过applescript的洗牌和重复模式被破坏 根据这种说法,洗牌现在是一种与播放列表无关的设置 因此,我尝试通过用户界面(通过iTunes的液晶显示屏或菜单栏)设置洗牌值。我所能得到的只是在尝试单击LCD区域或菜单栏中的随机播放按钮/菜单项时出现的“未知UI索引”错误。(我对applescript不熟悉) 如果你们中的一些人能想出一种在iTunes11上切换洗牌模式的方法,那就太好了。另外,我更喜欢基于菜单栏的解决方案,而不是基于LCD显示屏的解决方案,因为在

根据iTunes 11中的设置,通过applescript的洗牌和重复模式被破坏

根据这种说法,洗牌现在是一种与播放列表无关的设置

因此,我尝试通过用户界面(通过iTunes的液晶显示屏或菜单栏)设置洗牌值。我所能得到的只是在尝试单击LCD区域或菜单栏中的随机播放按钮/菜单项时出现的“未知UI索引”错误。(我对applescript不熟悉)

如果你们中的一些人能想出一种在iTunes11上切换洗牌模式的方法,那就太好了。另外,我更喜欢基于菜单栏的解决方案,而不是基于LCD显示屏的解决方案,因为在后者中,洗牌按钮并不总是可见的


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

当我看到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

以下是另一种方法:

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

我非常喜欢John Sauer的方法,我使用他的方法为自己编写了一些这些属性的getter/setter。它工作得很好,因为在使用iTunes之前,您不必激活iTunes。不管怎样,我想我应该把它们贴出来,以防它们对任何人都有帮助。您将使用“类型”(根据菜单项名称建模)获取或设置其值,如下所示:

重复类型为“关闭”、“全部”或“一”

洗牌类型有“关闭”、“按歌曲”、“按专辑”或“按分组”


在最新的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菜单时查看其动画

这一个通过菜单切换随机播放,不可见:

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处于后台,这两个功能也可以使用。

对于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

相册
更改为
歌曲
分组
打开
关闭

花了一些时间来解构这篇文章中所有模糊的解决方案(这些解决方案似乎不再有效),下面是一种更具可读性和可定制的方法,适用于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

这将打开shuffle,并将其设置为shuffle歌曲而不是专辑,并且很明显如何将其更改为执行其他操作。

对于新的音乐应用程序,这是有效的。如果您仍在使用iTunes,请将“音乐”更改为“iTunes”

重复可设置为oneoffall

tell application "Music"
   set song repeat to off
end
tell application "Music"
   set shuffle enabled to true
end
洗牌可以设置为truefalse

tell application "Music"
   set song repeat to off
end
tell application "Music"
   set shuffle enabled to true
end

有关详细信息,请访问。

Nice。很快将用英语启动我的iTunes并进行测试:)我只是用一个简单的setShuffleType(“相册”)尝试了一下,但我刚刚收到一条错误消息“系统事件出错:无法获取菜单项1的菜单项1,菜单项1的菜单项“Shuffle”,菜单项1的菜单栏项“Controls”,进程“iTunes”的菜单栏1,其名称以“Shuffle”结尾。索引无效。”在toggleShuffleOnOff函数中。iTunes 12.1。怎么了?在设置“随机播放”或“重复类型”之前是否需要激活iTunes?您回答中的后一个脚本在iTunes 12.2上对我来说非常有效,它是由一个带有的键盘键触发的。注意:这在iTunes 10中可以正常工作,但在iTunes 11中似乎不起作用