Skip to content

Instantly share code, notes, and snippets.

@sk22
Last active January 30, 2023 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk22/be12b13eec98839f0912de4864edc863 to your computer and use it in GitHub Desktop.
Save sk22/be12b13eec98839f0912de4864edc863 to your computer and use it in GitHub Desktop.
Generate desktop files based for Web Applications using a Snapcraft installation of Chromium
#!/bin/bash
base="$HOME/snap/chromium/current/.config/chromium/Default/Extensions"
desktops="$HOME/.local/share/applications"
chromium="/snap/bin/chromium"
# ./generator.sh ... Lists all installed Chromium Apps
# ./generator.sh [id] ... Installs Chromium app with given App ID to user applications
if [ $# = 0 ]; then
for file in $base/*/*/manifest.json; do
cat $file | grep -oP '(?<="name": ").*(?=")' | tr '\n' '\t'
id=$(echo $file | grep -oP '(?<=Extensions/).*?(?=/)')
echo $id # | tr '\n' ' '
# echo $file | grep -oP "(?<=$id/).*(?=/manifest.json)"
done
elif [ $# = 1 ]; then
id=$1
path=$(find $base/$id/* | head -1)
name=$(cat $path/manifest.json | grep -oP '(?<="name": ").*(?=")')
desktop="$desktops/chrome-$id-Default.desktop"
icon=$(ls -v $path/icons/* | tail -1) # Get largest icon, sorted using -v
echo "Name: $name"
echo "App ID: $id"
echo "Path: $path"
echo "Desktop file: $desktop"
echo "#!/usr/bin/env xdg-open" > $desktop
echo "[Desktop Entry]" >> $desktop
echo "Version=1.0" >> $desktop
echo "Terminal=false" >> $desktop
echo "Type=Application" >> $desktop
echo "Name=$name" >> $desktop
echo "Exec=env BAMF_DESKTOP_FILE_HINT=$desktop $chromium --app-id=$id" >> $desktop
echo "Icon=$icon" >> $desktop
echo "StartupWMClass=crx_$id" >> $desktop
chmod +x $desktop
fi

Background

https://bugs.launchpad.net/ubuntu/+source/chromium-browser/+bug/1732482 https://forum.snapcraft.io/t/how-to-expose-desktop-files-created-by-snaps-to-the-de/2853

Chromium has a feature that lets you add web apps to the desktop and app menu using Options -> More tools -> Create shortcut.... This is great because you can have apps for e.g. Twitter or WhatsApp appearing to be standalone apps (because the browser shell is hidden).

Sadly, at least with the Snap installation of Chromium, the generated .desktop files are broken (they have an invalid icon and open an empty Chromium window). But while the created shortcuts don't work, the apps are indeed added to the Chrome Apps (chrome://apps).

Using the script, all Chromium apps installed can be listed and proper shortcuts can be created (which are added to the user's app launchers located under ~/.local/share/applications).

Usage

To add a web app to the app drawer,

  1. Open the website
  2. Options -> More tools -> Create shortcut..., while making sure "Open as window" is ticked
  3. Delete the shortcut created on your desktop
  4. Download the script and open a terminal in the directory you downloaded it to.
  5. Run ./chromium-snap-desktop-generator.sh. It will output a list of all Chrome apps.
Instagram	maonlnecdeecdljpahhnnlmhbmalehlm
The Air Horner	mepckecgignpfokhpmhopohiikfbpnai
Google	okkolgldfknecfjnhhglfopimelbaceh
  1. Look for your web app and copy the ID printed next to it.
  2. Run ./chromium-snap-desktop-generator.sh [id], with [id] being replaced with your app's id.
./chromium-snap-desktop-generator.sh maonlnecdeecdljpahhnnlmhbmalehlm
  1. Open your app drawer and find your web app listed.

Prevent wrong grouping in dock

In order to prevent Chromium instances from being grouped with Chromium app launcher icons in the dock and to fix that Chromium opens a new icon in the dock when launched, add one line to Chromium's desktop launcher file.

Chromium's desktop file is located at /var/lib/snapd/desktop/applications/chromium_chromium.desktop, and thus you need to edit it with admin privileges (press Alt+F2 and paste the following command, or run it from the command line):

gedit admin:///var/lib/snapd/desktop/applications/chromium_chromium.desktop

The following line is missing in Chromium for Snap's desktop file:

StartupWMClass=chromium

I suggest placing it in the [Desktop Entry] group, so before [Desktop Action NewWindow].

When saved, launching Chromium from the dock won't create a second icon and make sure it's distinct from the other Chromium apps (say, the app launchers created with the script).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment