按艺术家计算的iTunes歌曲数量,名称以字母开头

时间:2015-04-05 19:51:31

标签: macos applescript itunes

我是Applescript的新手。我想要一个脚本,列出艺术家和该艺术家文件夹中的歌曲数量。我想为名字以A开头的艺术家做这件事。当我准备好之后,我会得到名字以B开头的艺术家名单,依此类推。我确实找到了这篇文章:"什么是iOS中检索特定艺术家歌曲数量的最快方式?"也许这个脚本可以工作,但我不知道如何修改这一行" if(artistName!= nil)"得到我想要的东西。此外,我不知道信息的存储位置,因此我可以检索它" //存储新的计数 [artists setObject:[NSNumber numberWithInt:numSongs] forKey:artistName];哦,我不使用iOS我将使用osx。也许我可以修改我找到的这个脚本。它获得了艺术家的专辑数量。

MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
NSArray *albumCollection = [albumQuery collections];
NSCountedSet *artistAlbumCounter = [NSCountedSet set];
[albumCollection enumerateObjectsUsingBlock:^(MPMediaItemCollection  *album, NSUInteger idx, BOOL *stop) {
NSString *artistName = [[album representativeItem] valueForProperty:MPMediaItemPropertyArtist];
[artistAlbumCounter addObject:artistName];
}];
NSLog(@"Artist Album Counted Set: %@", artistAlbumCounter);

感谢您提供的任何帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

查看iOS代码和ObjectiveC是没有意义的,以便弄清楚你应该用Applescript做什么!无论如何,这就是你想要的。

tell application "iTunes"
    # Get playlist currently selected 
    set myPlayList to view of window 1

    set s to (every track in myPlayList whose artist begins with "g")
    repeat with t in s
        log name of t
    end repeat
    log (count of s)
end tell

答案 1 :(得分:1)

这个使用selected playlist(或者,如果因某种原因失败,则使用整个库)并从A转到Z。用您的代码替换log部分。要查看其工作原理,请确保在Script-Editor中显示Log并获得更好的视图,然后选择Messages 标签。只处理file tracks

tell application "iTunes"
    try
        set selectedPlayList to view of window 1
    on error
        beep
        set selectedPlayList to (container of browser window 1) -- whole library (I think)
    end try
end tell

set totalItems to 0

repeat with i from (id of "A") to (id of "Z")

    set thisLetter to (character id i)

    log "-----------------------------------------------------------"

    tell application "iTunes"
        try

            set currentItems to (file tracks in selectedPlayList whose artist begins with thisLetter)
            set countItems to number of items in currentItems
            set totalItems to totalItems + countItems

            set s to "s"
            if countItems = 1 then set s to ""
            log (countItems as text) & " item" & s & " for artists starting with the letter " & quoted form of thisLetter
            log "-----------------------------------------------------------"

            repeat with i from 1 to countItems
                set thisItem to item i of currentItems

                tell thisItem -- this is like "tell file track x". Shortens the code because we can use "artist" instead of "artist of thisItem"

                    log (i as text) & ". " & quoted form of (get artist) & " | " & quoted form of (get name) & "    [ " & time & " ] "

                end tell

            end repeat


        on error the error_message number the error_number
            beep
            display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
            return
        end try

    end tell

end repeat


log "-----------------------------------------------------------"
log "Items: " & totalItems as text