How do I make the Polarr snap associate with image files?

Polarr is an electron app packaged with Snap. Ubuntu users have been asking for the ability to open image files on with either double click or right click > open with > Polarr. I wrote the code but now it’s not working with Snap.

Problem #1: electron-builder fileAssociations isn’t working?

I followed the instructions at https://www.electron.build/configuration/configuration#PlatformSpecificBuildOptions-fileAssociations and added fileAssociations to my package.json:

package.json
{
  "name": "polarr",
  "version": "5.1.2",
  "description": "Powerful and easy-to-use photo editor.",
  "main": "main.js",
  "author": "Polarr, Inc",
  "scripts": {
    "start": "electron . --dev",
    "build:mac": "electron-packager . Polarr --name=Polarr --platform=darwin --arch=x64 --icon=icon.icns --asar --overwrite --version-string.ProductName=Polarr",
    "build:win": "node build_win.js",
    "build:linux": "node build_linux.js",
    "docker": "bash run-docker.sh",
    "snap:linux": "electron-builder",
    "build_win_zh": "node build_win_zh.js"
  },
  "build": {
    "appId": "co.polarr.photoeditor",
    "linux": {
      "icon": "icon",
      "synopsis": "Powerful and easy-to-use photo editor.",
      "description": "Used by the world's most professional portrait and landscape photographers, Polarr offers advanced auto-enhance tools and sophisticated filters to edit every detail of your photo.",
      "category": "Graphics;Photography",
      "target": [
        "snap"
      ]
    },
    "fileAssociations": [
      {
        "ext": [
          "tiff",
          "tif",
          "3fr",
          "ari",
          "arw",
          "bay",
          "crw",
          "cr2",
          "cap",
          "dcs",
          "dcr",
          "dng",
          "drf",
          "eip",
          "erf",
          "fff",
          "iiq",
          "k25",
          "kdc",
          "mef",
          "mos",
          "mrw",
          "nef",
          "nrw",
          "obm",
          "orf",
          "pef",
          "ptx",
          "pxn",
          "r3d",
          "raf",
          "raw",
          "rwl",
          "rw2",
          "rwz",
          "sr2",
          "srf",
          "srw",
          "x3f"
        ],
        "name": "Raw Image"
      },
      {
        "ext": ["jpg", "jpeg", "png", "bmp", "gif"],
        "name": "Image"
      }
    ],
    "extraResources": [
      "node_modules/sharp/vendor/lib"
    ],
    "appImage": {},
    "snap": {
      "confinement": "strict",
      "summary": "Powerful and easy-to-use photo editor.",
      "grade": "stable",
      "stagePackages": [
        "default",
        "libcanberra-gtk-module",
        "libgail-3-0",
        "libatk1.0-0",
        "unity-gtk-module-common"
      ],
      "plugs": [
        "default",
        "removable-media"
      ],
      "environment": {
        "LD_LIBRARY_PATH": "$SNAP/usr/lib:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/app/resources/node_modules/sharp/vendor/lib"
      }
    }
  },
  "devDependencies": {
    "asar": "^0.14.3",
    "electron": "^2.0.0",
    "electron-builder": "^20.8.2",
    "electron-packager": "^12.1.0",
    "electron-squirrel-startup": "^1.0.0",
    "electron-winstaller": "^2.6.4",
    "env-paths": "^1.0.0",
    "grunt": "^1.0.2",
    "grunt-electron-installer": "^2.1.0",
    "iconv-lite": "^0.4.18",
    "locate-path": "^3.0.0",
    "universalify": "^0.1.0"
  },
  "dependencies": {
    "electron-fetch": "^1.0.0",
    "get-pixels": "^3.3.0",
    "libraw": "file:libraw",
    "readable-stream": "^3.0.1",
    "sharp": "^0.20.5",
    "temp": "^0.8.3",
    "url-exists": "^1.0.3"
  }
}
  1. sudo snap install --dangerous dist/polarr_5.1.2_amd64.snap
  2. Right click a .png file in Files.
  3. Open with Other Application.
  4. View All Applications.
  5. Polarr is missing from the list.

Problem #2: Snap isn’t giving my application command line arguments?
At the beginning of main.js, I print out the command line arguments:

console.log("process.argv", process.argv)

When run my application not in a snap (./node_modules/electron/dist/electron . /path/to/image.png), the parameterized image path will be in the printed arguments array and the editor will open the image as expected. Once I snap the application, process.argv prints

process.argv [ '/snap/polarr/x2/app/polarr' ]

regardless of how many arguments are passed in to it. So opening files is somehow broken for Snap.

You only added “ext” to “fileAssociations”. For linux you need to add mimeType, which is a string of MimeTypes to associate. Here’s an example from the Eye of GNOME desktop file:

MimeType=image/bmp;image/gif;image/jpeg;image/jpg;image/pjpeg;image/png;image/tiff;image/x-bmp;image/x-gray;image/x-icb;image/x-ico;image/x-png;image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/x-xbitmap;image/x-xpixmap;image/x-pcx;image/svg+xml;image/svg+xml-compressed;image/vnd.wap.wbmp;

1 Like

Thanks kenvandine, I’ll give that a try.

I updated my package.json’s fileAssociations:

package.json fileAssociations
    "fileAssociations": [
      {
        "ext": [
          "jpg",
          "jpeg",
          "png",
          "bmp",
          "gif"
        ],
        "name": "Image",
        "mimeType": "image/bmp;image/gif;image/jpeg;image/jpg;image/pjpeg;image/png;image/tiff;image/x-bmp;image/x-gray;image/x-icb;image/x-ico;image/x-png;image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/x-xbitmap;image/x-xpixmap;image/x-pcx;image/svg+xml;image/svg+xml-compressed;image/vnd.wap.wbmp;"
      },
      {
        "ext": [
          "tiff",
          "tif",
          "3fr",
          "ari",
          "arw",
          "bay",
          "crw",
          "cr2",
          "cap",
          "dcs",
          "dcr",
          "dng",
          "drf",
          "eip",
          "erf",
          "fff",
          "iiq",
          "k25",
          "kdc",
          "mef",
          "mos",
          "mrw",
          "nef",
          "nrw",
          "obm",
          "orf",
          "pef",
          "ptx",
          "pxn",
          "r3d",
          "raf",
          "raw",
          "rwl",
          "rw2",
          "rwz",
          "sr2",
          "srf",
          "srw",
          "x3f"
        ],
        "name": "Raw Image",
        "mimeType": "image/x-sony-arw;image/x-canon-cr2;image/x-canon-crw;image/x-kodak-dcr;image/x-adobe-dng;image/x-epson-erf;image/x-kodak-k25;image/x-kodak-kdc;image/x-minolta-mrw;image/x-nikon-nef;image/x-olympus-orf;image/x-pentax-pef;image/x-fuji-raf;image/x-panasonic-raw;image/x-sony-sr2;image/x-sony-srf;image/x-sigma-x3f;"
      }
    ],

Not only is Polarr not in the default list of applications for jpeg images, but it’s not even in the “View All Applications” list. Even if it was in the list, I don’t think it would work because I can’t get command line arguments that I pass to the app when Snap packaged to be available to the Node application. When I run the application locally with electron, it does get the command line arguments that I pass to it.