You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator renaming files sequentially as they are added to a folder

I am currently running a pretty lengthy and looping workflow in automator with screenshots at various points. All these screenshots are going into one folder but they're replacing each other each loop. I was wondering how I can rename the files coming in as "1", "2", "3", etc., until reaching ~7500.


I'm pretty new to all this stuff so please understand that my knowledge on anything regarding Java/AppleScript/Automator is extremely limited. I messed around with folder actions but I couldn't figure out how to make it work as items were dropped in.


Thank you for the help!

Posted on Jul 14, 2022 9:45 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 14, 2022 10:10 AM

There is no trivial way to do this with Automator.


Automator does have the ability to create Folder Actions which automatically run against files dropped in a folder.


Additionally, it does have the ability to rename those files, naming them sequentially, but it's not smart about how to do this - most notably, the 'Rename Finder Items' action will rename those files, but not take account of existing files when adding the number sequence.


For example, assuming you drop three files into the folder, and they get renamed to 'file-1', 'file-2' and 'file-3'.

You then drop another file into the folder, expecting it to be renamed 'file-4', but the Automator Action will NOT do this - it simply considers it as 'file-1' since it is completely unaware of the existing file number sequence in place.

In other words, it's a once-and-done approach that cannot be used repeatedly. It's OK if you want to drop all 7500 files in at once, but that doesn't sound like what you're asking.


However, you cannot embed a Folder Action like this inside a larger workflow - Folder Actions are specifically built to react to changes to folders on disk. You could have one workflow creating your screenshots, and a separate Folder Action workflow that renames the files as they're added to the folder, but the issues above still apply.


Additionally, the problem is compounded by the very first statement in your post:


> I am currently running a pretty lengthy and looping workflow in automator


There's no way to sugar coat this. Looping in Automator sucks. It is orders of magnitude worse than any other scripting or coding language I've ever used, to the point where I usually look for any other solution if a task includes looping.


To that end, I recommend refactoring your workflow.


I would consider splitting the workflow into separate tasks - for example, the main workflow does all the work to create your screenshots, then you kick off a second workflow (not a Folder Action) to rename the files once they all exist.


Or you refactor the renaming script to have additional logic that maintains some kind of counter, or is aware of the last-used sequence number and renames the files more intelligently - this cannot be done by Automator directly, but could be achieved by an AppleScript embedded within the workflow.

The one thing that Automator does do well in this case is avoid the issue of the newly-renamed files triggering the Folder Action again - conceptually, when you rename a dropped file, the renamed file appears as a new file in the folder, and triggers the script again. Automator handles this, but other models may not. For this reason I recommend a staging folder - have the main script save the screenshots in a temp/holding folder, and have a folder action (or other script) process that folder, moving the files to another folder as they are renamed.


At the end of the day, it sounds like your workflow is complex, and you will benefit from breaking it into separate modules rather than trying to craft a single monolithic entity to do it all.


Similar questions

3 replies
Question marked as Top-ranking reply

Jul 14, 2022 10:10 AM in response to MiCoMc

There is no trivial way to do this with Automator.


Automator does have the ability to create Folder Actions which automatically run against files dropped in a folder.


Additionally, it does have the ability to rename those files, naming them sequentially, but it's not smart about how to do this - most notably, the 'Rename Finder Items' action will rename those files, but not take account of existing files when adding the number sequence.


For example, assuming you drop three files into the folder, and they get renamed to 'file-1', 'file-2' and 'file-3'.

You then drop another file into the folder, expecting it to be renamed 'file-4', but the Automator Action will NOT do this - it simply considers it as 'file-1' since it is completely unaware of the existing file number sequence in place.

In other words, it's a once-and-done approach that cannot be used repeatedly. It's OK if you want to drop all 7500 files in at once, but that doesn't sound like what you're asking.


However, you cannot embed a Folder Action like this inside a larger workflow - Folder Actions are specifically built to react to changes to folders on disk. You could have one workflow creating your screenshots, and a separate Folder Action workflow that renames the files as they're added to the folder, but the issues above still apply.


Additionally, the problem is compounded by the very first statement in your post:


> I am currently running a pretty lengthy and looping workflow in automator


There's no way to sugar coat this. Looping in Automator sucks. It is orders of magnitude worse than any other scripting or coding language I've ever used, to the point where I usually look for any other solution if a task includes looping.


To that end, I recommend refactoring your workflow.


I would consider splitting the workflow into separate tasks - for example, the main workflow does all the work to create your screenshots, then you kick off a second workflow (not a Folder Action) to rename the files once they all exist.


Or you refactor the renaming script to have additional logic that maintains some kind of counter, or is aware of the last-used sequence number and renames the files more intelligently - this cannot be done by Automator directly, but could be achieved by an AppleScript embedded within the workflow.

The one thing that Automator does do well in this case is avoid the issue of the newly-renamed files triggering the Folder Action again - conceptually, when you rename a dropped file, the renamed file appears as a new file in the folder, and triggers the script again. Automator handles this, but other models may not. For this reason I recommend a staging folder - have the main script save the screenshots in a temp/holding folder, and have a folder action (or other script) process that folder, moving the files to another folder as they are renamed.


At the end of the day, it sounds like your workflow is complex, and you will benefit from breaking it into separate modules rather than trying to craft a single monolithic entity to do it all.


Jul 14, 2022 10:47 AM in response to MiCoMc

> one issue I am still seeing is that the screenshots are replacing each other rather than being "1 copy #"


I already explain this. Assuming you're using the Rename Finder Items action to add sequential numbering, this action DOES NOT take account of existing files in the folder - it simply tries to rename the incoming files starting at 1.


You need to add additional logic or (preferably) use some other method such as AppleScript to resequence the files based on existing files in the folder.

Automator renaming files sequentially as they are added to a folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.