Create a gist now

Instantly share code, notes, and snippets.

Embed
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 file_name_count to text items of (get name of this_file)
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 file_name_count to text items of (get name of this_file)
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

This comment has been minimized.

Show comment
Hide comment
@oliveratgithub

oliveratgithub Aug 18, 2015

Update August 18, 2015: the script will now add a preceding "0" to the index-number as long as the number is lower than 10 - this helps a better file name sorting in the filesystem(s) later. I.E. using "01", "02",..."10" etc. instead of "1", "2",..."10".

Owner

oliveratgithub commented Aug 18, 2015

Update August 18, 2015: the script will now add a preceding "0" to the index-number as long as the number is lower than 10 - this helps a better file name sorting in the filesystem(s) later. I.E. using "01", "02",..."10" etc. instead of "1", "2",..."10".

@sybon

This comment has been minimized.

Show comment
Hide comment
@sybon

sybon Mar 26, 2016

Dear Oliver,
Thank you for your script !
I was really happy to get such a flexible tool to bulk rename my files !

Have a nice day,

Best regards,

Sylvain

sybon commented Mar 26, 2016

Dear Oliver,
Thank you for your script !
I was really happy to get such a flexible tool to bulk rename my files !

Have a nice day,

Best regards,

Sylvain

@DarthAztek

This comment has been minimized.

Show comment
Hide comment
@DarthAztek

DarthAztek Dec 14, 2016

Thanks so much for this script! It has helped me immensely! By any chance, is there a way to extrapolate a before-and-after text file that's saved to the desktop, so that a user can reference the new file names to the old?

Thanks so much for this script! It has helped me immensely! By any chance, is there a way to extrapolate a before-and-after text file that's saved to the desktop, so that a user can reference the new file names to the old?

@oliveratgithub

This comment has been minimized.

Show comment
Hide comment
@oliveratgithub

oliveratgithub Dec 18, 2017

@jonhaloi tagging you here, because I think this AppleScript might be helpful for your question asked in my other Gist?

Oliver, I'm new to Applescript. I've been trying to name a group of files that are in a folder. I want to name the files according to a text list. For example, how would I apply a list of names (1,2,3,4,5) to a group of files in a folder whose current names are (a,b,c,d,e). I've searched the Internet and have been directed to your code above. I just can't seem to figure out how to do it. Any thoughts?
https://gist.github.com/oliveratgithub/acedf41a60f4cff1f8e1d1a777cff021#gistcomment-2132226

Owner

oliveratgithub commented Dec 18, 2017

@jonhaloi tagging you here, because I think this AppleScript might be helpful for your question asked in my other Gist?

Oliver, I'm new to Applescript. I've been trying to name a group of files that are in a folder. I want to name the files according to a text list. For example, how would I apply a list of names (1,2,3,4,5) to a group of files in a folder whose current names are (a,b,c,d,e). I've searched the Internet and have been directed to your code above. I just can't seem to figure out how to do it. Any thoughts?
https://gist.github.com/oliveratgithub/acedf41a60f4cff1f8e1d1a777cff021#gistcomment-2132226

@philippeibach

This comment has been minimized.

Show comment
Hide comment
@philippeibach

philippeibach Jan 17, 2018

Hey Oliver, this script is awesome.. super convenient. thanks a lot.
short question: what would you need to change to allow the file input via drag&drop on the app icon, instead of the prompt? I was trying, but did not get it.. only renaming all my files on the desktop to test01,... haha :)
thanks for any tip!
best, Philipp

Hey Oliver, this script is awesome.. super convenient. thanks a lot.
short question: what would you need to change to allow the file input via drag&drop on the app icon, instead of the prompt? I was trying, but did not get it.. only renaming all my files on the desktop to test01,... haha :)
thanks for any tip!
best, Philipp

@vegaomar

This comment has been minimized.

Show comment
Hide comment
@vegaomar

vegaomar Jan 20, 2018

Hi Oliver, I am quote new to Script Editor. I like the above text and seems helpful. My ask is that I want to delete the letter " " from certain file names and replace by "-". How to amend the above text to do that?

Hi Oliver, I am quote new to Script Editor. I like the above text and seems helpful. My ask is that I want to delete the letter " " from certain file names and replace by "-". How to amend the above text to do that?

@oliveratgithub

This comment has been minimized.

Show comment
Hide comment
@oliveratgithub

oliveratgithub Jan 24, 2018

@philippeibach

what would you need to change to allow the file input via drag&drop on the app icon, instead of the prompt?

That's pretty simple - I attached an additional version of the script, which you can save as "Application" from Script Editor & then drag'n'drop files onto it! :)

