Skip to content

Instantly share code, notes, and snippets.

@oliveratgithub
Last active March 2, 2024 17:05
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save oliveratgithub/b9030365c9ae483984ea to your computer and use it in GitHub Desktop.
Save oliveratgithub/b9030365c9ae483984ea to your computer and use it in GitHub Desktop.
Simple AppleScript to easily batch rename multiple files sequentially. GUI asks user to select files and input a name before renaming.
-- This code comes from https://gist.github.com/oliveratgithub/
-- Open in AppleScript Editor and save as Application
-- ------------------------------------------------------------
--this is required to break the filename into pieces (separate name and extension)
set text item delimiters to "."
tell application "Finder"
set all_files to every item of (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
display dialog "New file name:" default answer ""
set new_name to text returned of result
--now we start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
--the 'index' number is of course required for the sequential renaming of our files!
repeat with index from 1 to the count of all_files
--using our index, we select the appropriate file from our list
set this_file to item index of all_files
set {itemName, itemExtension} to {name, name extension} of this_file
--if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
if index is less than 10 then
set index_prefix to "0"
else
set index_prefix to ""
end if
--
--lets check if the current file from our list (based on index-number) has even any file-extension
if itemExtension is "" then
-- "" means there is no file-extension present.
set file_extension to ""
else
--yup, we are currently processing a file that has a file-extension
--we have to re-add the original file-extension after changing the name of the file!
set file_extension to "." & itemExtension
end if
--let's rename our file, add the sequential number from 'index' and add the file-extension to it
set the name of this_file to new_name & index_prefix & index & file_extension as string
end repeat
--congratulations for successfully accomplishing the batch renaming task :)
display alert "All done! Renamed " & index & " files with '" & new_name & "' for you. Have a great day! :)"
end tell
-- This code comes from https://gist.github.com/oliveratgithub/
-- Open in AppleScript Editor and save as Application
-- ------------------------------------------------------------
--this is required to break the filename into pieces (separate name and extension)
set text item delimiters to "."
-- Let the App receive drag'n'dropped files
on open theDroppedItems
-- build the file list based on the dropped files
set all_files to every item of theDroppedItems as list
tell application "Finder"
display dialog "New file name:" default answer ""
set new_name to text returned of result
--now we start looping through all selected files. 'index' is our counter that we initially set to 1 and then count up with every file.
--the 'index' number is of course required for the sequential renaming of our files!
repeat with index from 1 to the count of all_files
--using our index, we select the appropriate file from our list
set this_file to item index of all_files
set {itemName, itemExtension} to {name, name extension} of this_file
--if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
if index is less than 10 then
set index_prefix to "0"
else
set index_prefix to ""
end if
--
--lets check if the current file from our list (based on index-number) has even any file-extension
if itemExtension is "" then
-- "" means there is no file-extension present.
set file_extension to ""
else
--yup, we are currently processing a file that has a file-extension
--we have to re-add the original file-extension after changing the name of the file!
set file_extension to "." & itemExtension
end if
--let's rename our file, add the sequential number from 'index' and add the file-extension to it
set the name of this_file to new_name & index_prefix & index & file_extension as string
end repeat
--congratulations for successfully accomplishing the batch renaming task :)
display alert "All done! Renamed " & index & " files with '" & new_name & "' for you. Have a great day! :)"
end tell
end open
@oliveratgithub
Copy link
Author

Hi Oliver. is there any way to make the script to run once a day, go to selected folders and to rename the files in those folders to contain a specific string at the very beginning of the file name UNLESS they already contain such string?

Hey @PetrKGitHub
Sure something like this would be possible, but it incorporates some additional logics, starting already with a scheduler mechanism. I think you could put something together with "crontab" (if you're technically skilled) or a macOS app giving you a UI to control repeating jobs. The rest needs either a reworked Apple Script or at least 1 additional / preceeding Script.

Overall however, it's not a simple solution and has many "moving pieces" that I can cover here :)

@laneharding
Copy link

Hi Oliver,

Thank you for the code! I love how easy it is to use. I know it would take some extra manipulation, but is there a way to have it reference a document with a list of names to use instead of using one word and putting a number at the end? Thank you in advance for your help.

@oliveratgithub
Copy link
Author

I know it would take some extra manipulation, but is there a way to have it reference a document with a list of names to use instead of using one word and putting a number at the end?

@laneharding Sure that’s possible, it requires some rework with additional code though. Here‘s a good discussion about just getting every line of a text file (.txt) into a dynamic list within the script: https://stackoverflow.com/a/48967981/5750030

The question is, if this text file will always be read from the same location, or you‘d choose it manually when the script runs? Also consider error handling when the file happens to be empty. Or what if the number of names in the text file don’t add up to the number of selected files to be renamed?

Assuming you manage to add code to read the lines from text file into a list within the script, definitely you‘d have to disable/remove lines 8 and 17-21. Then adding a repeat, looping through modifying line 9 (set new_name) to take the first name from the text file list, but making sure the next file would take also the 2nd name from the list, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment