0

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!

3
  • RE: "Since I'm already working in Automator, I'd like to also rename in Automator, as opposed to using a separate renaming software." -- If you what this incorporated into an existing Automator workflow, then you should provide the workflow and state where in the workflow you want to perform the renaming task. Jan 28, 2020 at 18:32
  • Absolutely. I have several that I'm using for various tasks, which is why I didn't include that information in my initial post. I'm hoping that I can find a solution that will work with various workflows. But maybe that's just evidence of my lack of understanding in this area :) Here's an example workflow: Get specified Finder Items, Get Folder Contents, Copy finder Items, Rename <-- this is the step I need help with.
    – user360531
    Jan 28, 2020 at 18:38
  • Rebekah, I've updated the AppleScript code so it only acts on a file with an extension and ends with an underscore followed by eight digits. Please use the updated code. Jan 29, 2020 at 14:44

1 Answer 1

1

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

2
  • I've changed the sed expression to conform to example name given in the OP. Jan 29, 2020 at 13:30
  • That worked perfectly, thank you so much! You've made my life much easier :)
    – user360531
    Jan 29, 2020 at 14:10

You must log in to answer this question.