2017-05-02 3 views
0

標準のiTunesのAppleScriptを使用する代わりに、このAppleScriptをiTunes Library frameworkを使用するように変換できますか?新しいiTunesライブラリフレームワークを使用するためのiTunesのAppleScriptの変換

私の現在のスクリプトがユーザーのiTunesライブラリを読み込み、TrackName、TrackLocationとPersistentIdからなるファイルを作成し、それが確実に動作しますが、遅くなることができ、ユーザは大iTunesライブラリを持っている

tell application "iTunes" 
    with timeout of 2400 seconds 
     if (count of every file track of library playlist 1) is equal to 0 then 
      set thePath to (POSIX file "/tmp/songkong_itunes_model.new") 
      set fileref to open for access (thePath) with write permission 
      set eof fileref to 0 
      close access fileref 
      return 
     end if 

     tell every file track of library playlist 1 
      script performancekludge 
       property tracknames : its name 
       property locs : its location 
       property persistids : its persistent ID 
      end script 
     end tell 
    end timeout 
end tell 

set thePath to (POSIX file "/tmp/songkong_itunes_model.new") 
set fileref to open for access (thePath) with write permission 
set eof fileref to 0 

tell performancekludge 
    repeat with i from 1 to length of its tracknames 
     try 
      set nextline to item i of its tracknames ¬ 
       & "::" & POSIX path of item i of its locs ¬ 
       & "::" & item i of its persistids 
      write nextline & linefeed as «class utf8» to fileref 
     end try 
    end repeat 
end tell 
close access fileref 

異なる大きなはこれです新しいフレームワークではiTunesが実際に動作する必要はなく、かなり高速になるはずです。しかし、Sparse命令はObjectiveCについてしか解説していません。私はAppleScriptでバージョンを書く方法が明確ではありません(またはJavaではさらに良い)

答えて

0

これはiTunesLibraryフレームワークを使用したAppleScriptObjcの同等機能です。行ごとにテキストを書き込むのではなく、配列を作成し、新しい行の文字を挿入し、ファイル全体を一度に書き込みます。また、ファイルを書き込むときのエラー処理も優れています。

use AppleScript version "2.4" -- Yosemite (10.10) or later 
use scripting additions 

use framework "iTunesLibrary" 

set {library, theError} to current application's ITLibrary's libraryWithAPIVersion:"1.0" |error|:(reference) 
if theError is not missing value then 
    display dialog theError's localizedDescription() as text buttons {"Cancel"} default button 1 
end if 
set tracks to library's allMediaItems() 

set thePath to "/tmp/songkong_itunes_model.new" 

set theLines to {} 
repeat with aTrack in tracks 
    try 
     set nextline to (aTrack's title as text) ¬ 
      & "::" & (aTrack's location's |path| as text) ¬ 
      & "::" & (aTrack's persistentID()'s intValue()) 
     set end of theLines to nextline 

    end try 
end repeat 
set {TID, text item delimiters} to {text item delimiters, return} 
set theText to theLines as text 
set text item delimiters to TID 
try 
    set fileref to open for access thePath with write permission 
    set eof fileref to 0 
    write theText as «class utf8» to fileref 
    close access fileref 
on error 
    try 
     close access thePath 
    end try 
end try 
+0

私はそれを実行すると面倒ですが、ファイルを作成すると空ですが、古いスクリプトは大丈夫です。 –

+0

スクリプトエディタとスクリプトデバッガでスクリプトを正常に実行しました。唯一の違いは、テキストファイルをデスクトップに保存したことです。 – vadian

+0

私は、このAPIを使用するためにコード署名する必要があるアプリケーションについて何かを読んで、それはちょうどスクリプトエディタで実行されている問題である可能性があります。 (これが呼び出される主なJavaアプリケーションはコード署名されていますが) –

関連する問題