PDA

View Full Version : Rename Finder items using Spotlight Comments Metadata




m021478
Apr 10, 2012, 12:33 AM
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
Apr 10, 2012, 12:35 PM
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.

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 :
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

m021478
Apr 17, 2012, 11:24 PM
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!

m021478
Apr 17, 2012, 11:57 PM
Nevermind... I got it

http://dl.dropbox.com/u/54787/Screenshots/img_20120417_095614.png

Petroff1
Oct 15, 2012, 02:34 PM
This modified code puts the comment after the original name with an _


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