AppleScript修改文件为.eml扩展名及Automator工作流循环弹窗问题求助
Hey there! Let's tackle your two questions one by one—first writing an AppleScript to change file extensions to .eml, then fixing that annoying loop in your Automator workflow.
You can pick from two scripts based on your needs:
1. 处理单个选中的文件(Finder中选中文件后运行)
This script lets you select a single file and converts its extension to .eml, with checks to avoid redundant work:
set targetFile to choose file with prompt "选择要修改扩展名的文件:" set fileExtension to name extension of (info for targetFile) -- Skip if the file already uses .eml if fileExtension is "eml" then display dialog "这个文件已经是.eml格式了!" buttons {"确定"} default button 1 else -- Rename the file set newFilePath to POSIX path of targetFile as string set newFilePath to text 1 thru ((length of newFilePath) - (length of fileExtension) - 1) of newFilePath & ".eml" do shell script "mv " & quoted form of POSIX path of targetFile & " " & quoted form of newFilePath display dialog "扩展名已成功修改为.eml!" buttons {"确定"} default button 1 end if
2. 批量处理文件夹中的所有文件
Use this if you need to convert every eligible file in a folder:
set targetFolder to choose folder with prompt "选择要批量处理的文件夹:" set fileList to list folder targetFolder without invisibles repeat with fileName in fileList set fullFilePath to POSIX path of targetFolder & fileName set fileInfo to info for (POSIX file fullFilePath as alias) set fileExtension to name extension of fileInfo -- Skip folders and already .eml files if fileExtension is not "eml" and folder of fileInfo is false then set newFilePath to text 1 thru ((length of fullFilePath) - (length of fileExtension) - 1) of fullFilePath & ".eml" do shell script "mv " & quoted form of fullFilePath & " " & quoted form of newFilePath end if end repeat display dialog "批量修改完成!" buttons {"确定"} default button 1
Your issue is almost certainly caused by a loop: when you rename the file, the folder's watch trigger detects the change and runs the workflow again. The default rename action's confirmation dialog makes this loop obvious. Here are two solid fixes:
方案1:用AppleScript替代默认的重命名动作
This is the most reliable fix. Replace your existing rename action in Automator with a Run AppleScript action, then paste this code:
on run {input, parameters} repeat with aFile in input set fileInfo to info for aFile set fileExtension to name extension of fileInfo -- Only process non-folder files that aren't already .eml if folder of fileInfo is false and fileExtension is not "eml" then set fullPath to POSIX path of aFile set newPath to text 1 thru ((length of fullPath) - (length of fileExtension) - 1) of fullPath & ".eml" do shell script "mv " & quoted form of fullPath & " " & quoted form of newPath end if end repeat return input end run
This script runs silently, skips redundant changes, and won't trigger the folder watch loop again.
方案2:调整Automator的触发设置(备选)
If you prefer using the default rename action, try this:
- Open your Automator workflow, find the Folder Action trigger
- Click the Options button under the trigger, then check "Ignore input from this action"
- Note: This might not work if your folder is synced with iCloud—iCloud's sync events can still trigger the watch loop. For iCloud folders, stick with the AppleScript method.
内容的提问来源于stack exchange,提问作者codacopia

