如何编写 Applescript 将文件扩展名更改为 .eml?

How can I write an Applescript to change file extension to .eml?

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

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

编辑:

好的,在查看评论后,我现在正在寻找有关 Applescript 的一些方向来完成扩展重写。

正如评论中所讨论的,您的文件夹操作被调用两次的原因是因为它更改了给定的任何文件的文件扩展名。文件夹操作由添加到监视文件夹的任何新文件触发;通过更改文件扩展名,您有效地为该文件夹提供了一个以前不存在的新文件,因此再次调用该操作。

Automator 没有太多的流量控制方式,因此能够在收到已处理的文件时重定向操作,甚至终止操作是几乎不可能。

因此,最好的办法是使用 AppleScript。为此,请从您的工作流程中删除所有当前操作,并添加 运行 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 文件和文件夹。

正如我所说,另一种方法是将文件移动到其他地方进行处理。这可以包括 sub-folder 被监视的文件夹,因为文件夹操作由添加到被监视的实际文件夹的文件触发,并且 它的任何 sub-folder s.

如果您想使用此方法,那么此脚本将为您完成:

    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”文件夹中的 sub-folder,名为“ 重命名”。当然,您可以将其更改为您想要的任何内容。但是,此脚本 不会 为您创建文件夹,因此您必须先确保它存在。否则,文件夹操作将终止,什么也不会发生。

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


实际上我在测试第二个脚本时无意中这样做了。如果 sub-folder 尚不存在,我试图让文件夹操作创建它。但是,在我不得不终止脚本之前,它最终一次又一次地尝试创建 sub-folder 。哎呀