Applescript Apple脚本超时“Finder出错:连接无效”

Applescript Apple脚本超时“Finder出错:连接无效”,applescript,repeat,automator,finder,Applescript,Repeat,Automator,Finder,我用这个苹果脚本处理大量数据。它所做的就是将数百个文件夹名与数百个图片名进行比较。如果图片名称中包含文件夹名称。。它将此特定图片复制到相应的文件夹中。需要比较的数据是巨大的。。有时需要一个多小时才能完成。我需要一个如何解决这个具体问题的建议 此错误消息显示在我的桌面或工作服务器上执行脚本的时间和地点。通常出现在40分钟到60分钟之间 因此,所有查找器窗口都将关闭,脚本将停止。 看起来这是一段暂停期。 有没有办法延长这一期限或完全取消这一期限 谢谢大家!! 编辑: 以下是脚本本身: use App

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

此错误消息显示在我的桌面或工作服务器上执行脚本的时间和地点。通常出现在40分钟到60分钟之间

因此,所有查找器窗口都将关闭,脚本将停止。 看起来这是一段暂停期。 有没有办法延长这一期限或完全取消这一期限

谢谢大家!! 编辑: 以下是脚本本身:

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


由于我无法访问您的文件结构,所以我只能对以下脚本进行少量测试,因此请在您的数据副本上测试它,直到您确定它按您希望的方式工作为止

我的尽职调查完成了

“连接无效”错误来自Finder的“全部内容”命令,该命令在包含大量文件的文件夹上执行。当Finder仍在尝试构建文件列表时,脚本超时。无论如何,给查找程序编写这么重的脚本通常是个坏主意;发现者可能喜怒无常。考虑到这一点,我对您的脚本做了三个基本更改:

我从编写查找器脚本转移到尽可能编写系统事件脚本。系统事件通常更稳定。 我编写了一个迭代处理程序,它深入研究文件夹结构,而不是试图提前收集所有文件。这将查找者必须完成的工作分解为小的、可管理的块。 为了清晰起见,我将所有Finder调用放在单独的处理程序中。 我还将几个变量转换为属性,以便脚本处理程序在向下迭代时可以看到它们

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
注意:注意经常令人困惑的路径引用样式

系统事件有自己的文件系统引用对象磁盘项、文件、文件夹 Finder有自己的文件系统引用对象项、文件、文件夹 posix路径/users/../Desktop/。。。在系统事件中工作,但不在查找器中工作 系统路径Maintosh HD:用户:…:桌面:。。。在这两个应用程序中都工作
如果要将系统事件的引用发送到查找器,请使用SE的path属性返回系统路径,然后将其转换为查找器通知块中的查找器对象。请参阅上面的my Finder处理程序。

请显示脚本或至少相关部分。一般来说,为大型作业编写系统事件脚本比使用Finder更好。Finder是一个繁忙的应用程序,cfaps比SE更频繁。但并没有看到你们的剧本,这只是猜测。@TedWrigley你们当然是对的。我用脚本本身更新了帖子。这个脚本使用ApplescriptObjC还有更多吗?我很好奇为什么要调用一个未使用的框架。我最想知道的是,这样我就可以决定是在解决方案中使用AppleScriptObjC,还是应该使用纯AppleScript。@TedWrigley否,此脚本中没有使用AppleScriptObjC。我在其他脚本中使用了这个框架,但在这个脚本中它被错误地保留了下来。很抱歉。这是纯AppleScript。谢谢你的努力。你已经很好地解释了一切。即使是我理解的某些部分,我仍然有很多东西需要学习和实验,以便找出问题所在。脚本现在运行得非常快,但不幸的是,一旦它到达if语句,它就不会重复。我猜是什么在此之前,if还不太清楚它应该是什么样子。我已经做了一个屏幕截图来解释它应该如何在较小的数据规模下工作。也许这会有所帮助。再次感谢您的努力!如果不占用你太多时间,还有什么需要帮忙的吗?我会再近一点look@OutOfTouch:我一眼就怀疑问题出在出口重复线路上。那是你最初剧本的一部分,我不确定它的意图是什么,所以我把它留在了里面。但在某些情况下,它肯定会导致脚本短路。尝试删除该行,看看它是否解决了问题。不幸的是,这没有帮助。在我的脚本中,退出重复的要点是,一旦找到名为ArtFolderName&_VIGN_N.jpg的文件,就停止查找该文件。由于脚本按文件在文件夹中的占有顺序按名称对其进行检查,因此建议首先是带有包的文件以你的名义。。最后是VIGN_N。但即使在我的情况下,有时它也会跳过文件。所以请在假期前后有时间的时候…深入了解一下。我会在几个月内每天都使用这个脚本,如果你能为我节省时间,我会非常感激。