Search code examples
directoryapplescriptsubdirectoryfile-renameselected

How to rename files within selected folder using directory folder and selected folder using Applescript


I'm wanting to run a script that renames the files within a selected folder by taking the name of the directory folder of the selected folder and the 2nd word from the selected folder and adding a sequencing number to the end of the file's name.

Here is what I have so far but all that is returning is {}. Please help me to fix this.

property file_count : -1

set prefix to pad(file_count as string)
tell application "Finder"
    set theSel to item 1 of (get selection)
    set parDir to name of container of theSel
    set theName to name of theSel
    set issNum to word 2 of theName
end tell
set file_count to file_count + 1

on pad(s)
    repeat while length of s < 4
        set s to ("0" & s)
    end repeat
end pad

if file_count < 10 then
    set file_count to ("00" & file_count)
else if file_count < 100 then
    set file_count to ("0" & file_count)
end if

tell application "Finder"
    set theName to parDir & " " & issNum & "-" & file_count
    set theSubs to every file in theSel
    repeat with I from 1 to count of theSubs
        set name of every file of item I of theSubs to theName & ".jpg"
    end repeat
end tell

theName result is "(the directory file name) 001-000"

Thank you in advance l0g0p7

Edit:

as requested some examples of before and after for the file naming

Before: 08-GenerationX-1.jpg After: Generation X -001-000.jpg

(the result comes from the parent directory of the selected folder being "Generation X" the second word in the selected folder is "-001" and then the numbering sequence suffix starting at 000 plus the file extention)


Solution

  • Note of caution: Please test this script on a copy of your files, not on the originals. Renaming files in bulk is a PITA to undo if you make a mistake; it's much easier to toss the mistake out and make a new copy from the original set.

    This script should do what you want (I think). It takes the second word of the name of the selected folder, the name of its parent folder, and combines them with an index to rename the files in the folder. In line 5 it makes sure that the files are sorted by name before renaming them, to preserve file order. Let me know how it works.

    set file_index to 0
    
    tell application "Finder"
        set selected_folder to item 1 of (get selection)
        set selected_folder_path to POSIX path of (selected_folder as alias)
        set files_to_rename to (sort files of selected_folder by name) as alias list
    end tell
    
    tell application "System Events"
        set partition_directory to name of container of folder selected_folder_path
        set section_name to name of folder selected_folder_path
        set iss_number to word 2 of section_name
    
        repeat with a_file in files_to_rename
            set new_file_name to partition_directory & " " & iss_number & "-" & my zeroPad(file_index) & ".jpg"
            set name of a_file to new_file_name
            set file_index to file_index + 1
        end repeat
    end tell
    
    on zeroPad(a_num)
        return text -4 through -1 of ("0000" & a_num as string)
    end zeroPad
    

    P.s. I've redone your zero-padding routine with something a bit more efficient. I've also switched things over to System Events.app where possible. The Finder has a tendency to gum things up.