【问题标题】:Non-recursive folder actions with applescript使用 applescript 的非递归文件夹操作
【发布时间】:2020-09-27 11:40:55
【问题描述】:

我有一个文件夹操作来监视文件夹,以便在将文件添加到其中时运行 Applescript。

Applescript 告诉我的缩略图生成器应用程序来处理图像。这工作正常。该应用程序会创建一个子文件夹,并将缩略图放入其中。

问题是文件夹操作会为子文件夹中的每个图像触发,它会在另一个子文件夹中生成缩略图的缩略图。

由于某种原因,这种情况只发生了一次,但仍然太多了。

有没有办法可以设置文件夹操作来忽略添加到其子文件夹的文件?我需要维护缩略图生成器输出到子文件夹的目录结构。

这是脚本:

on adding folder items to this_folder after receiving added_items
    repeat with eachItem in added_items
        tell application "ThumbsUp" to open eachItem
    end repeat
end adding folder items to

在另一个论坛上有人建议这样做:

on adding folder items to this_folder after receiving added_items
    
    repeat with eachItem in added_items
        tell application "Finder" to set itsParent to (container of eachItem) as alias
        if itsParent is this_folder then tell application "ThumbsUp" to open eachItem
    end repeat
    
end adding folder items to

这应该使 ThumbsUp 不会在子文件夹中的项目上运行,但由于某种原因它会继续以相同的行为。

如何阻止文件夹操作在子文件夹上运行?

【问题讨论】:

标签: applescript


【解决方案1】:

另一个论坛上的某个人发布了脚本作为答案,这很有效。 问题是因为 ThumbsUp 创建了子文件夹,并且该子文件夹被添加到要打开的文件列表中。所以这个脚本解决了这个问题

on adding folder items to this_folder after receiving added_items
    repeat with eachItem in added_items
        tell application "System Events" to set itsKind to kind of eachItem
        if not (itsKind is "Folder") then tell application "ThumbsUp" to open eachItem
    end repeat
end adding folder items to

【讨论】:

  • 既然 ThumbsUp 也可以处理 folders,为什么不code 这样做而只忽略 由它创建的文件夹。那将是一个更强大的解决方案。将tell application "System Events" to set itsKind to kind of eachItem 更改为tell application "System Events" to set itemsKindAndName to {kind, name} of eachItem 并将if not (itsKind is "Folder") then tell application "ThumbsUp" to open eachItem 更改为:if not itemsKindAndName is equal to {"Folder", "_thumb"} then tell application "ThumbsUp" to open eachItem 注意_thumb 是默认名称,根据需要更改代码
【解决方案2】:

文件夹操作通常不会递归,因此我怀疑问题在于“ThumbsUp”应用程序旨在处理图像的文件夹以及单个图像。当它创建一个文件夹来存储缩略图时,该文件夹被视为“添加”到监视文件夹,并被发送到脚本并返回给 ThumbsUp。打开 ThumbsUp 并查看是否可以将输出文件夹设置为其他位置。如果这不起作用,请尝试在脚本中排除正在处理的文件夹:

on adding folder items to this_folder after receiving added_items
    tell application "System Events"
        repeat with an_item in added_items
            set disk_item to disk item (POSIX path of an_item)
            if class of disk_item is not folder then
                tell application "ThumbsUp" to open disk_item
            end if
        end repeat
    end tell
end adding folder items to

【讨论】:

  • 您好,泰德,感谢您的回复。我认为您诊断它为什么不起作用是正确的,尽管当我尝试您的 applescript 时,图像会通过其默认应用程序而不是 ThumbsUp 打开。在另一个论坛上有人想出了解决方案,所以我将在下面发布
  • 是的,我可以确认这只会在 Preview 中打开 file,或者目标文件类型的默认应用程序恰好是什么。问题是disk item 中的set disk_item to disk item (POSIX path of an_item),反正它不需要在那里。此外,既然 ThumbsUp 也可以处理 folders,为什么不code 这样做,而只忽略创建的 folder通过它。这肯定是一个更强大的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2013-12-06
  • 1970-01-01
  • 2016-09-08
  • 2011-05-28
  • 2016-05-28
相关资源
最近更新 更多