2017-04-09 4 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ファイルにエクスポートすることもできます。私は今の名前を思い出すことができませんが、それは私の74ギガバイトの音楽の速い作業をしました。 – Chilly

答えて

1

あなたが得る場合は、多くのAppleイベントを保存することができますたとえば、トラックオブジェクトではなくプロパティ値のリスト

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

文字列のリストを解析すると、Appleのイベントはまったく消費されません。ココア(AppleScriptObjC)の助けを借りて

-- 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 

それははるかに速く、おそらくです。 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を使ってこれを助けることを考えていませんでした。あなたが答えの最初の部分で使った短い構文についても知らなかった。ありがとうございました! –

関連する問題