Automate Attaching Serialize as a Folder Action

Caveats first!

  1. I’m NOT a programmer. I’ve used AI to write this AND fuss with it to get it to work, so use it at your own risk.
  2. While you can ask me for help with this, I probably can’t troubleshoot it for you. Use AI if you need help, or ask the actual knowledgeable folks here on the forum.
  3. Read 1 & 2 again and do not use this unless you understand and agree with the caveats.

I did this to handle an automation issue; it works well for me, and that’s why I’m sharing it.

Here are a couple of AppleScripts that together will serialize new files added to a folder. I add this Folder Action to many folders and hence the saving and utility.

Once attached to a folder, each new file added to it will be renamed by adding a two-digit serial number, e.g., 01 Test.doc, 02 New File.xls, 03 My Dog.png, etc., to the beginning of the file name.

Run as an AppleScript action in the Finder, you can select a folder and, with one click, attach the Folder Action to it. It’s a lot easier than going through all the steps to set up a Folder Action via the contextual menu route Apple provides.

This “Serialize New Files.script” file goes here: Library/Scripts/Folder Action Scripts/Serialize New Files.scpt

-- Rename new files to: "NN OriginalName" (NN = 01, 02, 03, ...)
on adding folder items to this_folder after receiving added_items
	-- Compute current max NN once per batch
	set maxIndex to 0
	tell application "Finder"
		set existingFiles to every file of this_folder
		repeat with f in existingFiles
			set nm to name of f
			if (my isSerializedName(nm)) then
				try
					set nVal to (text 1 thru 2 of nm) as integer
					if nVal > maxIndex then set maxIndex to nVal
				end try
			end if
		end repeat
	end tell
	
	-- Rename each newly added item
	repeat with anItem in added_items
		try
			tell application "Finder"
				set theAlias to (anItem as alias)
				set nm to name of theAlias
				if not (my isSerializedName(nm)) then
					set maxIndex to maxIndex + 1
					set prefix to my twoDigit(maxIndex)
					set newName to (prefix & " " & nm)
					
					-- Avoid accidental collisions
					set candidate to newName
					set suffix to 1
					repeat while (exists file candidate of this_folder)
						set candidate to prefix & " " & nm & " (" & suffix & ")"
						set suffix to suffix + 1
					end repeat
					
					set name of theAlias to candidate
				end if
			end tell
		end try
	end repeat
end adding folder items to

on isSerializedName(nm)
	if (length of nm) < 3 then return false
	set c1 to character 1 of nm
	set c2 to character 2 of nm
	set c3 to character 3 of nm
	return (my isDigit(c1) and my isDigit(c2) and c3 = " ")
end isSerializedName

on isDigit(ch)
	set idv to (id of ch)
	return idv ≥ 48 and idv ≤ 57 -- "0".."9"
end isDigit

on twoDigit(n)
	set t to "0" & (n as string)
	return text ((length of t) - 1) thru -1 of t
end twoDigit

I vaguely remember that the Folder Action Scripts folder may not exist and that you MAY need to create it and its parent folder in your Library folder if it doesn’t exist.

Here's the AppleScript Action as a macro:

26)Attach Serialize to Folder.kmmacros (18 KB)

Hopefully, this will provide you with some ease and spur some creativity.

Let the AI kvetching begin!

(Beginning with a caveat and ending with kvetching, what did you expect?)

I'm guessing you can't guarantee that all items in the folder are serialised? If you can, a shortcut could be:

set maxIndex to (count of items of this_folder) - (count of added_items)

And a fun way to do the 'is the name serialised", doing away with isDigit() as well, is:

on isSerializedName(nm)
	try
		return ((word 1 of nm) + 0) < 99
	on error
		return false
	end try
end isSerializedName

Though purists will probably complain about using on error in such a way!

Any particular reason why you stepped away from KM to do the renaming?

Didn't know it was an option to do through Keyboard Maestro, or I would have.

It's also fun to get the AI to let me do it and walk me through it. It feels empowering to have a way to get what I want without having to get someone to do it for me.

Totally get that!

For completeness -- the KM method could be similar to your AS. The main differences would be the start -- on a multi-item drop KM's "Folder" Trigger will fire once for each item added while AS will process a list of the items -- and that KM's lack of functions would mean you'd have to either inline everything or use subroutines/submacros. But KM having regex makes some things easier.

This isn't exhaustively battle-tested, but seems to do what you want. And because they're the KM Actions you know and love you can easily tweak it to not serialise folders, skip any .jpg files, add different prefixes depending on the folder dropped into, and so on.

Serialise Item Added to Folder.kmmacros (10.1 KB)

Image

I think the only sneaky trick is ignoring failures in the regex search:

This is normally "A Bad Idea" in a "For Each" loop because after a failed match Local_number still has the value of the last successful match -- you'd usually have to cater for that event. But we don't care in this case because all that will happen is the "old" number gets compared to currentMax a second (or third, or fourth...) time, the "If" will be false, and nothing will happen.

The rest is pretty straight-forward, but shout if you've any questions.

1 Like