Some AppleScript Help Needed Please - File Rename Task

Hi, I am trying to automate a process that may use Keyboard Maestro, or Automator (or both) depending on how it ends up. One thing I do know is that as part of the process I need to:

Look at a filename
Loop through an array of 'phrases' (100 or so)
If there's a match, rename with the prescribed change.

It's not just a case of renaming underscores as hyphens or dashes, as there are certain patterns, but thankfully the patterns are quite simple.

To give a snippet:

If I find 1-1-1-, I want to replace with 1.1.1-
1-4-1- changes to 1.4.1-
1-4-10 changes to 1.4.10-

Basically, some digits at the beginning of the file, which are hyphenated need to change to . but ONLY those first two hyphen instances.

I already have a lookup for the changes ready (although I am sure that this is possible with Regex but I will never get my head around that.

I am a web developer and I know exactly how I'd do this in JavaScript, but AppleScript is something that I just can't quite fathom.

If anyone is able to paste a snippet of AppleScript that could accept a filename passed in (if it were a step in Automator or KM) and show looping through an array of date based on the above, I would REALLY appreciate it.

[For info, currently I have the file rename (minus that specific request) set up as an Automator service, so I can highlight a batch of files and trigger it with a key combination (not via Keyboard Maestro, but in the macOS shortcuts). If this is something that would make more sense to do in KM, I am all ears as that would make syncing such things across my Macs better, anyway.]

Then why not use JavaScript for Automation (JXA)?
See JXA Resources.

I'm sure this could be done in either JXA or AppleScript, but it also can be done using KM non-scripting Actions easy enough.

Here's an example to help you get started. Let us know if you have any questions.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example Output

image

MACRO:   Rename Files With a Certain Pattern [Example]

-~~~ VER: 1.0    2021-02-04 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Rename Files With a Certain Pattern [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Rename Files With a Certain Pattern [Example]

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Select the Files in the Finder to be processed
  3. Trigger this macro.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • IF/THEN Action
      • Change the two RegEx expression to the pattern you want to match
        • ^\d+-\d+-\d+-
        • ^(\d+)-(\d+)-

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @RegEx @Finder @Rename

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

==USE AT YOUR OWN RISK==

  • While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

1 Like

This is PERFECT. Thank you so much, it works exactly as I want it to, and I'm much happier to have this in KM than in an Automator app. But what's more, having a complete working example that you have provided means that I am able to amend that and adapt that for further similar tweaks. I can learn from this one.

And also know I have JavaScript automation to learn about.

1 Like