AppleScript to remove last three letters from a file name, preserving extension
I'm using some software that outputs files of the format
abcdefg#01.wav
abcdefg#02.wav
abcdefg#03.wav
and so on.
I'd like to create a Quick Action in Automator to use in Finder to remove the #, and everything after the #, but preserving the file extension.
Can anyone help me out?
In a new Automator Quick Action Workflow, you’ll want to set the initial properties so that the workflow accepts files and folders as input, and select Finder as the application to which this workflow is applicable.
Add a single action to the workflow. There are two good choices, and which you choose is up to you. I recommend the Run Shell Script action for its speed and compactness, but the Run AppleScript action will likely be easier for you to adapt to other situations in the future:
1) Run Shell Script :
-
If your preferred shell is
zsh
orbash
, or one of these options is present in the list, then you can use the code below with the following parameters selected for the action:→ Shell:
/bin/zsh
or/bin/bash
→ Input:as arguments
Check the path to the chosen shell matches the shebang (
#!/path/to/shell
) on the first line of this script, editing it as required:#!/bin/zsh alias rename=echo # alias rename='mv -n' for f in "$@" ; do rename "$f" "${f%#*}.${f##*.}" done
This script is safe to run "as is", as it will not perform any file operations just yet. It will print each filepath followed by the proposed path of the renamed file on a single line, one line per file. If the result looks good, you can remove the
#
precedingalias rename='mv -n'
, after which, any susbsequent invocation of the script will perform the file operations that will rename the files.
-
If you use
FiSH
as your preferred shell, select it from the list of options, and apply the following parameters to the action (the path to yourFiSH
shell may be different, e.g./usr/local/bin/fish
):→ Shell:
/usr/bin/fish
→ Input:as arguments
Edit the shebang on the first line to reflect the actual path to
FiSH
:#!/usr/bin/fish function mv echo $argv \n end for filepath in $argv mv -n "$filepath" ( string replace -r \ -- '(?x) \# [0-9]+ (?=\.[^.]+$)' \ '' "$filepath" ) end
This is safe to run "as is", and will only print the proposed renaming next to the original filepath, one per line for each file. If it looks good, you can delete the function declaration, namely these three lines:
function mv echo $argv \n end
after which, any subsequent invocation of the script will act upon the files and rename them accordingly.
2) Run AppleScript:
-
Replace any sample code that is present in the editing pane, and replace it with the following:
property text item delimiters : "#" on run {filepaths} tell application id "com.apple.systemevents" repeat with filepath in the filepaths tell the item named filepath set {filename, extension} to its {name, name extension} tell the extension to if its length > 0 ¬ then set the extension to "." & it if "#" is in the filename then set its name to ¬ "" & (the filename's text items 1 thru -2) ¬ & the extension end tell end repeat end tell end run
I recommemd testing this on some dummy files initially to make sure it does what you expected. I haven't the means to test this code, which I would normally do, although even if I had, it's always advisable to test any code you didn't write yourself beforehand.