See Drag-n-Drop Files Batch Rename.scpt
(for the sake of time, I didn't implement a fallback to the file chooser, in case the App is opened manually)

Cheers,
Oliver

Owner

oliveratgithub commented Jan 24, 2018

@philippeibach

what would you need to change to allow the file input via drag&drop on the app icon, instead of the prompt?

That's pretty simple - I attached an additional version of the script, which you can save as "Application" from Script Editor & then drag'n'drop files onto it! :)

See Drag-n-Drop Files Batch Rename.scpt
(for the sake of time, I didn't implement a fallback to the file chooser, in case the App is opened manually)

Cheers,
Oliver

@oliveratgithub

This comment has been minimized.

Show comment
Hide comment
@oliveratgithub

oliveratgithub Jan 24, 2018

@vegaomar yes, this is possible by enhancing the AppleScript as follows:

  1. add the following code to the very bottom of the script (after end tell)
on replace_text(search_string, replacement_string, this_text)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to prevTIDs
	return this_text
end replace_text
  1. then change line #9 as shown in the following before & after
  • before:
set new_name to text returned of result
  • after:
set new_name to my replace_text(" ", "-", text returned of result)
Owner

oliveratgithub commented Jan 24, 2018

@vegaomar yes, this is possible by enhancing the AppleScript as follows:

  1. add the following code to the very bottom of the script (after end tell)
on replace_text(search_string, replacement_string, this_text)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to prevTIDs
	return this_text
end replace_text
  1. then change line #9 as shown in the following before & after
  • before:
set new_name to text returned of result
  • after:
set new_name to my replace_text(" ", "-", text returned of result)
@Lokissimo

This comment has been minimized.

Show comment
Hide comment
@Lokissimo

Lokissimo Mar 14, 2018

Oliver!

Thank you so much for this script... It is really helping me understand how scripting works.

I am trying to modify this for my specific task without avail. I would like to drop a folder on the droplet and have it name
all of the files within it sequentially as "Name of Folder" + "_" + number. So far, the following gets me the foldername:

on open theDroppedItems

tell application "Finder"
	
	set Foldername to (name of (info for theDroppedItems))
	display dialog Foldername as string
	set new_name to Foldername & "_"

But when I try to access the files in the folder using:

set all_files to (every file of Foldername as list)

I get an error! What am I doing wrong?

Thanks,

Matt

Oliver!

Thank you so much for this script... It is really helping me understand how scripting works.

I am trying to modify this for my specific task without avail. I would like to drop a folder on the droplet and have it name
all of the files within it sequentially as "Name of Folder" + "_" + number. So far, the following gets me the foldername:

on open theDroppedItems

tell application "Finder"
	
	set Foldername to (name of (info for theDroppedItems))
	display dialog Foldername as string
	set new_name to Foldername & "_"

But when I try to access the files in the folder using:

set all_files to (every file of Foldername as list)

I get an error! What am I doing wrong?

Thanks,

Matt

@oliveratgithub

This comment has been minimized.

Show comment
Hide comment
@oliveratgithub

oliveratgithub Apr 12, 2018

@Lokissimo thanks for the feedback :)

What you want to do requires some more enhancements to the Drag-n-Drop script:

  1. First question is: do you want it to allow (multiple) Folders but ALSO Files to be drag'n'dropped? => makes it even more complicated ;)
  2. When you drag a folder onto the Script-Application, you have to go "inside" the dropped folder(s) in order to process only the files WITHIN => your code snippet renames only the Folder itself

You can find a pretty good example code here:
https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ProcessDroppedFilesandFolders.html#//apple_ref/doc/uid/TP40016239-CH53-SW10

If I find the time, i might enhance the script to allow drag'n'dropping of Files & Folders alike. At a first try, I didn't get it to work properly.

Owner

oliveratgithub commented Apr 12, 2018

@Lokissimo thanks for the feedback :)

What you want to do requires some more enhancements to the Drag-n-Drop script:

  1. First question is: do you want it to allow (multiple) Folders but ALSO Files to be drag'n'dropped? => makes it even more complicated ;)
  2. When you drag a folder onto the Script-Application, you have to go "inside" the dropped folder(s) in order to process only the files WITHIN => your code snippet renames only the Folder itself

You can find a pretty good example code here:
https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ProcessDroppedFilesandFolders.html#//apple_ref/doc/uid/TP40016239-CH53-SW10

If I find the time, i might enhance the script to allow drag'n'dropping of Files & Folders alike. At a first try, I didn't get it to work properly.

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