2017-04-09 97 views
0

我試圖從我的圖書館獲取獨特的iTunes藝術家和流派的列表。在某些操作中,AppleScript可能會很慢,在這種情況下,我無法在速度上做出妥協。有沒有進一步的重構,我可以做我的代碼?通過AppleScript獲取獨特的iTunes藝術家列表

tell application "iTunes" 
    -- Get all tracks 
    set all_tracks to shared tracks 

    -- Get all artists 
    set all_artists to {} 
    repeat with i from 1 to count items in all_tracks 
     set current_track to item i of all_tracks 
     set current_artist to genre of current_track 
     if current_artist is not equal to "" and current_artist is not in all_artists then 
      set end of all_artists to current_artist 
     end if 
    end repeat 
    log all_artists 
end tell 

我覺得應該有一個更簡單的方式來獲得從我只是不知道iTunes的藝術家或流派的列表...

+0

你檢查過DougScripts嗎?那裏有很多腳本快速運行。有一個專門用於你想要的東西,如果你願意,可以將它導出到一個txt文件。我現在不記得它的名字,但它使我的74GB音樂的快速工作。 – Chilly

回答

1

您可以保存許多蘋果事件,如果你得到例如屬性值列表而不是跟蹤對象

tell application "iTunes" 
    -- Get all tracks 
    tell shared tracks to set {all_genres, all_artists} to {genre, artist} 
end tell 

解析字符串列表根本不消耗Apple事件。

-- Get all artists 
set uniqueArtists to {} 
repeat with i from 1 to count items in all_artists 
    set currentArtist to item i of all_artists 
    if currentArtist is not equal to "" and currentArtist is not in uniqueArtists then 
     set end of uniqueArtists to currentArtist 
    end if 
end repeat 
log uniqueArtists 

在Cocoa(AppleScriptObjC)的幫助下,它可能要快得多。 NSSet是一個包含唯一對象的集合類型。當從一個數組創建一個集合時,所有的重複項都會被隱式刪除。方法allObjects()將設置轉換回數組。

use framework "Foundation" 

tell application "iTunes" to set all_artists to artist of shared tracks 
set uniqueArtists to (current application's NSSet's setWithArray:all_artists)'s allObjects() as list 
+0

很酷,我沒有想過使用objective-c來解決這個問題。不知道你在答案的第一部分中使用的簡短語法。謝謝! –

相關問題