Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Set Comment to "Where From" URI.scpt' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set Comment to "Where From" URI.scpt
Authored by: Rainy Day on May 14, '08 07:02:22PM
Save this script into Library/Scripts/Applications/Finder/ and you can easily invoke it from the Finder via the Script Menulet. Works on the frontmost window. Works on either a Get Info window, or a Finder window with one or more selected files:

tell application "Finder"
	if the class of window 1 is information window then -- If a Get Info window
		fetchWherefromData(item of window 1 as alias, true) of me
	else if the class of window 1 is Finder window then -- If a regular Finder window
		repeat with thisItem in the selection as list -- may have multiple files selected
			fetchWherefromData(thisItem as alias, true) of me
		end repeat
	end if
end tell
beep

(*  Grab the WhereFrom metadata; If "setComment" is true, put it into the file's comment box  *)
on fetchWherefromData(theFile, setComment)
	set theURI to do shell script "mdls -name kMDItemWhereFroms " & quoted form of POSIX path of theFile & " | perl -ne 'print if s/.*"(.*)".*$/\1/'" --  shell script to fetch metadata; using Perl to extract URI from mdls's messy output
	if setComment = true then -- Put it into the file's comment box?
		if theURI ≠ "" then -- did we get a URI?
			tell application "Finder"
				set theComment to comment of theFile
				if theComment = "" then -- if no comment exists, put in the URI
					set comment of theFile to theURI
				else if theComment ≠ theURI then -- if the comment is the URI, do nothing, else append the URI to the end of the comment
					set comment of theFile to theComment & return & theURI
				end if
			end tell
		end if
	end if
	return theURI
end fetchWherefromData


[ Reply to This | # ]