Rename only if needed

I have a macro that renames images by deleting the last 3 characters (a 3 digit counter) and typing a letter. However it would be great if there were a way for the macro to determine if the image has already been renamed before renaming again.

Example: 123456_123 gets renamed to 123456_A. If the macro trigger is hit again it will delete the last 3 characters resulting in a wrong name - 12345A.

The macro also performs other tasks, like processing and color tagging images. If a photographer realizes that further adjustments need to be made, they cannot hit the macro trigger again as it would misname the file.

Any suggestions would be appreciated!

Hey Jimmy,

Well, that's easy.

This macro just uses a regular expression to evaluate whether or not the file name ends with 3 digits.

If True then it renames the file — otherwise it passes.

As long as you can create a test that is consistently reliable for your task you're good to go.

--
Best Regards,
Chris


Finder-Selection { Rename Numbered Files }.kmmacros (4.6 KB)

Oh — note that the macro I posted is NOT a turnkey solution — just an example of what you can do.

-Chris

Thanks for that response! I’ll check it out. I think I just need to wrap my head around the ‘if then else’ action. My current macro hits the hotkey to rename an image, so the filename text would be highlighted at that point. I think I have to figure out how to turn that selected text into a variable (without replacing the clipboard contents).

Hey Jimmy,

Here’s an example of how I’d do this job with AppleScript:

--------------------------------------------------------------
# REQUIRES INSTALLATION OF SATIMAGE.OSAX AppleScript Extension
--------------------------------------------------------------
tell application "Finder" to set finderSelection to selection as alias list

if finderSelection ≠ {} then
  set AppleScript's text item delimiters to return
  copy (finderSelection as text) to nameList
  set nameList to change "^.+:" into "" in nameList with regexp without case sensitive
  set nameList to change "\\d{3}$" into "A" in nameList with regexp without case sensitive
  set nameList to splittext nameList using return
  set _cntr to 0
  
  tell application "Finder"
    repeat with i in finderSelection
      set _cntr to _cntr + 1
      set newName to item _cntr of nameList
      if name of i ≠ newName then
        set name of i to newName
      end if
    end repeat
  end tell
end if
--------------------------------------------------------------

As the script says the Satimage.osax is required, because AppleScript does not natively possess regular expression support. The SIO provides that and many other useful features.

In the script I create a list of new file names.

With regex I can specify that only 3 digits on the end be replaced with A (in this case).

If the file has already been renamed then the regex will NOT change it again, and the script ignores files when they would be renamed with their identical name.

Pretty simple stuff.

-Chris

Hey Jimmy,

I've done that in the macro by saving the file name and the file path to variables.

No need to use the clipboard.

Here's the basic logic of the macro:

FOR each filePath in a list (paths of files selected in the Finder)
   
   GET the file name of the file at filePath to variable fileName

   IF test for last 3 digits is TRUE

      Rename file by {
         
         GET the parent-path of the file to fileDir

         CHANGE the file name using a regex to variable fileName

         MOVE (used to RENAME in Unix) Original-file-path TO new-file-path

      }

   ELSE

      NOTHING

   END IF

END FOR

Sometimes it helps make sense of things to diagram it out.

-Chris

Edited previous post for clarity 2015/05/29 10:58
-ccs

So in Keyboard Maestro, how exactly do I set selected text as a variable?

Hey Jimmy,

What do you mean selected text?

I thought we were talking files selected in the Finder.

The venue makes a lot of difference.

Selected text in TextEdit for instance has to be gotten into the clipboard via Cmd-C and then saved to a variable.

On the other hand in TextWrangler or BBEdit you can use AppleScript to directly grab the selected text.

Give me a more clear idea of what you mean, and I’ll give you a better answer. :smile:

-Chris

Ok, let me try to explain, I’m probably not saying it correctly. When you wish to rename any file, you click the file name and either click again a moment later or hit ‘return’ to enter file-naming mode. At that point, the file name (text) is highlighted (selected).

I was hoping you could turn that text into a variable at that point somehow because it would be selected. However if it has to be added to the clipboard first, that wouldn’t work because the clipboard contents are important. They represent a scanned UPC that is used to create the first part of the file name (before the 3 digit counter)

Thanks a lot for your help thus far.

Hey Jimmy,

We've already done that. The first macro I posted shows how.

Run this in the Applescript Editor (Script Editor on Yosemite).

Select some files in the Finder first.

tell application "Finder"
  set finderSelectionVar to selection as alias list
  if finderSelectionVar ≠ {} then
    repeat with i in finderSelectionVar
      set contents of i to name of i
    end repeat
  end if
end tell

set AppleScript's text item delimiters to linefeed
set finderSelectionVar to finderSelectionVar as text

tell application "TextEdit"
  activate
  make new document with properties {text:finderSelectionVar}
end tell

This is similar to what Keyboard Maestro is doing in the macro.

Here's a somewhat simpler example of how to do this with KM.

Select 1 or two files in the Finder and run the macro.

Finder-Selection { Show File Name }.kmmacros (3.3 KB)

-Chris

Another question, getting more specific now. I am renaming images in ‘CaptureOne’ application. My current macro hits the hotkey to rename the file in that application and at that point the text is selected. I see that you can turn a file name from selected files in ‘Finder’ into a variable, but can you use any selected text (or file) in an application like ‘CaptureOne’ and turn that into a variable?

