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?
Best Answer
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:
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:
for
loop sets the value of thedomain
variable to that which is between//
and the following/
in the URL of the .webloc file.fileicon
shell script.More about the
domain
variable:Using a .webloc file created for this question, its contents is, e.g.:
The
PlistBuddy -c "Print :URL" "${f}"
portion, of the compound command, returns: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 thedomain
variable to then be used in the URL of the subsequentcurl
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 the64
is probably going to be too blurred and would not look as nice. Even64
in some cases may be too blurred. If this becomes an issue set it to32
, although by the nature of typical favicon files, which are16x16
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
is within thePATH
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.