正如 cmets 中所讨论的,您的文件夹操作被调用两次的原因是因为它会更改所提供的任何文件的文件扩展名。文件夹操作由添加到监视文件夹的任何新文件触发;通过更改文件扩展名,您有效地为文件夹提供了一个以前不存在的新文件,因此再次调用该操作。
Automator 没有太多的流控制,因此当它接收到一个已经处理过的文件时,它几乎不可能重定向操作,甚至终止它。
因此,最好的办法是使用 AppleScript。为此,请从您的工作流程中删除所有当前操作,并添加一个 Run AppleScript 操作,该操作可在 Utilities 类别中找到。
然后将此代码粘贴到文本区域:
on run {input, parameters}
set the text item delimiters of AppleScript to "."
tell application "Finder" to ¬
repeat with f in input
repeat 1 times -- An intentionally redundant loop
-- Allows us to move straight to the
-- next iteration of the outside loop
-- Ignore files that already have the ".eml" extension
if the name extension of f is "eml" then exit repeat
-- Ignore folders
if the kind of f is "Folder" then exit repeat
set the name extension of f to "eml"
end repeat
end repeat
end run
如果您浏览该脚本,您会看到它的说明可以忽略已具有扩展名 .eml 的文件以及忽略文件夹(以防万一)。文件夹操作将仍被调用两次,但一旦在第二次执行期间到达这些行,脚本将终止。
这是有关文件夹操作的一般建议的部分原因,即首先将文件从监视文件夹移到单独的文件夹中,然后在那里处理它们,特别是如果有正在重命名等发生。这可以防止第二次调用动作和脚本。
在您的情况下,结果仅仅是再次弹出警报带来的不便。但是,在其他情况下,您可以创建一个需要用户干预才能终止的无限处理循环。这方面的一个例子是一个动作,而不是将文件扩展名更改为.eml,而是简单地将扩展名添加到文件名的末尾,因此filename.txt将变为filename.txt.eml,这然后将变为filename.txt.eml.eml 等等,无限循环。†
但是,我个人对此的感觉是,将文件保存在被监视的文件夹中是可以的,只要您可以满足递归调用动作或脚本的可能性。
我上面的脚本通过告诉它忽略 .eml 文件和文件夹的行来执行此操作。
另一种方法是,正如我所说,将文件移动到其他地方进行处理。这可以包括被监视文件夹的子文件夹,因为文件夹操作由添加到被监视的实际文件夹的文件触发,不是它的任何子文件夹。
如果您想使用此方法,那么此脚本将为您执行此操作:
on run {input, parameters}
set the text item delimiters of AppleScript to "."
get some item of (input as list) as text
set fol to the result & "::" as alias -- The watched folder path
tell application "Finder" to ¬
repeat with f in input
repeat 1 times -- An intentionally redundant loop
-- Allows us to move straight to the
-- next iteration of the outside loop
-- Ignore files that already have the .eml extension
-- and all folders
if the name extension of f is "eml" then exit repeat
if the kind of f is "Folder" then exit repeat
-- Moving the file before renaming will prevent this
-- folder action from being called a second time
move f to folder "renamed" in folder fol
set the name extension of the result to "eml"
end repeat
end repeat
end run
这会将提供的文件(.eml 文件和文件夹除外)移动到“email”文件夹中名为“renamed”的子文件夹中.当然,您可以将其更改为您想要的任何内容。但是,此脚本不会为您创建文件夹,因此您必须首先确保它存在。否则,文件夹操作将终止,不会发生任何事情。
如果您有任何问题,请发表评论,我会尽快回复您。
†我实际上是在测试第二个脚本时偶然这样做的。我试图让文件夹操作来创建子文件夹,如果它不存在的话。但是,在我不得不终止脚本之前,它最终一次又一次地尝试创建子文件夹。哎呀。