1

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

5
  • Could be "comments" @user3439894 // some files do carry which URL they came from, in a field called "from".. it is not universal though.
    – anki
    Commented Feb 20, 2020 at 20:26
  • 1
    @ankii, The field a URL is stored in, as shown in the Info Sheet, is Where from: not "from", and at the metadata level its kMDItemWhereFroms and there is no ' "description" area' and why I'm asking for clarification. Yes it's probably the Comments: field, as shown in the Info Sheet, (kMDItemFinderComment as metadata) but the OP needs to be explicit and specific, not ' "description" area' which doesn't exist! Commented Feb 20, 2020 at 20:49
  • All those browsers should add the com.apple.metadata:kMDItemWhereFroms extended attribute - if not, how are you downloading the file?
    – red_menace
    Commented Feb 20, 2020 at 21:01
  • Sorry everyone. I meant comments and have made this change to the post. red_menace's "wherefroms" reveal gives me hope. I'm googling this now in search of ideas.
    – Ryan
    Commented Feb 20, 2020 at 22:47
  • Ryan, On a files Get Info sheet, if it was downloaded from the Internet, it typically will show in the Where from: field, which is the kMDItemWhereFroms metadata. Are you asking for a way to have that information replicated to the Comments: field? Commented Feb 20, 2020 at 23:01

1 Answer 1

1

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'.
3
  • There is no need in this use case to be piping the output of mdls to grep as mdls can return just the info for a given attributeName, e.g. mdls -name kMDItemWhereFroms eliminates the need to pipe to grep. Also, your tell application "Finder" block is missing its end tell. Commented Feb 21, 2020 at 13:08
  • Thanks user3439894. I've seen the -name option in the man page but I think my eyes glazed over it each time I looked so this was helpful. And thanks for pointing out the missing 'end'.
    – Mockman
    Commented Feb 21, 2020 at 14:15
  • Really appreciate all the help. I am embarrassingly a total idiot in this case. I don't use the command+i feature regularly and therefore wasn't fully aware that downloaded content already has a "where from" URL affixed to it by default. My next challenge is to FORCE myself, by way of reminder, to manually-enter the URL of certain screenshots I take on obscure websites for genealogical research. Thanks again, folks!
    – Ryan
    Commented Mar 29, 2020 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .