Monday, May 20, 2019

#1 2018-11-21 10:20:31 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 392

Renaming video files with a date followed by junk

There's nothing innovative or clever here, it is a very quick (maybe 20 minute) script, it just serves a purpose I thought it's possible someone else might find useful.

I had a lot of video (movie) files. In the file names, some had periods or underscores instead of spaces between the words, many had a bunch of junk I didn't want at the end of the name, and many had the year demarcated in different ways -
2016
- 2016
[2016]
(2016)

So I made a script to sort these out for me.

Most of the files had a year immediately preceeding the junk in  the name. So the script looks for a (probable) year and deletes everything after it, preserving extensions. It converts periods (except for a final one for an extension) into spaces, and converts underscores into spaces. It processes both files and folders.

Because most of these files were downloaded from the internet, I thought it reasonably likely that someone else would have the same sort of filename clutter and might come across this and want to use it.

It worked well on my files. Not all included the year, but on my test folder of 116 files, it fixed 101 correctly, ignored 14, and would have gotten one wrong if I hadn't caught it in preview and fixed it manually, because it put the date near the beginning.

Note I'm ignoring things as possible dates if they're prior to 1950... you might want to change that if you have older stuff. Also ignoring things after 2025, so if you're using this after 2025 (if Applescript's still a thing that runs ; ), then you might want to change that too.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

Applescript:


set rename to false


tell application "Finder"
   if rename is false then
       display dialog "The \"rename\" variable at the beginning of this script is set to FALSE, so your files will not be renamed. The script will simply preview what would be done."
   else
       display dialog "The \"rename\" variable at the beginning of this script is set to TRUE, so your file names will actually be modified!" & return & "Please click cancel and change this variable to FALSE if you would like to preview the changes first."
   end if
   set moviesFolder to choose folder with prompt "Select a folder of movies for renaming."
   set filesList to every item of moviesFolder
   set newFolderTitles to {}
   set unprocessedFolderTitles to {}
   set newFileTitles to {}
   set unprocessedFileTitles to {}
   repeat with aFile in filesList
       set theTitle to the name of aFile
       if the kind of aFile is "folder" then
           set newTitle to my format_title(theTitle)
           if newTitle ≠ false then
               set newFolderTitles to newFolderTitles & newTitle
               if rename is true then set the name of aFile to newTitle
           else
               set unprocessedFolderTitles to unprocessedFolderTitles & theTitle
           end if
       else
           repeat with i from (count of characters of theTitle) to 1 by -1
               if character i of theTitle is "." then
                   set theExtension to text (i + 1) through end of theTitle
                   set extensionlessTitle to text beginning through (i - 1) of theTitle
                   set newTitle to my format_title(theTitle)
                   if newTitle ≠ false then
                       set newTitle to newTitle & "." & theExtension
                       set newFileTitles to newFileTitles & newTitle
                       if rename is true then set the name of aFile to newTitle
                   else
                       set unprocessedFileTitles to unprocessedFileTitles & theTitle
                   end if
                   exit repeat
               end if
           end repeat
       end if
   end repeat
end tell

if rename is false then
   set previewVariable to "The folders will be renamed as follows:" & return
   repeat with anItem in newFolderTitles
       set previewVariable to previewVariable & anItem & return
   end repeat
   set previewVariable to previewVariable & return & "The following folders will not have their names changed:" & return
   repeat with anItem in unprocessedFolderTitles
       set previewVariable to previewVariable & anItem & return
   end repeat
   set previewVariable to previewVariable & return & "The files will be renamed as follows:" & return
   repeat with anItem in newFileTitles
       set previewVariable to previewVariable & anItem & return
   end repeat
   set previewVariable to previewVariable & return & "The following files will not have their names changed:" & return
   repeat with anItem in unprocessedFileTitles
       set previewVariable to previewVariable & anItem & return
   end repeat
   return previewVariable
end if





on format_title(theTitle)
   set newTitle to false
   set spacedTitle to my replace_chars(theTitle, ".", " ")
   set spacedTitle to my replace_chars(spacedTitle, "_", " ")
   
   repeat with i from 1 to count of words of spacedTitle
       set currentWord to word i of spacedTitle
       set currentWord to my replace_chars(currentWord, "(", "")
       set currentWord to my replace_chars(currentWord, ")", "")
       set currentWord to my replace_chars(currentWord, "[", "")
       set currentWord to my replace_chars(currentWord, "]", "")
       set isInteger to false
       try
           set theYear to currentWord as integer
           set isInteger to true
       end try
       if isInteger is true then
           if theYear > 1950 and theYear < 2025 then
               set newTitle to ""
               repeat with j from 1 to i - 1
                   set newTitle to newTitle & word j of spacedTitle & " "
               end repeat
               set newTitle to newTitle & "(" & theYear & ")"
               exit repeat
           end if
       end if
   end repeat
   return newTitle
end format_title

on replace_chars(this_text, search_string, replacement_string)
   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 ""
   return this_text
end replace_chars

Last edited by t.spoon (2018-11-21 10:21:03 pm)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)