Script or App to auto write URL of browser-downloaded files to their respective Comments field (metadata) in the files Get Info sheet

applescriptfinderfirefoxgoogle-chromesafari

I'm looking for a script or any other way to automatically write the URL of a download file to its "Comments" field, as shown in a files Get Info sheet (Command+I in Finder).

Anyone know if I could make a script to do this, or if browser extensions can do this, etc?

Equally fine, for my needs anyway, would be to simply have the files Get Info sheet show me the URL in a separate field, i.e. one that displays whatever is in com.apple.metadata:kMDItemWhereFroms

Best Answer

I'm not sure what you mean by 'automatically' but this script will take the 'where froms' URL and stuff it into the Comments of (single) selected file's Finder Info window (i.e. when you type command-i on a file).

property fPath : path to downloads folder
tell application "Finder"
set aFil to selection as alias

set AppleScript's text item delimiters to {"\""}
try
    set sCom to do shell script "/usr/bin/mdls -name kMDItemWhereFroms " & quoted form of (POSIX path of fPath & (name of aFil))
    set rUrl to second text item of sCom

    set comment of aFil to rUrl
end try
set AppleScript's text item delimiters to {""}
end tell

Essentially, this runs the 'mdls' command, greps the result for 'kMDItemWhereFroms' and then grabs the URL from that key's value, which is then set into the 'comments' field.

Some considerations:

  • If a file doesn't have 'kMDItemWhereFroms' then nothing happens
  • If file isn't in 'Downloads' folder then probably nothing happens
  • Existing 'comments' are replaced. If you want the URL appended or whatever, then more scripting would be required.
  • It's conceivable that the structure of the mdls output could be different which could break the grep. It works for me on 10.12 using Safari. Not sure if other browsers reliably provide 'where froms'. The text item delimiters separate at the '"' and the text between the first two quote marks is deemed the URL.
  • If you want it to 'automatically' occur upon the file's download, then you'd need to do something like 'folder actions'.