Rename Finder items using Spotlight Comments Metadata

Discussion in 'Mac Programming' started by m021478, Apr 9, 2012.

  1. macrumors 6502

    Joined:
    Nov 23, 2007
    #1
    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!
     
  2. kryten2, Apr 10, 2012
    Last edited: Apr 10, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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
     

    Attached Files:

  3. thread starter macrumors 6502

    Joined:
    Nov 23, 2007
    #3
    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!
     
  4. thread starter macrumors 6502

    Joined:
    Nov 23, 2007
  5. macrumors newbie

    Joined:
    Oct 15, 2012
    #5
    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
    
     

Share This Page