PJJPtx

Q: Automator/Applescript to Rename files when dropped in folder based on parent folder name

When a file is dropped in a folder ( ParentFolder/Folder/File.pdf )

I want to rename the file to ParentFolder_Folder_01.pdf

    --Get folder

    --Get ParentFolder

    --Check for next available number and use it.

    If ParentFolder_Folder_01.pdf exists, try _02

 

I automator, I have chosen folder action

Added 'Get selected finder items'

I have attempted to modify another sript I found here to no avail.

 

on run {input, parameters}

    tell application "Finder"

        set theFolder to input as string

        set theNameOfFolder to name of folder theFolder

        set theFiles to every file in folder theFolder

        set theFolders to every folder in folder theFolder

        my ProcessFiles(theNameOfFolder, theFiles)

        my ProcessFolders(theFolders)

    end tell

end run

 

to ProcessFolders(theFolders)

    tell application "Finder"

        repeat with thisFolder in theFolders

            set theNameOfFolder to name of thisFolder

            set theFiles to every file in thisFolder

            set theFolders to every folder in thisFolder

            my ProcessFiles(theNameOfFolder, theFiles)

            my ProcessFolders(thisFolder)

        end repeat

    end tell

end ProcessFolders

 

to ProcessFiles(NameOfOuterFolder, theFiles)

    tell application "Finder"

        repeat with thisFile in theFiles

            set theSuffix to my grabSuffixOfFile(name of thisFile)

            set name of thisFile to NameOfOuterFolder & "_" & theSuffix

        end repeat

    end tell

end ProcessFiles

 

to grabSuffixOfFile(theFile)

    set text item delimiters to "_"

    return (text item 2 of theFile)

    set text item delimiters to ""

end grabSuffixOfFile

iMac, Mac OS X (10.7.2)

Posted on Jan 8, 2012 11:25 PM

Close

Q: Automator/Applescript to Rename files when dropped in folder based on parent folder name

  • All replies
  • Helpful answers

Previous Page 2
  • by red_menace,

    red_menace red_menace Feb 2, 2013 2:35 PM in response to esq
    Level 6 (15,546 points)
    Desktops
    Feb 2, 2013 2:35 PM in response to esq

    The script statements are being placed into an Automator Run AppleScript action, where the action's input parameter contains the items from the previous action.  The variable f is the loop variable (the current item) for the repeat statement.

  • by MarcoD,

    MarcoD MarcoD Sep 23, 2016 12:00 PM in response to red_menace
    Level 1 (4 points)
    Sep 23, 2016 12:00 PM in response to red_menace

    Thank you very much, this is also exactly what I needed (without the grandparent name and number padding of 000 instead of 00, but that I was able to adapt myself). Saves me a lot of time

Previous Page 2