How to copy filename and date to rename a folder using Automator?

I have a number of folders with images inside them that were exported from "Photos" app and I would like to rename all the folders to have the same title as the images.


I would like some help to create a workflow or app using Automator to rename all the folders.


So here's my question, how do I get the filename and creation date from the 1st image and then automatically rename the folder to be in the following format:


year-month-day - filename of image


Here's the process I am thinking:


*click application*

prompt for a list of folders to rename

I select the folders

the application will save the filename and creation date in a variable

then it will rename the folder

once complete, it will move onto the next folder until all folders have been renamed


Below are some sample images of how the folder and files look like.


Image 1: Here is the original folder

User uploaded file

Image 2: Within the folder are a list of images. Choose the first image and copy the filename and creation date.

User uploaded file

Image 3: The folder is renamed to the new format.

User uploaded file


Many thanks in advance.

MacBook Pro with Retina display, macOS High Sierra (10.13.6)

Posted on Oct 10, 2018 2:52 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 11, 2018 5:22 PM

I have tested both an AppleScript, and an Automator application solution based on that AppleScript against four different folders — each of which had five sequential filenames in them in the format you shared earlier. Both the AppleScript via Script Editor, and an Automator application successfully changed the folder names to the format that you indicated. On macOS High Sierra 10.13.6 (17G65), and on Mojave 10.14 (18A391).


If you plan to just copy/paste into Script Editor, then the following vertical and horizontally scrollable AppleScript can just be copy/pasted into Script Editor. You would click the compile (hammer) icon, and then could either just run it against a selection of folders to try it out, or save the script as an application, so you could double-click it from the Desktop. Just so you know, I have already copy/pasted this code back into Script Editor and successfully ran it against four folders with success.

-- folder_rename.applescript -- Allows user to select multiple folders, and for each folder, sorts its file content -- by name, and then pick the first filename in that list. It then builds the -- ISO8601 date string (YYYY-MM-DD) from the file's creation date, and then appends -- the extracted filename's name string preceding " - 0000n.ext". Next, it renames -- the selected folder to the newly constructed new folder name. -- Given input filename format: "New Years Day Party - 00001.jpg" whose creation date -- is 2/1/07, the new folder name will be "2007-02-01 - New Years Day Party" -- Version: 1.0 -- Tested: macOS High Sierra 10.13.6 (17G65), Mojave 10.14 (18A391). -- VikingOSX, 2018-10-11, Apple Support Communities, No warranties of any kind. property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" try -- press and hold the command key while clicking folders for multiple selection set folder_choices to (choose folder with prompt amsg default location aDesktop with multiple selections allowed without invisibles and showing package contents) on error errmsg number errnbr if errnbr = -128 then display alert "User canceled. Ending script." as critical giving up after 10 end if return end try tell application "Finder" repeat with afolder in folder_choices -- get list of files in folder, sort by name, pick the very first file set first_item to first item of (sort (get files of folder afolder whose kind contains "image") by name) as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) log aname set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set the name of afolder to newFolderName -- display dialog newFolderName as text end repeat end tell return


If you plan to use Automator Application instead, then there are some very minor changes needed in the code that are reflected below. You will need the File and Folders library : Ask for Finder Items, and the Utilities library : Run AppleScript action if on High Sierra, otherwise Finder library : Ask For Finder Items, and the Automator library : Run AppleScript if on Mojave. Drag and drop these in that order rightward onto the larger Workflow window.


In the Run AppleScript action, just select everything and remove it — before copying and pasting the following replacement content into it.

property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" use scripting additions on run {input, parameters} -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" tell application "Finder" repeat with afolder in input set first_item to first item of (sort (get files of folder afolder whose kind contains "image") by name) as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) log aname set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set the name of afolder to newFolderName -- display dialog newFolderName as text end repeat end tell return input end run


Once you have pasted this AppleScript into the Run AppleScript action, click the compile (hammer) button within the action. Save this Automator Action to your Desktop. You run it with a double-click.


Here is a screen shot of the Automator Application workflow. Right-click on the image and save it to your Desktop where you can enlarge it further in Preview.

User uploaded file

Similar questions

16 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to copy filename and date to rename a folder using Automator?

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