开发者

Applescript to change rating of song currently playing in iTunes

开发者 https://www.devze.com 2023-03-07 06:25 出处:网络
I set up an applescript to change the rating of the current song in iTunes and it worked the first few times but now does nothing. Here is the code:

I set up an applescript to change the rating of the current song in iTunes and it worked the first few times but now does nothing. Here is the code:

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 r开发者_如何学JAVAating of theTrack to 100
        end repeat
    end tell
end if

Anyone see any glaring issues in there? FYI, this is attached to a global hotkey, but even opening it in Applescript Editor and hitting "Run" does nothing.


Ack. All I had to was ask to find the answer myself.

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


I don't remember where I found this, but it's a more robust version that might help you. I execute it with a key command using FastScripts. It brings up a dialog box prompting you to rate the current song on a scale of 0 to 5 with a default rating of 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
0

精彩评论

暂无评论...
验证码 换一张
取 消