File renaming with Automator

Joined
Mar 22, 2012
Messages
1
Reaction score
0
I often need to rename files according to the title of the folder they're in and then put them in a new spot or the parent directory.
That is, from:
/Users/Willis/Photos/Artifacts2008/file1.jpg
to
/Users/Willis/Photos/Artifacts2008-file1.jpg

I can do this with Automator folder by folder. But just wondering if there was any way to program Automator, create a workflow or something similar, to do this automatically (and apply to many folders at once) ? Its as if the folder name needs to be stored as a variable and added to the filename perhaps? I can't figure it out.

Cheers
 
Joined
Sep 29, 2005
Messages
37
Reaction score
2
This is a Folder Action used to rename photos or files added to a folder that it is attached too.
Just drag all photos or files into a folder and it will rename files to folder name like:

"X-Mas2010" WILL CREATE "X-Mas2010_102 - X-MAS2010_104..."

Copy this script into Script Editor
and
SAVE AS: Script
SAVE IN: "~ LIBRARY - SCRIPTS - FOLDER ACTION SCRIPTS"
~ = Your user account
or
"Macintosh HD - LIBRARY - SCRIPTS - FOLDER ACTION SCRIPTS"

Activate by Right Clicking or "Control Clicking" on Folder and choose Attach a folder action

Then drag a bunch of files to folder and it will rename them to folder name


Code:
--START SCRIPT 
on adding folder items to theFolder after receiving these_items
	set curCount to 100 as integer -- STARTING NUMBER 
	tell application "Finder"
		
		set theFolderName to name of theFolder
		
		copy the result as list to {text_returned}
		set theFolderName to text_returned
		
		set fileList to every file of theFolder
		repeat with eachFile in fileList
			set curCount to curCount + 2
			set fileExtension to eachFile's name extension
			set name of eachFile to theFolderName & "_" & curCount & "." & fileExtension
		end repeat
		------------------------- MOVE FILES -------------------------
		delay 2
		set NewFolder to (choose folder) as alias
		move every item of folder theFolder to NewFolder
		------------------------------------------------------------------
		return
	end tell
	
end adding folder items to
--END SCRIPT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top