【问题标题】:How can I write an Applescript to change file extension to .eml?如何编写 Applescript 将文件扩展名更改为 .eml?
【发布时间】:2018-08-08 17:55:40
【问题描述】:

我有以下 Automator 工作流程。这个想法是,当我将文件加载到email 文件夹中时,Automator 将添加扩展名.eml。在实践中,这可以正常工作,但有一个例外。在我将文件移动到email 目录后,Automator 会弹出对话框选择文件并确认扩展名。完成此操作后,将再次弹出对话框。然后我必须手动点击cancel 按钮来停止循环。

有没有办法清理这个问题,以便 Automator 只执行一次此操作?另外,有没有办法在没有所有弹出窗口和确认的情况下执行此操作?

编辑:

好的,在查看了 cmets 之后,我现在正在 Applescript 上寻找一些方向来完成扩展重写。

【问题讨论】:

  • 我没有 Mac。但 Automatot 不是 osascriptable 吗? (Applescriptable)?我认为您可以添加一个过滤器,将 .eml 扩展名作为排除标准
  • 是:首先检查电子邮件文件是否以扩展名.eml 结尾;如果是,则终止工作流程;如果没有,让它继续。事实上,Automator 在控制执行和流程方面确实是次要的;编写一个 AppleScript 并将其用作文件夹操作会容易得多。
  • @CJK 感谢您的指导。我尝试添加一个过滤器来检查扩展,但这似乎并没有停止工作流程。我没有跟上 AppleScript 的速度,你有我可以在这里放置的示例脚本来实现这个工作流程的目标吗?
  • @CJK 我已经更新了这个问题,以便更直接地回答您的评论。一个示例 Applescript 将不胜感激。
  • @CJK 不用担心。如果写出脚本过于本地化,参考链接会有所帮助。

标签: applescript file-rename automator finder


【解决方案1】:

正如 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”的子文件夹中.当然,您可以将其更改为您想要的任何内容。但是,此脚本不会为您创建文件夹,因此您必须首先确保它存在。否则,文件夹操作将终止,不会发生任何事情。

如果您有任何问题,请发表评论,我会尽快回复您。


我实际上是在测试第二个脚本时偶然这样做的。我试图让文件夹操作来创建子文件夹,如果它不存在的话。但是,在我不得不终止脚本之前,它最终一次又一次地尝试创建子文件夹。哎呀。

【讨论】:

  • 你摇滚!这是我在这里收到的最彻底的答案之一。它工作得很好,非常感谢:)
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2020-02-24
相关资源
最近更新 更多