The full story: I am renaming images in CaptureOne, by having the macro hit the rename keystroke, deleting the final 3 digit counter, and typing the appropriate letter like ‘A’ The macro also hits keystroke to process (output RAW into jpg) image. Sometimes if you need to adjust anything with the image (contrast, exposure, crop, etc.) you need to re-process the image. Hitting the macro again would also rename the image again, but this time the 3 digit counter is already gone and has been replaced with ‘A’ so the filename would be wrong. I would like to be able to determine if the file has been renamed already and then proceed with processing without renaming if not necessary.

Hey Jimmy,

For future reference — it’s always a good idea to specify what app you’re working in when making help requests.

Capture One Pro turns out to be scriptable in a fairly basic way, but that might be enough to accomplish your task.

Are you working in a catalog or a session?

Is there a way to rename a file on-disk in the Finder and have that reflected in the COP working document?

-Chris

Sorry I didn’t realize I would have to get so specific at first, but in the future I will start out being specific.

Our studio works in sessions. Right now I’ve got KM renaming (deleting 3 digits/replacing with appropriate letter) processing, and color tagging. Like I mentioned, sometimes you may have to change the crop and want to reprocess the file. The easy solution is to tell photographers to hit CMD+D to reprocess at that point, but I was just looking for a solution that would allow the hotkey to be pressed again without fear of creating a wrong name. It just so happens that people are so used to the hotkey, every day someone accidentally hits it to re-process and the filename gets messed up. Our naming needs to be done in COP.

No worries.

Just keep in mind that most software is in some way idiosyncratic, therefore macros tend to have characteristics very specific to the apps they're working with (as well as more general attributes applicable to any app).

That's simple.

I've already showed you mostly how to do it.

Here's the basic methodology of doing this with Keyboard Maestro:

Rename Photo for Capture One Pro.kmmacros (7.0 KB)

I managed to figure out how to do a batch rename with AppleScript. It's a trifle on the kludgy side, but it works.

A) Get the file paths from COP.
B) Rename the files in the Finder.
E) COP will refresh its file list.

The following AppleScript will do this with 1 selected file rather than batch rename all the capture files.

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/06/01 12:04
# dMod: 2015/06/01 12:04 
# Appl: Capture One, Finder
# Task: Rename Selected File in Session
# Osax: Satimage.osax { http://tinyurl.com/dc3soh } •••• REQUIRED! ••••
# Tags: @Applescript, @Script, @Capture_One, @Finder, @Satimage.osax, @SIO
------------------------------------------------------------
# THE SATIMAGE.OSAX MUST BE INSTALLED FOR THIS TO WORK!
------------------------------------------------------------

tell application "Capture One"
  set selectedVarient1 to (get parent image of (get item 1 of (get selected variants)))
  set photoFilePosix to (id of selectedVarient1)
  set photoFileName to name of selectedVarient1
end tell

if fndBool("\\d{3}\\.[[:alpha:]]{3}$", photoFileName, false, false) of me then
  set photoFileAlias to alias POSIX file photoFilePosix
  set newFileName to cng("\\d{3}(\\.[[:alpha:]]{3})$", "A\\1", photoFileName) of me
  tell application "Finder" to set name of photoFileAlias to newFileName
else
  beep
end if

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on cng(_find, _replace, _data)
  change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
  try
    find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
    return true
  on error
    return false
  end try
end fndBool
------------------------------------------------------------

NOTE: This script is a demonstration of what is possible and is NOT a turnkey solution to your problem. A production script would need at least some error-checking routines.

Provided your renaming convention is uniform I would rather batch rename all the files at once with a script similar to this one and then move on to the other steps of your macro.

BTW: Figuring this stuff out was a pain in the behind and would have been nearly impossible had I not downloaded the Capture One Pro demo. As such it is a classic example of why as much detail as possible should to be included in a help request.

-Chris

Thanks for that response!

One more question: since this macro replaces the clipboard contents with the file name, is it possible to revert to the previous clipboard contents as part of this macro? In CaptureOne, we are using ‘Clipboard Contents’ as part of the naming convention as we shoot. The current SKU that we are working on is copied and will be on the mac’s clipboard. If someone is shooting A and B shots, the first image shot will be renamed to SKU_A, but then the next file captured will be SKU_A_555. (Clipboard Contents_3 digit counter). This would result in the B shot being named SKU_A_B.

Sure. You can use the Delete Past Clipboard action to delete the previous clipboard copy.

Be careful of race conditions thought. See Why is the clipboard not restored after any clipboard action? and Sometimes the Wrong Thing is Pasted.

If you use a couple clipboard copies in a process, a good thing to do is to add a clipboard marker into the clipboard history, and then delete until it. Something like:

  • Set Clipboard to text “KM-MARKER”
  • Do various things
  • While Clipboard is not “KM-MARKER”
    • Delete Past Clipboard: 0
  • Delete Past Clipboard: 0

If fileNameVar contains the full path of the file to open (including paths like ~/Documents/file.txt), then Open File %Variable%fileNameVar% will work.


Keyboard Maestro http://www.keyboardmaestro.com/ Editors’ Choice Award winner.
Macros for your Mac http://www.stairways.com/ http://download.keyboardmaestro.com/
Join us on the Keyboard Maestro forum http://forum.keyboardmaestro.com/