Skip to content

Instantly share code, notes, and snippets.

@andrewyager
Forked from oliveratgithub/Batch File Rename.scpt
Last active July 14, 2016 07:19
Show Gist options
  • Save andrewyager/05a107d0bbc49c238c043c2a3b295c7e to your computer and use it in GitHub Desktop.
Save andrewyager/05a107d0bbc49c238c043c2a3b295c7e to your computer and use it in GitHub Desktop.
Simple AppleScript to easily batch rename multiple files sequentially for use with an PRG MBox. GUI asks user to select files and will preserve the name and extension. It as forked from another script that gave us most of the scaffolding.
-- 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
--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)
--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 "00"
else if index is less than 100 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 number of file_name_count is 1 then
--file_name-count = 1 means, we extracted only 1 text-string from the full file name. So 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 "." & item -1 of file_name_count
end if
--let's rename our file, add the sequential number from 'index' and add the file-extension to it
set old_name to name of this_file
set the name of this_file to index_prefix & index & "." & old_name as string
end repeat
--congratulations for successfully accomplishing the batch renaming task :)
display alert "All done! Renamed " & index & " files for you. Have a great day! :)"
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment