Renaming files sequentially from folder name with Automator

Asked previously about renaming files in a folder (Rename File(s) from Foldername using automator/AppleScript), and now I have a folder flow that almost works.

I have created a folder in my Downloads called DOIT (As per attachments and workflow screenshots). I drop a folder in there called "Race Track 2019" which has a few random filenames in.

What I want is to rename the files sequentially from the folder name. I have the folder name variable working (as per screenshots) but it won't rename the files.

I was after (in this example) to be left with 2 files called "Race Track 2019-1 and Race Track 2019-2"

enter image description here enter image description here


Solution 1:

Using Automator for the Folder Action

In the Automator Folder Action image further below in this answer, pay attention to the Automator actions and their settings. In particular, the Options set on the Get Value of Variable action has [√] Ignore this action's input checked.

The first action, Set Value of Variable, is set to IncomingFolder and gets filtered in a Filter Finder Items action so as to get just the folder itself in order to use a Run Shell Script action to ascertain its name and assign it to the FolderName variable in the Set Value of Variable action.

There needs to be a logical break between this Set Value of Variable action and the ensuing Get Value of Variable action so as to then filter out the folder(s) as it's the files that will be renamed, not the folder(s).

The image shown further below in this answer is of the actual working Automator Folder Action and why there is no Get Selected Finder Items action nor and green circle check marks, as it is run in the background when the Race Track 2019 folder is dropped into the DOIT folder.

In my testing:

Race Track 2019
├── cb4d4gdc.jpg
└── nn49494.jpg

Became:

Race Track 2019
├── Race Track 2019-1.jpg
└── Race Track 2019-2.jpg


Using an AppleScript script for the Folder Action

Here is another approach that uses an AppleScript script for the Folder Action.

One of its benefits is it can handle multiple folders being dropped at the same time into the target folder of the Folder Action.

Using Script Editor, save the example AppleScript code to ~/Library/Scripts/Folder Action Scripts and use Folder Action Setup to assign it to the target folder.

enter image description here

Example AppleScript code:

on adding folder items to thisFolder after receiving theseNewItems
    tell application "Finder"
        repeat with thisItem in theseNewItems
            set thisItemsProperties to properties of thisItem
            if class of thisItemsProperties is folder then
                set folderName to name of thisItemsProperties
                set theseFiles to files of entire contents of thisItem
                repeat with i from 1 to count theseFiles
                    set name of item i of theseFiles to ¬
                        folderName & "-" & i & "." & ¬
                        name extension of item i of theseFiles
                end repeat
            end if
        end repeat
    end tell
end adding folder items to

Notes:

The example AppleScript code, as coded, assumes files to be renamed have an extension. If you need to act on files not having an extension, then the example AppleScript code will need to be modified to accommodate that condition.



Screenshot of Automator Folder Action

Note: This Automator Folder Action is designed to receive a single folder at a time!

Dropping multiple folders will result in the all files having the name of the first folder dropped!

For multiple folders, see: Using an AppleScript script for the Folder Action

enter image description here



As an Automator Service/Quick Action for Finder

If you'd like you be able to select folders in Finder and have the files within renamed, then create an Automator Service/Quick Action with the settings as shown in the image of the Automator Service/Quick Action below, adding a Run AppleScript action with the example AppleScript code.

Example AppleScript code:

on run {input, parameters}
    
    tell application "Finder"
        repeat with thisItem in input
            set folderName to name of thisItem
            set theseFiles to files of entire contents of thisItem
            repeat with i from 1 to count theseFiles
                set name of item i of theseFiles to ¬
                    folderName & "-" & i & "." & ¬
                    name extension of item i of theseFiles
            end repeat
        end repeat
    end tell
    
end run

Notes:

The example AppleScript code, as coded, assumes files to be renamed have an extension. If you need to act on files not having an extension, then the example AppleScript code will need to be modified to accommodate that condition.

enter image description here



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.