Rename Files Using a Simple Microsoft Excel or Apple Numbers Spreadsheet or a Text Table in BBEdit

Hello Dear Keyboard Maestro Fellows,

I have found this promissing macro by JMichaelTX (may he rest in peace) to rename PDF-files named 1.pdf, 2.pdf - 349.pdf (I don't know the exact number right now).

I have changed the columns in the Excel file, but I do not know how to change the regular expression to capture the filenames?

Can anyone help me with this, please?
Best, Matze

Hey @Matze,

If you'll provide an example worksheet for Excel with 10 or so records, I'll take a look at Jim's macro.

-Chris

Hi Chris
I actually wanted to use the worksheet from Jim's Macro and only adjust the relevant lines there. the lines are:
1
2
3
4
(pdfs titles to be renamed)
and

Prename Name A_1
Prename Name A_2
Prename Name A_3
Prename Name A_4
(renamed pdf titles)

Thank you!!
edit: how do I upload an Excel sheet?
ParticipantIDs 2.xlsx.zip (7.6 KB)

Just zip it and drag it into the Keyboard Maestro Forum editor.

1 Like

Done.

So – you're using the ID Column as the current file name and the ParticipantID Column as the new file name column – yes?

I'm going out for breakfast, so it'll be a while before I get back to this.

Ups sorry no, I've pasted wrong. I will upload you a new sheet.

Have a nice and healthy breakfast. ID column is the actual name and Participant ID Column is the new name

ParticipantIDs 2 2.xlsx.zip (7.6 KB)

1 Like

Okay, I'll take a look when I get back.

Is there any specific reason you're keeping Jim's original data structure?

I'd think it would be simpler to just have OLD NAME and NEW NAME columns along with any other desired data in the worksheet.

I just did not want to make anything wrong :wink:

1 Like

Hey @Matze,

Okay...

My first thought is that it's really a lot of trouble to fool around with Jim's very complicated macro without need – better to simplify if possible.

The task would be better done with a basic Excel worksheet or better yet with BBEdit and a more simple text table.

I recommend BBEdit because…
  • Although BBEdit is a commercial product, the trial version reverts to a
    still very powerful and free (BBEdit-Lite) version after 30 days.
  • It is very Mac-like.
  • It has excellent support for RegEx.
  • It is very AppleScriptable.
  • It has a two versatile command line tools.
  • It has been in continuous development for over 25 years.

A text file is much easier to work with an manage than a spreadsheet, unless of course you're very familiar with the spreadsheet software.

image

By using BBEdit our data acquisition script becomes this simple:

# Acquire rename table.
tell application "BBEdit"
   tell front text window
      set renameTable to contents of lines whose contents is not ""
   end tell
end tell

set renameTable to items 4 thru -1 of renameTable

Although using a simpler Excel workbook also gives us a much simpler data acquisition script:

image

# Acquire rename table.

tell application "Microsoft Excel"
   tell active sheet of active workbook
      set valuePairList to value of used range
   end tell
end tell

if item 1 of valuePairList contains "Old File Name" then
   set contents of (a reference to item 1 of valuePairList) to 0
end if

set valuePairList to lists of valuePairList

From either one of these places it becomes pretty straightforward to iterate through the old-name ⇢ new-name pairs and rename the files.

I can do either of these pretty easily.

The remaining question for you is where do you want the the target folder to be?

  • Do you want the front Finder window?
  • Do you want a specific folder path?
  • Do you want to pick the folder on the fly?

In general I think it's better to work with the file extension in the rename list, but that's not absolutely necessary.

Some people choose to hide file extensions in the Finder, and that can affect what you copy out of the Finder.

I think this is a bad idea, but YMMV.

Let me know what direction you want to go in.

-Chris

1 Like

Hello Chris, I am a little ashamed that I caused such a fuss with this question. Thank you all the more for your solution. It's evening here with us right now. I'll get to work on it tomorrow morning and get back to you. Thank you very much in advance. Matze

Don't be – it's a perfectly reasonable question. It's just that Jim's solution here was designed around a specific user's request and is over complicated as a general solution.

I've been meaning to post a more general solution anyway.

I've provided a couple of building blocks but no solution as yet.

Hey @Matze,

Alright... Here's a preliminary proof-of-concept solution using Excel and a very simplified old-name ⇢ new-name worksheet.

I noticed in your Excel worksheet that you were keeping the digit(s) from the old name and transferring them to the new name with an underscore.

I'm not sure that's what you really want to do, but I've followed suit in the macro.

Old Name New Name
1.pdf Kroell_1

** Note that I've added the file suffix to the Old Name.


The format for the Excel rename template:

Old Name New Name
1.pdf Kroell
2.pdf Schroeder
3.pdf Vollmer

Output:

Kroell_1.pdf
Schroeder_2.pdf
Vollmer_3.pdf

REQUIRED!

  • Excel must be running.
    • It may be in the background but cannot be hidden using the Hide menu command.
    • The rename template file must be the frontmost window.
  • The macro is currently in a Finder macro group.
    • Don't forget to activate the macro after importing it – and make sure its parent macro group is also activated.
    • Change the hotkey trigger as desired.

Rename Files in Front Window Given an Open Rename List Template in Excel v1.00.kmmacros (8.9 KB)
File Rename Template.xlsx.zip (7.5 KB)
Rename_test_demo_files.zip (11.3 KB)

Macro Image

Keyboard Maestro Export

-Chris

1 Like

Good Morning, dear Chris,

I did run the macro.

Excel Sheet open in background gives the following error-message:

Bildschirm­foto 2023-01-14 um 08.45.22

Excel Sheet open as the frontmost window and running the macro via hotkey gives no result at all.

Edit: When the folder is open, and I run the macro via hotkey, I get the same error message.

Best Matze

I've made two simple macros. :smiling_face: One copies each name of the list of old names to "Copy 'Em". The other selects the old files in the Finder one by one and pastes the clipboards via Finder Rename.

The target window in the Finder must be open and frontmost.

What system are you running?

Try this on a dummy file or folder – run it from Apple's Script Editor.app:

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   set theItem to item 1 of finderSelectionList
   
   set name of theItem to "NUTS!"
   
end tell

It should rename the selected item. Let me know what it does.

1 Like

Hi Chris,

My Mac is running Ventura 13.0.1

Running your Apple Script renames the selected file but only when a file is selected.

I have figured out why your first macro did not work. Excel Sheet was on an external monitor! If both Finder window and Excel Sheet are in the main monitor it works!

Thank you. Matze

Correct. The script operates on the selected item in the Finder:

set finderSelectionList to selection as alias list

That's very odd...

Open Excel – put the template workbook on your external monitor – and run this script in Apple's Script Editor.app.

  • Make sure the template window is frontmost in Excel.
  • Make sure Excel is not hidden.
tell application "Microsoft Excel"
   return name of windows
end tell

What is the result?

Hey Chris,

The result is: nothing happens – no matter which monitor the sheet is frontmost.

Well, that makes no sense at all.

Hmm... Try this:

  • Select Microsoft Excel in the Finder.
  • Run this script.
    • Post the result.
tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   set theItem to item 1 of finderSelectionList
   return (properties of theItem)
end tell