Question

I'd love to have all website URL files on the desktop take the favicon of the corresponding web page, rather than the Safari icon.

This would mimic the function of adding a page to the home screen of iOS.

How can I set something up, perhaps in Automator, so that, when I drag a new URL from Chrome to the desktop, the page's favicon is fetched and applied as the icon image?

Was it helpful?

Solution

Disclaimer: This answer is being presented strictly as a proof of concept in that it has had limited testing, shell script contains limited error handling, and requires the use of a third-party shell script to apply the favicon to the .webloc file(s).


Note that the third-party shell script is making use of native macOS utilities so it should inherently be safe to use. I did review the code within the third-party shell script and felt comfortable using it as I saw nothing ominous within.

Third-party shell script: fileicon -- I used the manual installation instructions on the linked web page.


That said, you would need to assign a Folder Action to the destination folder to which the URL dragged from the browser, or file, is dragged and dropped.

The Folder Action would then trigger e.g. a shell script that would extract the domain name from the URL within the .webloc file to then download an e.g. .png file of the website's favicon and then set it as the icon of the .webloc file.

Retrieving the favicon is done using curl and a Google URL designed for that.

You can use Automator to create a Folder Action workflow using a Run Shell Script action, with settings as shown in the image further below.

Replace the default code in the Run Shell Script action with the following example shell script code:

for f in "$@"; do
    [ -f "${f}" ] || continue
    [[ ${f} =~ ^.*\.webloc$ ]] || continue
    domain="$(/usr/libexec/PlistBuddy -c "Print :URL" "${f}" | awk -F '/' '{print $3}')"
    /usr/bin/curl "https://www.google.com/s2/favicons?sz=64&domain_url=${domain}" -o "/tmp/favicon.png"
    /usr/local/bin/fileicon set "${f}" "/tmp/favicon.png"
    [ -e "/tmp/favicon.png" ] && rm "/tmp/favicon.png"
done

Automator Folder Action


Notes:

What the shell script code in the Run Shell Script action is doing for each item dropped in the target folder that has the Folder Action set:

  • Checks the object dropped is a file, or continues to the next item if more than one was dropped.
  • Checks the object dropped ends with .webloc, or continues to the next item if more than one was dropped.
  • The third line of code within the for loop sets the value of the domain variable to that which is between // and the following / in the URL of the .webloc file.
  • Downloads the target website's favicon as a .png file.
  • Set the downloaded favicon.png file as the icon for the .webloc file using the third-party fileicon shell script.
  • Deletes the downloaded favicon.png file.

More about the domain variable:

Using a .webloc file created for this question, its contents is, e.g.:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>URL</key>
    <string>https://apple.stackexchange.com/questions/411944/how-to-auto-change-all-new-webloc-files-icons-to-web-page-favicon</string>
</dict>
</plist>

The PlistBuddy -c "Print :URL" "${f}" portion, of the compound command, returns:

https://apple.stackexchange.com/questions/411944/how-to-auto-change-all-new-webloc-files-icons-to-web-page-favicon

Which is piped to awk and returns e.g. apple.stackexchange.com, and through command substitution $(command), which allows the output of a command to replace the command itself, sets it as the value of the domain variable to then be used in the URL of the subsequent curl command.


More about the URL used in the curl command:

Google provides a method by which to retrieve a .png file of a website's favicon and as coded, it retrieves a 64 pixel size of the favicon as set in sz=64 segment of the Google URL. This can be changed, however anything higher the 64 is probably going to be too blurred and would not look as nice. Even 64 in some cases may be too blurred. If this becomes an issue set it to 32, although by the nature of typical favicon files, which are 16x16 pixels, there will always be issues when increasing its size.


Removing the applied favicon:

If you do not like the favicon that was applied to the .webloc file, or just want to remove it, you can remove it from its Get Info window by selecting the target file in Finder and press ⌘I, and then select the icon and press the delete key.

You can also, in Terminal, use fileicon to remove it, e.g.:

fileicon rm /path/to/filename.webloc
  • The command above assume fileicon is within the PATH passed to the shell, otherwise use e.g.: /path/to/fileicon

Working with the Folder Action afterwards:

If after you have initially created and used the Folder Action and would like to edit/stop/start/remove the Folder Action, this can be done using the Folder Action Setup utility. Press ⌘Spacebar to bring up Spotlight and start typing Folder Action Setup until it appears, which you can then press enter, or click it the list, to open it.

The UI of the Folder Action Setup utility should be self-explanatory and easy to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top