7

根据http://dougscripts.com/通过 applescript 设置随机播放和重复模式在 iTunes 11 中被破坏。

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

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

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

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

4

7 回答 7

6

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

重复可以设置为一个关闭全部

tell application "Music"
   set song repeat to off
end

Shuffle 可以设置为truefalse

tell application "Music"
   set shuffle enabled to true
end

您可以在Dougscripts找到更多详细信息。

于 2017-05-12T16:00:19.507 回答
3

当我看到current playlistiTunes 应用程序的 AppleScript 属性时,我很乐观,但它并不能很好地工作。它能够获取和设置当前播放列表的名称,但它不能对属性shufflesong repeat. 尝试设置任一属性时都会出错,并且总是返回 'false'shuffle和 'off' song repeat

我认为您唯一的选择是 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
于 2013-02-03T18:12:56.933 回答
3

我非常喜欢 John Sauer 的方法,我使用他的方法为这些属性编写了一些 getter/setter。它运作良好,因为您不必在使用 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
于 2013-02-04T01:38:13.783 回答
2

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

相应地更改AlbumsSongsGroupings和。OnOff

于 2015-05-17T19:28:22.583 回答
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
于 2013-02-03T19:03:35.747 回答
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 菜单动画。

这个通过菜单无形地切换随机播放:

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 在后台,这两种方法也可以工作。

于 2015-03-15T09:02:47.107 回答
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

这将打开随机播放并将其设置为随机播放歌曲而不是专辑,并且应该很明显如何将其更改为做其他事情。

于 2015-06-29T00:02:12.307 回答