Apple Script Time-Out "Finder got an error: Connection is invalid"

Apple Script Time-Out "Finder got an error: Connection is invalid"

我有这个 Apple 脚本来处理大量数据。它所做的几乎是将数百个文件夹名称与数百个图片名称进行比较。如果图片名称中包含文件夹名称.. 它将此特定图片复制到相应的文件夹中。需要比对的数据非常庞大。。有时需要一个多小时才能完成。我需要如何解决这个具体问题的建议。

无论脚本执行的时间和地点(我的桌面或工作服务器计算机),都会出现此错误消息。通常出现在 40 分钟到 60 分钟之间。

因此所有查找器 windows 都关闭并且脚本停止。 看起来像是某种超时期限。 有没有办法延长这个期限或完全删除它?

谢谢! 编辑: 这是脚本本身:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property defaultColor : "Green"
on run {input}
    tell application "Finder"
        set MainFolder to (choose folder with prompt "ART.Numbers FOLDERS Directory:")
        set theArtFoldersList to every folder of MainFolder
        set NameOfMainFolder to name of MainFolder as text
        set targetFolder to "Users:zzz:Desktop:Pictures" as alias
        set pictures_collection to every file of entire contents of (targetFolder)
    end tell
    set userColor to 5
    repeat with CurrItem in theArtFoldersList
        tell application "Finder"
            if label index of CurrItem is not 7 and label index of CurrItem is not 6 and label index of CurrItem is not 5 then
                tell application "Finder" to set ArtFolderName to name of CurrItem as text
                tell application "Finder" to set elementpath to container of CurrItem as text
                set elementfolder to (elementpath & ArtFolderName) as alias
                
                repeat with anItem in pictures_collection
                    tell application "Finder" to set NameOfPicture to name of anItem
                    tell application "Finder" to set outpath to container of anItem as text
                    
                    tell application "Finder"
                        if NameOfPicture contains ArtFolderName and NameOfPicture does not contain (ArtFolderName & "_VIGN_N.jpg") and label index of anItem is 7 then
                            tell application "Finder" to duplicate anItem to folder (elementfolder as alias)
                            if label index of elementfolder is not 2 then -- пази червеният цвят на папката
                                set label index of elementfolder to userColor
                            end if
                        end if
                        if NameOfPicture contains (ArtFolderName & "_VIGN_N.jpg") and label index of anItem is 7 then
                            tell application "Finder" to duplicate anItem to folder (elementfolder as alias)
                            if label index of elementfolder is not 2 then
                                set label index of elementfolder to userColor
                                exit repeat
                            end if
                        end if
                    end tell
                end repeat
            end if
        end tell
    end repeat
    tell me to activate -- display dialog as active window
    display dialog NameOfMainFolder & " is Done !"
    return input
end run

由于我无法访问您的文件结构,因此我只能对以下脚本进行简单的测试,因此请对您的数据的副本进行测试,直到你确定它按你想要的方式工作。

我的尽职调查完成了...

'Connection is invalid' 错误来自 Finder 的 entire contents 命令,在包含大量文件的文件夹上执行。当 Finder 仍在尝试构建文件列表时脚本超时。无论如何,对 Finder 编写如此大量的脚本通常不是一个好主意; Finder 可能是喜怒无常的。考虑到这一点,我对您的脚本进行了三处基本更改:

  • 我从编写 Finder 脚本转向尽可能编写系统事件脚本。系统事件通常更稳定。
  • 我编写了一个迭代处理程序,它深入了解文件夹结构,而不是尝试提前收集所有文件。这将 Finder 必须完成的工作分解为易于管理的小块。
  • 为清楚起见,我将所有 Finder 调用放入单独的处理程序中。

我还将几个变量转移到 properties 以便脚本处理程序在向下迭代时可以看到它们。

property defaultColor : "Green"
property userColor : 5
property image_folder : "/Users/zzz/Desktop/Pictures"

on run
    set sorting_folder to (choose folder with prompt "ART.Numbers FOLDERS Directory:")
    tell application "System Events"
        set sorting_folder_name to name of sorting_folder as text
        my iterateThroughFolder(sorting_folder)
    end tell
    tell me to activate -- display dialog as active window
    display dialog sorting_folder_name & " is Done !"
end run

on iterateThroughFolder(sorted_art_folder)
    tell application "System Events"
        set subfolder_list to every folder of (sorted_art_folder)
        repeat with a_folder in subfolder_list
            my iterateThroughFolder(a_folder)
        end repeat
        
        -- skip top level       
        if POSIX path of sorted_art_folder is equal to image_folder then return
        
        set sorted_art_folder_list to folders of sorted_art_folder
        repeat with an_sorted_art_folder in sorted_art_folder_list
            set artFolderName to name of an_sorted_art_folder
            set file_list to (every file of folder image_folder whose name contains artFolderName)
            repeat with a_file in file_list
                if (my finderLabelIndex(path of a_file)) ≠ 7 then
                    my finderDupFile(path of a_file, path of an_sorted_art_folder)
                    if (my finderLabelIndex(path of an_sorted_art_folder)) ≠ 2 then
                        my finderSetLabelIndex(path of an_sorted_art_folder, userColor)
                    end if
                end if
            end repeat
        end repeat
    end tell
end iterateThroughFolder

on finderLabelIndex(finderPath)
    tell application "Finder"
        return label index of item finderPath
    end tell
end finderLabelIndex

on finderSetLabelIndex(finderPath, idx)
    tell application "Finder"
        set label index of item finderPath to idx
    end tell
end finderSetLabelIndex

on finderDupFile(fileFinderPath, destingationFinderPath)
    tell application "Finder"
        duplicate file fileFinderPath to folder destingationFinderPath
    end tell
end finderDupFile

N.B.: 注意经常混淆的路径引用样式。

  • 系统事件有自己的文件系统引用对象(disk itemfilefolder
  • Finder 有自己的文件系统引用对象(itemfilefolder
  • posix 路径 (/users/.../Desktop/...) 在系统事件中有效,但在 Finder 中无效
  • 系统路径 (Maintosh HD:users:...:Desktop:...) 在两个应用程序中都有效

如果你想从 System Events 发送一个引用到 Finder,使用 SE 的 path 属性(returns 一个系统路径),然后将它转换成一个 Finder 对象在 Finder tell 块中。请参阅上面的我的 Finder 处理程序。