Manipulating text strings?

Hi smart people!

I've searched the manual every way I can think of, but I have a feeling if this functionality exists it's defined or described in a way I haven't thought of. My daily workflow often includes very similar text manipulation—for example, a content creation app I use exports audio files with the naming convention "MAINFILE - REGION.mp3" but I nearly always need to change that to "MAINFILE (ADJECTIVE).mp3"

Is there a KM filter (or something) that can accept "MAINFILE - REGION" as an inputted text string and export "MAINFILE" given that I will always want the part of the string that precedes the space-hyphen-space separator? (Unfortunately the length of the two sides of the inputted string are not predictable.)

Thanks in advance for whatever wisdom you can share!

David

Can you give a before and after example and are you just wanting to select the file in the Finder and then have it rename the file? What if there is a conflict of the same name. Do you want a dialog box to ask you what to replace REGION with?

Input: "MAINFILE - REGION.mp3"
Desired output: "MAINFILE.mp3"

Yeah, I've got the suffixes all ready to go—I just need a way to change the variable FileName from "MAINFILE - REGION.mp3" to "MAINFILE"

Rename Files.kmmacros (24 KB)

1 Like

Brilliant, @skillet ! It's that "offset of" function that I was hoping existed! Thanks so much!

1 Like

Beware -- as written this will nuke file extensions too. Easy enough to add them back when building the new name, though -- change the set newName... line to:

set newName to text 1 thru ((offset of " - " in oldName) - 1) of oldName & "." & name extension of theItem

You can do more general solutions than @skillet's using "standard" KM actions -- the route you take will depend on what you are trying to do.

For one-shotting multiple lines, the easiest is to "Search and Replace Variable", deleting everything from " - " to the end of each line. You'll need a regular expression since you don't know exactly what "until the end of the line" might contain:

Block Trim.kmmacros (3.1 KB)

If you are working on one item at a time -- either because there is only one item or you are doing this in a "For Each" action -- you can instead "capture" the text before " - ". Since the divider is a constant, the easiest way is to treat the text as a pseudo array using a custom delimiter, grabbing the first element:

Item Trim (Array).kmmacros (3.6 KB)

Or you can use the "Search Using Regular Expression" action -- useful for when your "divider" can vary -- this, for example copes with one or more dashes in the divider. (And notice that it ignores the hyphenated word because there are no spaces. The others do to, I just forgot to include it!):

Item Trim (RegEx).kmmacros (4.2 KB)

Main thing to watch out for with that is to turn off "on failure" options if you'll have un-dashed items in your list:

If you're doing file renaming, these can all be combined with KM's "Move or Rename a File" action. This, for example, will rename Finder-selected items of the form:

MAINFILE - REGION.mp3
Another File - Something.txt

...to:

MAINFILE (DONE).mp3
Another File (DONE).txt

Name Change in Place.kmmacros (2.3 KB)

...although you'll probably want some error checking!

There's a bucket-load of ways to do this sort of thing -- have a search of the Forum for more ideas.

1 Like

My favorite part of this forum is the variety of solutions that come through from various helpful folx! Thanks tons for all of these ideas!

Great ideas, and always good to learn from you @Nige_S thank you for posting those macros as well to speed up the proces.