如何通过applescript将iTunes 11设置为随机播放或重复模式 [英] How to set iTunes 11 in shuffle or repeat mode via applescript

查看:45
本文介绍了如何通过applescript将iTunes 11设置为随机播放或重复模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to http://dougscripts.com/ setting shuffle and repeat modes via applescript is broken in iTunes 11.

根据此 stackoverflow 答案 shuffle 现在是独立于播放列表的设置.

According to this stackoverflow answer shuffle is now a playlist independent setting.

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

Thus, I tried to set the shuffle value via the UI, either by the LCD-ish display of iTunes or via the menu bar. All I could get was "unknown UI index" errors when trying to click the shuffle button/menu item, either in the LCD area or the menu bar. (I'm new to applescript).

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

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

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

Ideally, I'd prefer a semantic-based solution over a UI-based solution but I'm not sure if it's possible (iTunes 11 applescript library seems to be outdate since it mention a "shuffle" property for "playlists" items).

推荐答案

我非常喜欢 John Sauer 的方法,我使用他的方法为这些属性编写了一些 getter/setter.它运行良好,因为您不必在使用它们之前激活 iTunes.无论如何,我想我会发布它们,以防它们对任何人有帮助.您将使用类型"(以菜单项名称为模型)获取或设置它们的值,如下所示:

I liked John Sauer's approach so much I wrote myself some getters/setters for these properties using his approach. It's works well because you do not have to activate iTunes before using them. Anyway, I thought I'd post them in case they're of help to anyone. You will get or set their values using the "types" (modeled after the menu item names) as follows:

重复类型为关闭"、全部"或一个".

Repeat types are "Off", "All", or "One".

随机播放类型为关闭"、按歌曲"、按专辑"或按分组"

Shuffle types are "Off", "By Songs", "By Albums", or "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'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

这篇关于如何通过applescript将iTunes 11设置为随机播放或重复模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