Increase filename number by 1 at the end of a file in finder (mac)

Off the bat, I've used KM quite a lot but it's mostly simple things, but what I'm wanting here is beyond my level.

I work with filenames in mac's finder that have a number at the end (but before the filename extention) and with every revision to the file, I need to increase it by 1.

e.g "document_CON1.indd" and when i do a revision it would then be "document_CON2.indd"

At the moment I have a macro that duplicates the file and then highlights the last number so I can then manually type the next number it should be;

I would ideally like this to be more automatic so I don't need to manually change the number at the end. So basically I want KM to know what the current last number is say 4, and then changes it to 5, the next number.

Any help with this would be greatly appreciated, thanks.

Working on a mac with MacOs Big Sur 11.6.7.

1 Like

Here's one way to do it:

Duplicate & Increment.kmmacros (21 KB)

Macro screenshot

1 Like

Thanks for your reply.

I just tried using it on a file but I get the below error message;

Not sure what I've done wrong. I put it in my "finder" group of macros so not sure why it didn't.

Hi, @del_84. This works for me...

DOWNLOAD Macro File:
Copy Finder Item with Incremented Name.kmmacros (8.0 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

2 Likes

Thanks for your reply Jims.

I wonder why this isn't working for me. I get this for some reason;

I don't know how to read the rest of the error message unfortunately.

Seems like you might have a permission issue with Keyboard Maestro. What version of KM are you running?

What is the name of the file you are selecting when you run the macro? Where is it located (folder path)?

Create a file named untitled.txt in your Home directory and then run the macro. Do you see the same error?

What was the file name? @noisneil's macro only works when the name ends with a digit -- ie it'll do untitled1.txt -> untitled2.txt, but not untitled.txt -> untitled1.txt.

(@_jims's version doesn't do that because he's turned off the regex action's "Abort on Failure" and "Notify" options -- local_Suffix remains unset, which he catches in the next action. Neat, @_jims!)

1 Like

Thanks so much @_jims, I tested it on my home computer at it works!

For some reason it didn't work on my work computer in the office this morning. I guess our IT people have strict permission settings or something. Hopefully I can get them to allow it.

@del_84, glad it worked for you. BTW, I just uploaded an improved version within my original post. The version I had previously uploaded would not work properly if you were viewing Finder files as List and you selected a file below the top of the hierarchy. For example:

2022-08-02 14.30.32@2x

The reason the original version failed is because this snippet of AppleScript does not return the parent directory under those circumstances:

tell application "Finder"
	set file_Parent to (folder of the front Finder window) as alias
end tell

I should have caught that; sorry. :confused:

On a positive note, both versions correctly increment the integer suffix no matter the base file selected. For example, if a folder included the following files:

a.txt
a1.txt
a2.txt

The next file created (via copy) would be a3.txt regardless of the file selected. One advantage of this behavior is that you can select one of the files and repeatedly trigger the macro if you want to create multiple copies.


For those interested, here's the full AppleScript that was in the original macro:

AppleScript-image
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set file_Prefix to getvariable "local_Prefix" instance kmInst
	set file_Suffix to getvariable "local_Suffix" instance kmInst
	set file_Extension to getvariable "local_Extension" instance kmInst
end tell

if file_Extension is not "" then
	set file_Extension to "." & file_Extension
end if

tell application "Finder"
	set file_Parent to (folder of the front Finder window) as alias
end tell

tell application "System Events"
	set file_List to get the name of every disk item of file_Parent
end tell
set x to file_Suffix
set file_New to file_Prefix & x & file_Extension
repeat
	if file_New is in file_List then
		set x to x + 1
		set file_New to file_Prefix & x & file_Extension
	else
		return file_New
	end if
end repeat

And here's the AppleScript in the corrected macro:

AppleScript-image
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set file_Parent to getvariable "local_Parent" instance kmInst
	set file_Prefix to getvariable "local_Prefix" instance kmInst
	set file_Suffix to getvariable "local_Suffix" instance kmInst
	set file_Extension to getvariable "local_Extension" instance kmInst
end tell

if file_Extension is not "" then
	set file_Extension to "." & file_Extension
end if

tell application "System Events"
	set file_List to get the name of every disk item of (file_Parent as POSIX file as alias)
end tell
set x to file_Suffix
set file_New to file_Prefix & x & file_Extension
repeat
	if file_New is in file_List then
		set x to x + 1
		set file_New to file_Prefix & x & file_Extension
	else
		return file_New
	end if
end repeat
2 Likes

Oh great, I always view as list so that improvement will be important for my workflow.

Thanks so much @_jims I really appreciate your help with this.

1 Like

My original attempt was based on the example given, where the starting file ended with a number. @_jims has already come up with a lovely solution, but for the sake of completeness, my here's my non-AppleScript approach, updated to cope with filenames that don't end with a number:

Duplicate & Increment.kmmacros (22 KB)

Macro screenshot

2 Likes