Toggle Finder File Selection to Web Friendly Names

Continuing the discussion from Batch Rename files in the Finder:

I’ve still got a long way to go in utilizing the power of Keyboard Maestro variables, I am trying to make a couple Macros I use in QuicKeys to convert selected Finder files from having spaces to underscores and vice versa. How would do an “if then” on something like this?

Hey Skillet,

Get the name.

Do a find/replace.

Compare the new name to the original name, and if they are different then rename the file.

For Each Item in the Finder Selection:

  • Get the file name to variable originalName.
  • Search/Replace originalName for whatever.
  • IF originalName does not equal newName
    • Rename the file.
  • Otherwise Skip it.

Yada, yada.

Peter’s given you most of the pieces in his batch rename macro, but holler if you get stuck.

-Chris

There are just so many overly geeky things to understand that I feel like I am learning to program with Keyboard Maestro. I am constantly reminded of things like GarageBand has versus Logic that just made common tasks like ducking so much easier but yes Logic could do it and do so much more but it wasn't a few button clicks and bam you're in. All the stuff like adding a | for more than one option and %variable% to add the variable back into the macro when you run it again are all very cool but make for less accessibility to the light power user.

On the bright side it is forcing me to go deeper not having other options but in the meantime it is frustrating to get something quickly not having more easily visual rename tools and purpose built actions in areas like this.

I of course am trying to go more complicated and make it a single command and have figured out how to do one or the other from Peter's macro by permantly saving the values I am after. I am sure I will get it before too long.

Hey Skillet,

Overly geeky? You're just used to too much coddling by QuicKeys.  :sunglasses:

BTDT.

Peter gives you the tools to do things yourself instead of always doing them for you, and that's a fairly big paradigm shift to accommodate to.

Here's an AppleScript that allows you to find/replace in item-names using a regular expression.

It's a little more organic than Keyboard Maestro, simply because it uses the Finder to do the renaming (instead of copying the existing file to a new file with a new name.

It will bog down if you feed it more than 500 items at a time.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/05/08 00:59
# dMod: 2016/07/27 17:46
# Appl: Finder
# Task: Change Names of Selected Items Using a Regular Expression
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Find, @Replace, @Text, @RegEx, @Finder, @Rename
-------------------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------------------------------

set findPattern to "_"
set replacePattern to " "

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   
   repeat with theItem in finderSelectionList
      set itemName to name of theItem
      set newItemName to (my cngStr:findPattern intoString:replacePattern inString:itemName)
      
      considering case
         if newItemName ≠ itemName then
            set name of theItem to newItemName
         end if
      end considering
      
   end repeat
end tell

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
   set anNSString to current application's NSString's stringWithString:dataString
   set dataString to (anNSString's ¬
      stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
         options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
-------------------------------------------------------------------------------------------

I have a macro that uses a similar script and pops up a dialog for me to provide the find and replace patterns.

I also have scripts that will auto-massage file names for me.

Many things are possible.

Sometimes the learning process is a headache...

-Chris

Yes, this is a perennial problem in software - you have to choose between simplicity and power. You can have simple and limited, or complex and unlimited. It is not possible to have simple and unlimited.

With Keyboard Maestro, the way I have chosen to resolve this is to try to make each new thing you learn orthogonal to everything else, so that when you learn a new facility, it extends/amplifies everything else you have learnt.

For example, you could use Keyboard Maestro with only ever one action in a macro, no Variables, no Tokens, no Functions, and Keyboard Maestro would be much simpler and still a very useful tool.

Quickeys takes a very different approach and your example highlights it nicely. You can do what you specifically want - search and replace _ with space in the finder selection, and that is relatively easy. Great, but what happens when you only want to replace some of the Finder selection, or your want to rename it in a way they have not foreseen and pre-coded, then you hit a wall.

Keyboard Maestro takes a different approach. Instead of one action to rename the finder’s selection, if breaks the concept down into a set of independent concepts:

So yes, instead of one complex action, it is four actions and several different concepts. But those same concepts will work no matter how you apply them. And when you decide you want to rename only .jpg files, you can do that without needing me to add some sort of filter to the rename finder selection action thingy.

For Each.kmactions (1.3 KB)

1 Like

Peter, please forgive me if I disagree with you on this. :wink:

I don't know about "unlimited" (is any app/language really unlimited?), but I have to say that Keyboard Maestro offers a very unique combination of both simplicity and power.

Even if you know nothing about programming, you can easily construct a visual set of steps, called "Actions", to automate some process/workflow. You can even easily add simple, or complex, logic making decisions based on your data.

But wait! There's more!

If you are a programmer, you can go as far as you like, using scripts written in:

  • AppleScript
  • JavaScript
  • JavaScript for Automation (JXA)
  • Swift
  • Shell/Bash

You can even write a highly customized web page, using any JavaScript library you like, using the very powerful "Custom HTML Prompt" (the name does not do the feature justice).

So, IMO, Keyboard Maestro is an excellent tool for both the non-programmer, typical user, and the programmer.

Many, many thanks, Peter, for an outstanding tool.

2 Likes

No doubt there are so many things to unearth it is both exciting and extremely daunting.

Very useful, I would love to see your other AppleScripts if you care to post them. I did search MacScripture first just to see if you had posted something there.

No doubt I think you have done a pretty good job at that too.

Things like 01) 02) 03) to hide but sort macros with the same keyboard shortcuts is briliant and a wonderful trick I use all the time but never would have figured that out on my own and I use it quite frequently and several times today in fact.

