Script to Organise files automatically and rename file

Hi all! I’m superrrrr new to scripting and was wondering if there is a script that will file my PDF files automatically and then have said file take on the folder name it is added to. More detailed breakdown below:

Step 1: Add files to parent folder
Step 2: File with the same name at the beginning will be added to subfolder that matches closest to file
Step 3: File will then take on the same name as its subfolder

It’s worth noting that I’m currently using Hazel to perform the first 2 steps without issue it’s just Step 3 that is causing a problem.

Any help would be incredibly appreciated

Hello Jake!,

This is not pretty code, but this will evaluate the folders names to determine the best name match ( folder name contains the file name elements, but no order check is performed ), moves, and then renames the files.

You can attach this as a folder action script to your folder and it will operate on all new files added to the top level.

    use AppleScript version "2.4" -- Yosemite (10.10) or later
    --use framework "Foundation"
    use scripting additions
    
    on adding folder items to theAttachedFolder after receiving theFiles
        repeat with aFile in theFiles
            sortFile(aFile)
        end repeat
    end adding folder items to
    
    
    on sortFile(aFile)
        tell application "Finder"
            --return properties of aFile
            set thisFileName to name of aFile
            set nameExtension to name extension of aFile
            set nameRoot to text 1 thru -(((length of characters of nameExtension) + 2)) of thisFileName
            set containerName to name of container of aFile --top level folder containing sort folders
            set sortFolderList to (every folder of (container of aFile)) as alias list
            set sortFolderNameList to name of (every folder of (container of aFile))
        end tell
        set sortFolderData to {}
        set AppleScript's text item delimiters to {"-"}
        repeat with i from 1 to length of sortFolderList
            set thisSortFolder to (item i of sortFolderList)
            set thisSortFolderName to (item i of sortFolderNameList)
            copy text items of thisSortFolderName to thisSortFolderNameComponents
            set the end of sortFolderData to {item i of sortFolderList, thisSortFolderNameComponents, 0}
        end repeat
        sortFolderData
        set AppleScript's text item delimiters to {"-"}
        copy text items of nameRoot to nameComponents
        copy text items of containerName to containerNameComponents
        --loop through possible target folders and check name component matches
        set matches to 0
        repeat with i2 from 1 to length of sortFolderData
            set thisSortFolderData to item i2 of sortFolderData
            set thisSortFolderNameComponentsList to (item 2 of thisSortFolderData)
            repeat with i3 from 1 to length of nameComponents
                set thisNameComponent to item i3 of nameComponents --repeat with i3 from 1 to length of thisSortFolderNameComponentsList
                if thisSortFolderNameComponentsList contains thisNameComponent then
                    set matches to matches + 1
                end if
            end repeat
            set item -1 of (item i2 of sortFolderData) to matches
            set matches to 0
        end repeat
        --loop through rated sort folders and choose best match
        set highestMatch to 0
        repeat with i2 from 1 to length of sortFolderData
            set thisSortFolderData to item i2 of sortFolderData
            set thisSortFolderMatches to item 3 of thisSortFolderData
            if thisSortFolderMatches > highestMatch then
                set highestMatch to thisSortFolderMatches
                set targetSortFolder to item 1 of thisSortFolderData
            end if
        end repeat
        --Place file in sort folder and rename
        tell application "Finder"
            set aFile to aFile as alias
            set targetFolderName to (name of targetSortFolder)
            move aFile to targetSortFolder
            --Having difficulty renaming this moved file. Make a new path and convert to alias 
            set newFilePath to (targetSortFolder as text) & thisFileName
            set newFileAlias to newFilePath as alias
            set the name of newFileAlias to (nameRoot & "-" & (word -1 of targetFolderName) & "." & nameExtension)
        end tell
    end sortFile

In all honesty, I thought this thread was dead and as going to be ignored but you have just restored my faith in humanity.

Just tested it and it does exactly what I want it to do. THANK YOU SO MUCH!

They say never meet your heroes, but Paul, I hope I meet you one day! LEGEND!

1 Like