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
Normally it is a bad idea to do things with items that are in the attached folder (earlier OS versions will retrigger folder actions when an item is renamed, for example), and you don't need to use a Get Selected Finder Items action since the dropped items are already passed to your workflow (also note that the input items will be a list).
It looks like you are trying to use multiple passes to add on the folder names, but you will have less of a headache if you build the base name and just deal with the number suffix. If I understood your naming scheme correctly, the following script should do the trick - it isn't very fast, but should be OK for a few items at a time.
on run {input, parameters} -- rename input Finder items (aliases) to name of containing folders
set divider to "_" -- the divider character between name pieces
set output to {} -- the result to pass on to the next action
set counter to "01" -- start suffix at one
repeat with anItem in the input -- step through each item in the input
set anItem to contents of anItem -- dereference
tell application "Finder" to if class of item anItem is document file then -- don't mess with folders or applications
set {itemParent, itemExtension} to {container, name extension} of anItem
if itemExtension is not "" then set itemExtension to "." & itemExtension
set grandParentName to name of container of itemParent
set parentName to name of itemParent
set newName to grandParentName & divider & parentName & divider & counter
set documentNames to my getExistingNames(itemParent)
repeat while newName is in documentNames -- increment counter suffix as needed
set counter to text -2 thru -1 of ("0" & (counter + 1))
set newName to grandParentName & divider & parentName & divider & counter
end repeat
set name of anItem to (newName & itemExtension)
set end of output to anItem -- alias still refers to the same file even after renaming
end if
end repeat
return the output
end run
to getExistingNames(someFolder) -- get base document names (no extensions) from a folder
set nameList to {}
tell application "Finder" to repeat with aFile in (get document files of someFolder)
set {fileName, fileExtension} to {name, name extension} of aFile
if fileExtension is not "" then set fileExtension to "." & fileExtension
set fileName to text 1 thru -((count fileExtension) + 1) of fileName -- just the name part
set end of nameList to fileName
end repeat
return nameList
end getExistingNames
Posted on Jan 10, 2012 6:49 PM