Script for batch renaming files based on kMDItemWhereFroms

I’ve googled and searched all day in script forums and could not find a script that could do this. I grabbed pieces from a few scripts I found in forums and tried to make one without applescript knowledge. I managed to rename one file based on the “Where From” in the Finder “Get Info” window. But obviously, the script is faulty. I am hoping someone can help me sort it out. I need to batch rename hundreds of files with the URL in the “Where From” info. Here’s the script.



-- get Spotlight kMDItemWhereFroms attribute
 
property getSpecific : true -- show the specific download URL?
 
set theFile to (choose file)
set aFile to quoted form of POSIX path of theFile
set theURL to (do shell script "mdls -name kMDItemWhereFroms -raw " & aFile)
if theURL is "(null)" then -- no attribute
          set theURL to "(URL info not found)"
          set theMessage to ""
else
          if getSpecific then -- get the first item (the download URL)
                    set theURL to paragraph 2 of theURL
                    set here to offset of "\"" in theURL
                    set theURL to text (here + 1) thru -3 of theURL -- the download URL
                    tell application "Finder"
                              set name of theFile to theURL --I have no idea what im doing
                    end tell
          else -- get the last item (the page/site URL)
                    set theMessage to "page/site "
                    set theURL to paragraph -2 of theURL
                    set here to offset of "\"" in theURL
                    set theURL to text (here + 1) thru -2 of theURL
          end if
end if


Model: MacPro
AppleScript: 2.2.1
Browser: Safari 537.1
Operating System: Mac OS X (10.7)

Hello and welcome to Macscripters! :slight_smile:

This should bring you a little bit further.

I have removed the http:// part, as I am not sure if those are accepted as start of filenames, and added the extension for your convenience. What was amiss, was the lack of the file object specifier in your command to rename the file.

Good Luck! :slight_smile:




-- get Spotlight kMDItemWhereFroms attribute

property getSpecific : true -- show the specific download URL?

set theFile to (choose file)
set aFile to quoted form of POSIX path of theFile
set theUrl to (do shell script "mdls -name kMDItemWhereFroms -raw " & aFile)
if theUrl is "(null)" then -- no attribute
	set theUrl to "(URL info not found)"
	set theMessage to ""
else
	if getSpecific then -- get the first item (the download URL)
		set theUrl to paragraph 2 of theUrl
		set here to offset of "\"" in theUrl
		set theUrl to text (here + 1) thru -3 of theUrl -- the download URL
		tell application "Finder"
			try
				set name_ext to name extension of theFile
				set name of file theFile to text 8 thru -1 of theUrl & "." & name_ext --I have no idea what im doing
			on error e number n
				activate
				display dialog e & " " & n
			end try
		end tell
	else -- get the last item (the page/site URL)
		set theMessage to "page/site "
		set theUrl to paragraph -2 of theUrl
		set here to offset of "\"" in theUrl
		set theUrl to text (here + 1) thru -2 of theUrl
	end if
end if

THANK YOU MY FRIEND.