Applescript改变当前在iTunes中播放的歌曲的评级

时间:2011-05-18 18:22:23

标签: applescript itunes

我设置了一个改变iTunes中当前歌曲评级的AppleScript,它在前几次有效,但现在什么也没做。这是代码:

tell application "System Events"
    if (name of processes) contains "iTunes" then
        set iTunesRunning to true
    else
        set iTunesRunning to false
    end if
end tell

if iTunesRunning then
    tell application "iTunes"
        repeat with theTrack in (get selection)
            set the rating of theTrack to 100
        end repeat
    end tell
end if

有人在那里看到任何明显的问题吗?仅供参考,这是一个全球热键,但即使在Applescript编辑器中打开它并点击“Run”也不会做任何事情。

2 个答案:

答案 0 :(得分:7)

确认。我只需要自己找到答案。

tell application "System Events"
    if (name of processes) contains "iTunes" then
        set iTunesRunning to true
    else
        set iTunesRunning to false
    end if
end tell

if iTunesRunning then
    tell application "iTunes"
        set rating of current track to 100
    end tell
end if

答案 1 :(得分:0)

我不记得我在哪里发现这个,但它是一个更强大的版本,可能会帮助你。我使用FastScripts使用键命令执行它。它会弹出一个对话框,提示您按0到5的等级对当前歌曲进行评级,默认评级为4.

tell application "Finder"
    set frontApp to name of first process whose frontmost is true
end tell
set currTrack to ""
set thisNum to false
tell application "iTunes"
    if player state = playing then
        set currTrack to current track
        set thisName to name of current track
        set thisArt to artist of current track
        set numList to {"0", "1", "2", "3", "4", "5", "", "Rate All"}
        set thisRating to rating of currTrack
        set songArt to ("'" & thisName & "' by " & thisArt)
    end if
end tell
tell application frontApp
    if currTrack = "" then
        beep
        tell application frontApp
            display dialog "There is no song playing in iTunes at this time." buttons "OOPS" default button "OOPS" with icon note
        end tell
    else
        if thisRating ≠ 0 then
            set thisRating to 4
            set thisList to (choose from list numList with prompt "Select a rating for this song:" & return & songArt & return & "Select \"Rate All\" to apply rating to all matching songs." default items thisRating OK button name "Select" with empty selection allowed and multiple selections allowed)
        else
            set thisList to (choose from list numList with prompt "Select a rating for this song:" & return & songArt & return & "Select \"Rate All\" to apply rating to all matching songs." OK button name "Select" with empty selection allowed and multiple selections allowed)
        end if
    end if
end tell

if thisList ≠ false then
    --tell application "iTunes"
    set thisRating to ""
    repeat with i from 1 to count of items of thisList
        set thisItem to item i of thisList
        if thisItem = "Rate All" then
            if thisRating = "" then
                display dialog "You must select a rating to apply to this song." buttons "OK" default button "OK" with icon caution
            else
                tell application "iTunes"
                    set dataList to {name, artist, album, time} of currTrack
                    set addSourceData to {time, size, year, bit rate} of currTrack
                    set matchList to {"Time", "Size", "Year", "Bit Rate"}
                    set matchDef to (choose from list matchList with prompt "You may choose criteria to match in addition to track name, artist, and album." with multiple selections allowed and empty selection allowed)
                    if matchDef = {} then set matchDef to false
                    set trackList to (every track of library playlist 1 whose ((name = item 1 of dataList) and (artist = item 2 of dataList) and (album = item 3 of dataList)))
                    set trackCount to count of items of trackList
                    repeat with j from 1 to trackCount
                        set thisTrack to item j of trackList
                        set notMatch to false
                        if matchDef ≠ false then
                            set addSynchData to {time, size, year, bit rate} of thisTrack
                            repeat with m from 1 to 4
                                if ((m = 1) and (matchDef contains "Time")) or ((m = 2) and (matchDef contains "Size")) or ((m = 3) and (matchDef contains "Year")) or ((m = 4) and (matchDef contains "Bitrate")) then
                                    if item m of addSourceData ≠ item m of addSynchData then
                                        set notMatch to true
                                        exit repeat
                                    end if
                                end if
                            end repeat
                        end if
                        if notMatch = false then
                            set rating of thisTrack to thisRating
                        end if
                    end repeat
                    if thisRating > 0 then
                        set thisRating to thisRating / 20 as integer
                    end if
                end tell
                display dialog "Rating of " & thisRating & " applied to " & trackCount & " song(s)." buttons "OK" default button "OK" giving up after 3
            end if
        else
            set thisRating to thisItem * 20
            tell application "iTunes"
                set rating of currTrack to thisRating
            end tell
        end if
    end repeat
    --end tell
end if