如何修复我的 AppleScript 以删除 iTunes 中的选定曲目?

Posted

技术标签:

【中文标题】如何修复我的 AppleScript 以删除 iTunes 中的选定曲目?【英文标题】:How do I fix my AppleScript to delete a selection of tracks in iTunes? 【发布时间】:2009-09-21 15:10:43 【问题描述】:

我创建了以下 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 项的第 1 项。我认为问题在于“this_track”不是Track类的对象,而是选择项。如何从选择项中获取实际的轨道对象?

如果您没有看到解决方案,我将欢迎有关调试的提示或任何其他建议。

【问题讨论】:

【参考方案1】:

变量this_track 是对对象说明符的引用。您必须使用 contents 属性来获取封闭的对象说明符。在第二个循环中访问变量 pair 也是如此。请参阅AppleScript language guide 中reference 类的类参考部分。

列表to_delete 的构建方式存在另一个问题。语句set to_delete to to_delete & pair 不会生成对列表,而是生成平面列表。请参阅AppleScript language guide 中的类list 的类参考部分。

这是您的第二个脚本的一个版本,其中这些错误已被删除:

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

【讨论】:

我做了你的改变并得到了错误:'Can't get item 1 of > id 41009 of > id 40953 of > id 40 个应用程序“iTunes”。' 感谢您更新您的答案。我想我需要弄清楚如何调试 AppleScript——我会问一个问题。

以上是关于如何修复我的 AppleScript 以删除 iTunes 中的选定曲目?的主要内容,如果未能解决你的问题,请参考以下文章

我的新玩具-AppleScript(四)

从 Dock 中删除 AppleScript 脚本的图标

applescript:如何在多个窗口中执行击键

如何修复maven依赖it.sauronsoftware问题?

Applescript 从码头中删除项目

如何在 Applescript 中调用 Automator 变量?