Search code examples
applescriptrenamebatch-rename

Apple Script- Search Folder and Rename files


Im looking for some help with an addition to a larger apple script, ive seen lots of similar queries but none that quite fit the bill, so if anyone can help or direct me to an answer it would be a huge help,

I wanting to follow this general premise

`“Choose Name” default answer “” set ChosenName to text returned of result

set ImagesFolder to (choose folder with prompt “Choose Images Folder:”)`

The bit im struggling with

if the ImagesFolder contains a folder named “Image Set 1” then look through the folder “Images Set 1” and rename the contents using this logic

if file name conatins 0001_ rename file to ChosenName & “front”

if file name conatins 0002_ rename file to ChosenName & “Back”

if file name conatins 0003_ rename file to ChosenName & “Top”

if file name conatins 0004_ rename file to ChosenNamet & “Bottom”

else

if the ImagesFolder contains a folder named “Image Set 2” then look through the folder images 2 and rename the content using this logic

if file name conatins 0001_ rename file to ChosenName & “F”

if file name conatins 0002_ rename file to ChosenName & “B”

if file name conatins 0003_ rename file to ChosenName & “T”

if file name conatins 0004_ rename file to ChosenNamet & “B”

(The unqiue characters im using to identify these files are always the last characters if this helps)

Thanks P


Solution

  • This script does what you need. You need to extend it to also manage the "Image Set 2" folder and its extension name, but It will be quite easy to just duplicate what's inside the Tell "Finder" block.

    Because you have multiple folder, I used a sub-routine to process your folder, each time calling new rule. For instance the 1st rule is to process "Image Set 1, search for 0001,0002,0003,0004 and replace each with Front,Back,Top, Bottom.

    The rule 2 is to process "Image Set 2, search for 0001,0002,0003,0004 and replace each with F,B,T, B.

    The first part build the rules. The script itself is reduced to a loop through each rule, calling the sub-routine "Process_SubFolder" with the 3 variables: sub folder name, current targets and new names.

    (* 
    Define record Rule, made of 3  variables : 
       NFolderNFolder: the name of sub-folder
       NSource : the list of part of file names to be processed
       NDest : the list of new names. This list MUST count same number of items as NSource       
    All rules are added into ListRules
    *)
    global ChosenName, ImagesFolder -- mandatory to use in the sub-routine
    
    set Rule to {NFolder:"Image Set 1", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"Front", "Back", "Top", "Bottom"}}
    set ListRules to {Rule}
    set Rule to {NFolder:"Image Set 2", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"F", "B", "T", "B"}}
    set ListRules to ListRules & {Rule}
    
    
    set R to display dialog "Enter a name" default answer ""
    set ChosenName to text returned of R
    if ChosenName is "" then return -- no name selected, end of script
    
    set ImagesFolder to choose folder with prompt "Choose Images Folder:"
    repeat with aRule in ListRules
        Process_SubFolder(NFolder of aRule, NSource of aRule, NDest of aRule)
    end repeat
    -- end of main script
    
    
    on Process_SubFolder(LFolder, LSource, LDest)
        tell application "Finder"
            set SubFolder to (ImagesFolder as string) & LFolder
            if folder SubFolder exists then
                set FileList to every file of folder SubFolder -- get all files of Images Set 1
                repeat with aFile in FileList -- loop through each file
                    set FName to name of aFile
                    set NewName to ""
    
                    -- Manage extension of the file
                    if name extension of aFile is "" then
                        set NewExt to ""
                    else
                        set NewExt to "." & name extension of aFile
                    end if
    
                    repeat with I from 1 to count of LSource --loop trhough each source of the rule
                        if FName contains (item I of LSource) then set NewName to ChosenName & (item I of LDest) & NewExt
                    end repeat
                    if NewName is not "" then set name of aFile to NewName -- only if name must be changed !
                end repeat -- loop through files of LFolder
            end if -- folder exists
        end tell
    end Process_SubFolder
    

    With this structure, you can add as many rules as you want !

    Of course, I assume that you will never get twice same names in sub folder ! It is not the case in Image Set 2, where you will have 2 files with new name = ChosenNameB : it will create an error !!