Automator: remove last 9 characters in filename

I'm hoping to find a solution that will be a simple fix. Here's what I want to do: Every week I have a new batch of files that I process through Apple's Automator to complete various functions such as copying/moving, etc. An example file name is:

example_20200128.mov

I would like to remove the last 9 characters, preserving the extension:

example.mov

Since I'm already working in Automator, I'd like to also rename in Automator, as opposed to using a separate renaming software. It seems like there should be a simple solution, however, I do not "speak code", so I'm a bit lost. Any help would be greatly appreciated!


The following AppleScript should do the job. It removes only an underscore followed by eight digits, e.g. _2020012, at the end of a filename without changing the extension. I don't know how familiar you are with using AppleScripts but you should be okay if you carefully follow the directions below. Sorry that I can't make its creation simpler but, once created, it is simple to use.

on run {input, parameters}

    repeat with thisFile in input
        tell application "Finder"
            set {theName, theExtension} to {name, name extension} of thisFile
            if theExtension is not in {missing value, ""} then
                set theExtension to "." & theExtension
                set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
                set theName to do shell script "/usr/bin/sed -E 's/_[0-9]{8}$//' <<< " & theName's quoted form -- strip consecutive trailing numbers
                set name of thisFile to theName & theExtension
            end if
        end tell
    end repeat

end run

To place this code on your Mac, select File->New->Service inside Automator and then double click on "Run AppleScript" among the "Library" options. Select the "files or folders" option in the "Service receives selected" menu at the top of the newly opened window. Paste the code inside the window and save it through File->Save inside Automator. Make sure you select all of the code above before copying it from here as a few lines of the code run quite a bit to the right outside the highlighted window here.

You can give this AppleScript a name such as "Remove_Number" while saving it inside Automator. The first attached picture shows how things should look like in Automator after the code is pasted and saved. You can quit Automator after the code is saved (but it does not actually matter).

When completed, you should be able to change the names of any number of files you might select through Finder. After selecting the files to be changed in Finder, right click on the selected files and you should see an option with the name you have given the AppleScript towards the bottom of the right-click menu. (The 2nd attached picture shows how things look like in Finder at this stage.) Any selected file with an underscore followed by eight digits at the end of its name should have that underscore and the digits removed immediately after you select the AppleScript from the right-click menu.

Automator state Finder state