2017-05-02 12 views
0

표준 Apple iTunes를 사용하는 대신이 AppleScript를 변환하여 iTunes Library framework을 사용할 수 있습니까?iTunes AppleScript로 새로운 iTunes 라이브러리 프레임 워크를 사용하도록 변환

나의 현재 스크립트는 사용자의 iTunes 보관함을 읽고 TrackName, TrackLocation 및 PersistentId로 구성된 파일을 만들고,이 작품을 안정적으로하지만 사용자가

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 보관함이있는 경우 속도가 느려질 수 있습니다 새로운 프레임 워크는 실제로 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를 사용하기 위해 코드 서명을해야하는 응용 프로그램에 대해 읽었습니다. 스크립트 편집기에서 실행중인 문제 일 수 있습니다. (비록 이것이 내 메인 자바 애플리케이션이라고 불리는데 코드 서명이되어있다.) –