iTunes에서 선택한 트랙을 삭제하려면 AppleScript를 어떻게 수정해야 합니까?

StackOverflow https://stackoverflow.com/questions/1455011

  •  12-09-2019
  •  | 
  •  

문제

선택한 모든 트랙을 삭제하기 위해 다음 AppleScript를 만들었습니다.

property okflag : false

-- check if iTunes is running 
tell application "Finder"
    if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
    tell application "iTunes"
        if selection is not {} then
            repeat with this_track in selection
                try
                    try
                        set cla to class of this_track
                        set floc to (get location of this_track)
                        delete this_track
                    on error error_message number error_number
                        display alert error_message message ("Error number: ") & error_number & "."
                    end try
                    if cla is file track then
                        my delete_the_file(floc)
                    end if
                end try
            end repeat
        end if
    end tell
end if

to delete_the_file(floc)
    try
        -- tell application "Finder" to delete floc
        do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
    on error
        display dialog "Track deleted, but could not be moved to trash" buttons {"Hmm"} default button 1 with icon 1
    end try
end delete_the_file

단일 항목을 선택하면 제대로 작동하지만 둘 이상을 선택하면 다음과 같은 결과가 나타납니다."선택 항목 2의 위치를 ​​가져올 수 없습니다"(오류 번호 -1728).나는 이것이 트랙을 삭제함으로써 선택 항목에 대한 스크립트의 색인이 손상되었기 때문이라고 생각합니다.

먼저 삭제할 트랙 목록을 직접 만들어 볼까 생각했습니다.

tell application "iTunes"
    if selection is not {} then
        set to_delete to {}
        repeat with this_track in selection
            try
                set cla to class of this_track
                set floc to (get location of this_track)
                if cla is file track then
                    set pair to {this_track, floc}
                    set to_delete to to_delete & pair
                end if
            end try
        end repeat
        repeat with pair in to_delete
            set the_track to item 1 of pair
            set floc to item 2 of pair
            delete the_track
            my delete_the_file(floc)
        end repeat
    end if
end tell

그러나 '응용 프로그램 "iTunes"의 선택 항목 중 항목 1을 가져올 수 없습니다'라는 메시지가 표시됩니다 내 생각에 문제는 "this_track"이 Track 클래스의 객체가 아니라 선택 항목의 객체라는 것입니다.선택 항목에서 실제 트랙 개체를 어떻게 가져오나요?

해결 방법이 보이지 않으면 디버깅에 대한 팁이나 기타 제안 사항을 알려주세요.

도움이 되었습니까?

해결책

변수 this_track 개체 지정자에 대한 참조입니다.당신은 contents 포함된 객체 지정자를 가져오는 속성입니다.변수에 접근하는 경우에도 마찬가지입니다. pair 두 번째 루프에서.클래스의 클래스 참조 섹션을 참조하세요. reference 에서 AppleScript 언어 가이드.

목록을 작성하는 방식에 또 다른 문제가 있습니다. to_delete 건설되고 있습니다.성명서 set to_delete to to_delete & pair 쌍 목록이 아닌 단순 목록이 생성됩니다.클래스의 클래스 참조 섹션을 참조하세요. list 에서 AppleScript 언어 가이드.

다음은 이러한 버그가 제거된 두 번째 스크립트 버전입니다.

tell application "iTunes"
    if selection is not {} then
        set to_delete to {}
        repeat with this_track in selection
            try
                set cla to class of this_track
                set floc to (get location of this_track)
                if cla is file track then
                    set pair to {contents of this_track, floc}
                    copy pair to end of to_delete
                end if
            end try
        end repeat
        repeat with pair in to_delete
            set the_track to item 1 of contents of pair
            set floc to item 2 of contents of pair
            delete the_track
            my delete_the_file(floc)
        end repeat
    end if
end tell
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top