I was just reminded of this again as I tried to import For Each.kmactions which was good for me to build on my own from looking at your image anyway.

That sounds exciting, not sure exactly what that entails but if you could get it to auto-massage your back that would be extremely welcome!

Maybe there is something that would break down internally if there were more turn-key or ID10T solutions with prebuilt actions that do common actions like simple renames or case change etc like in QuicKeys while still leaving the other actions in for people that really need more flexibility. That seems like the best of both worlds but it sounds like that sort of thing isn't possible. Even just including the four action solution you just posted in the Macro library would be helpful!

Really more than anything I hate to bother but now that [quote="peternlewis, post:5, topic:4488"]
Looping over a set of things with the For Each action.
Using the Finders Selection collection.
Getting the name or other information on the file with the Get File Attribute action.
Search & Replacing on the name with the Search and Replace Variable action.
Renaming the file with the Rename a File action.

So yes, instead of one complex action, it is four actions and several different concepts. But those same concepts will work no matter how you apply them. And when you decide you want to rename only .jpg files, you can do that without needing me to add some sort of filter to the rename finder selection action thingy.

For Each.kmactions (1.3 KB)
[/quote]

This post was so extremely helpful I just spent the past couple hours reading through this. It was so helpful to see the list of actions and then again see you post what it looks like in Keyboard Maestro. I am starting to get a little better idea of how lists translate into actions, though I have a very long way to go I know! All those filled in details in the action make so much more sense to me.

There was so much complexity in the macro I was trying to reference that I was struggling to break it down to its core like this. In all I am starting to better understand and at least have references to go back to for things I did in QuicKeys and how you do it in Keyboard Maestro. That just got me past other QuicKeys macros I have been holding off on moving over to Keyboard Maestro due to my ignorance.

I have several more as that I am stuck on but I think I can work through many more of the 195 left that I have to convert over and this should keep me going for a while. I actually thankfully have been able to work on one computer 90% of the time QuicKeys free that I didn't even launch it. The biggest hurdle is Keyboard Maestro recognizing background applications when they have a dialog box in the front, maybe that was a hack that QuicKeys does but it still works in El Capitan if it was.

Just found this AppleScript I had but forgot about.

set theFiles to choose file with multiple selections allowed

tell application "System Events"
	repeat with thisFile in the theFiles
		set oldFileName to name of thisFile
		
		set {oldTID, my text item delimiters} to {my text item delimiters, {"_", "-", "Whatever you want to remove"}}
		set fileNameBits to text items of oldFileName
		
		set my text item delimiters to " "
		set newFileName to fileNameBits as text
		
		set my text item delimiters to oldTID
		
		set name of thisFile to newFileName
		
	end repeat
end tell

Might be useful to someone.Remove Characters.zip (11.3 KB)