Tip: Automatically Add URL To A Downloaded File As Spotlight Comment

Also from Reddit, a nice little hack that allows you to automatically add a downloaded file’s URL as a Spotlight comment to the file itself. Spotlight comments are useful because they add metadata to a file, and they are supported across many 3rd party applications such as DEVONthink, Leap and Launchbar.

Spotlight comments are searchable, and having a URL automatically attached to a downloaded file can come in handy when you remember the website you downloaded something from, but you can’t find the file.

To automatically add a URL as Spotlight comment, fire up AppleScript Editor (located in Application/Utilities) and paste the following code:

on adding folder items to thisFolder after receiving someItems
    if (count items of someItems) ≠ 1 then return
    set itemAlias to (item 1 of someItems) as alias
    do shell script "mdls " & (quoted form of (POSIX path of itemAlias))
    set urls to the result
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"kMDItemWhereFroms"}
    try
        set urls to (text item 2 of urls)
        set AppleScript's text item delimiters to {quote}

    if (count urls's text items) ≤ 3 then
        set urls to (text item 2 of urls)
    else
        set urls to (text item 4 of urls)
    end if

    set myFile to itemAlias as string
    tell application "Finder"
        set the comment of file myFile to urls
    end tell
    on error
        set AppleScript's text item delimiters to oldDelimiters
        return -- urls don't exist
    end try
end adding folder items to

Then save the file as a Script into the Folder Action directory ~/Library/Scripts/Folder Action Script, where “~” is your user folder.

Next, open Folder Action Setup either by right-clicking on the folder where your downloaded files will end up in and selecting Services > Folder Action Setup, or manually opening the app located into Mac HD/System/Library/Core Services.

In Folder Action Setup, select the destination folder on the left and the script you just created on the right. Now, every time your browser will save a file in that folder, a Spotlight comment with the source URL will be added to the file, making it searchable in apps that support Spotlight comments.

I’ve personally tested this in DEVONthink and it works great.

Access Extra Content and Perks

Founded in 2015, Club MacStories has delivered exclusive content every week for nearly a decade.

What started with weekly and monthly email newsletters has blossomed into a family of memberships designed every MacStories fan.

Learn more here and from our Club FAQs.

Club MacStories: Weekly and monthly newsletters via email and the web that are brimming with apps, tips, automation workflows, longform writing, early access to the MacStories Unwind podcast, periodic giveaways, and more;

Club MacStories+: Everything that Club MacStories offers, plus an active Discord community, advanced search and custom RSS features for exploring the Club’s entire back catalog, bonus columns, and dozens of app discounts;

Club Premier: All of the above and AppStories+, an extended version of our flagship podcast that’s delivered early, ad-free, and in high-bitrate audio.