كيف يمكنني إصلاح جهاز AppleScript لحذف مجموعة مختارة من المسارات في iTunes؟

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

ولكن بعد ذلك، أحصل على "لا أستطيع الحصول على العنصر 1 من البند 1 من اختيار التطبيق" iTunes ". أعتقد أن المشكلة هي "This_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