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)