Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

m021478

macrumors 6502
Original poster
Nov 23, 2007
378
5
Trying to figure out how to use Automator to batch rename a bunch of Finder items using the metadata in the spotlight comments of my files to rename said files.

Any suggestions would be greatly appreciated... Thanks!
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Try this

The script works with files not with an alias. The thingy with the little black arrow on it. I included a tumbnail how it looks like. The script runs slow. I have not tested it with a lot of files. The automator action runs faster. All your spotlight comments need to be unique. I you have put in a comment like Tahiti for all your holiday pictures from your last trip to Tahiti it won't work.

Code:
set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
	set these_files to every file of folder this_folder
end tell
repeat with i from 1 to the count of these_files
	set this_file to (item i of these_files as alias)
	set this_info to info for this_file
	if visible of this_info is true and alias of this_info is false then
		-- insert actions here for: this_file
		tell application "Finder"
			get properties of this_file
			set file_name to name of this_file
			set file_name_extension to name extension of this_file
			try
				set file_comment to comment of this_file
				if file_comment is not "" then
					set name of this_file to file_comment & "." & file_name_extension
				end if
			on error errMsg number errNum
				display dialog errMsg & " " & errNum giving up after 5
			end try
		end tell
	end if
end repeat

Automator :

Get Specified Finder Items --> A folder not files
Get Folder Contents
Run Applescript with this code inside it :
Code:
on run {input, parameters}
	(* Your script goes here *)
	repeat with i from 1 to the count of input
		set this_file to (item i of input as alias)
		set this_info to info for this_file
		if visible of this_info is true and alias of this_info is false then
			-- insert actions here for: this_file
			tell application "Finder"
				get properties of this_file
				set file_name to name of this_file
				set file_name_extension to name extension of this_file
			try
				set file_comment to comment of this_file
				if file_comment is not "" then
					set name of this_file to file_comment & "." & file_name_extension
				end if
			on error errMsg number errNum
				display dialog errMsg & " " & errNum giving up after 5
			end try
			end tell
		end if
	end repeat
	--	return input
end run
 

Attachments

  • Afbeelding 2.png
    Afbeelding 2.png
    11.2 KB · Views: 494
  • Afbeelding 3.png
    Afbeelding 3.png
    12.5 KB · Views: 479
  • Afbeelding 4.png
    Afbeelding 4.png
    3.1 KB · Views: 509
  • Afbeelding 5.png
    Afbeelding 5.png
    51.3 KB · Views: 92
Last edited:

m021478

macrumors 6502
Original poster
Nov 23, 2007
378
5
Thanks so much for your help.

How can I use the script you provided to create an automator service to allow me to right-click directly on the files I'd like to rename (not a folder containing the files), and then choose the option to "rename selected files using spotlight comment?

Thanks!
 

Petroff1

macrumors newbie
Oct 15, 2012
1
0
Adding the comment to the name

This modified code puts the comment after the original name with an _

Code:
on run {input, parameters}
	(* Your script goes here *)
	repeat with i from 1 to the count of input
		set this_file to (item i of input as alias)
		set this_info to info for this_file
		if visible of this_info is true and alias of this_info is false then
			-- insert actions here for: this_file
			tell application "Finder"
				get properties of this_file
				set file_name to name of this_file
				set file_name_extension to name extension of this_file
				set file_length to (count of file_name) - (count of file_name_extension) - 1
				set file_body to text 1 thru file_length of file_name
				try
					set file_comment to comment of this_file
					if file_comment is not "" then
						set name of this_file to file_body & "_" & file_comment & "." & file_name_extension
					end if
				on error errMsg number errNum
					display dialog errMsg & " " & errNum giving up after 5
				end try
			end tell
		end if
	end repeat
	--	return input
end run
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.