如何用AppleScript/JavaScript在Automator中创建文件名规范化服务
Got it, let's solve this problem step by step. You need two solutions: a standalone AppleScript to rename files, and an Automator service (for quick right-click access in Finder) that cleans filenames by replacing invalid characters/spaces with dashes and converting everything to lowercase. Here's how to implement both:
1. Automator Quick Action (Service) for One-Click Filename Cleaning
This is the most convenient option for daily use—you can right-click any file/folder in Finder and run it directly.
Step-by-Step Setup:
- Open Automator (found in Applications or via Spotlight search).
- Select Quick Action (older macOS versions may label this "Service").
- At the top of the workflow window:
- Set Workflow receives selected to files or folders
- Set in to Finder
- Check the box for Output replaces selected text (optional but recommended)
- Search for Run AppleScript in the left pane, then drag it into the workflow area.
- Replace the default AppleScript code with the following:
on run {input, parameters} -- Define regex pattern for invalid characters + spaces set invalidChars to "[()\\[\\]\\\\/:\"*?<>|+_ ]" set counter to 1 repeat with itemToRename in input tell application "Finder" set originalName to name of itemToRename set nameExtension to name extension of itemToRename -- Split filename into base and extension (if present) if nameExtension is not "" then set baseName to text 1 thru ((length of originalName) - (length of nameExtension) - 1) of originalName -- Clean base name: replace invalid chars with dashes, convert to lowercase set newBaseName to do shell script "echo " & quoted form of baseName & " | sed -E 's/" & invalidChars & "+/-/g' | tr '[:upper:]' '[:lower:]'" set newName to newBaseName & "." & nameExtension else set newName to do shell script "echo " & quoted form of originalName & " | sed -E 's/" & invalidChars & "+/-/g' | tr '[:upper:]' '[:lower:]'" end if -- Handle duplicate filenames to avoid overwriting set newNameFinal to newName repeat while exists file newNameFinal in container of itemToRename set newNameFinal to text 1 thru ((length of newName) - (length of nameExtension) - 1) of newName & "-" & counter & "." & nameExtension set counter to counter + 1 end repeat -- Apply the new filename set name of itemToRename to newNameFinal end tell end repeat return input end run
Key Features of This Script:
- Replaces all specified invalid characters (
()[]\/:"*?<>|+_) and spaces with dashes - Collapses consecutive invalid characters into a single dash (thanks to the
+in the regex) - Converts filenames to lowercase
- Automatically adds a counter (-1, -2, etc.) if the new filename already exists
- Preserves file extensions (you can modify the code to lowercase extensions too if needed)
Save and Use:
- Save the service with a name like Clean Filenames
- Now, right-click any file/folder in Finder → go to Services → select your new service to rename it instantly.
2. Standalone AppleScript for Batch Renaming
If you want to run a script directly (e.g., via Script Editor) to rename all files in a selected folder, use this code:
set targetFolder to choose folder with prompt "Select folder to rename files in:" set invalidChars to "[()\\[\\]\\\\/:\"*?<>|+_ ]" tell application "Finder" set fileList to every file in targetFolder repeat with aFile in fileList set originalName to name of aFile set nameExtension to name extension of aFile if nameExtension is not "" then set baseName to text 1 thru ((length of originalName) - (length of nameExtension) - 1) of originalName set newBaseName to do shell script "echo " & quoted form of baseName & " | sed -E 's/" & invalidChars & "+/-/g' | tr '[:upper:]' '[:lower:]'" set newName to newBaseName & "." & nameExtension else set newName to do shell script "echo " & quoted form of originalName & " | sed -E 's/" & invalidChars & "+/-/g' | tr '[:upper:]' '[:lower:]'" end if -- Handle duplicates set newNameFinal to newName set counter to 1 repeat while exists file newNameFinal in targetFolder set newNameFinal to text 1 thru ((length of newName) - (length of nameExtension) - 1) of newName & "-" & counter & "." & nameExtension set counter to counter + 1 end repeat set name of aFile to newNameFinal end repeat end tell display dialog "Files renamed successfully!" buttons {"OK"} default button 1
How to Use:
- Open Script Editor (Applications → Utilities)
- Paste the code above
- Click the run button (▶️)
- Select the folder containing files you want to rename when prompted
Important Notes:
- Always test on a copy of your files first to avoid accidental data loss.
- If you don't want to collapse consecutive dashes, remove the
+from the regex pattern (changes/" & invalidChars & "+/-/gtos/" & invalidChars & "/-/g).
内容的提问来源于stack exchange,提问作者Yuri
相关产品推荐
相关产品推荐

