How to set the default program to open a certain file type in a certain folder?

As well as being a Panda I am also an artist, I make my artwork mostly in GIMP and save my pictures to a certain directory on my computer. However I have a slight inconvenience because I save my images in jpeg format, however the default program to open jpegs is the Image Viewer, and if I change it to GIMP for convenience with my artwork, it means that all the other jpegs on my computer open in GIMP, and I don't want them to so this is where the problem begins...

So I am wondering if there is any way to make it so that all jpegs opened in that certain directory on my computer open in GIMP, but all other jpegs elsewhere open in the Image Viewer?


You need a desktop file, and a wrapper script:

  1. The desktop file

    • Create a desktop file

      nano ~/.local/share/applications/jpeg-wrapper.desktop
      
    • Add the configuration below (change the property for Name)

      [Desktop Entry]
      Name=Special or Standard
      Comment=Open a JPEG depending of the path
      Exec=/home/user/bin/jpeg-wrapper %f
      Icon=
      Terminal=false
      Type=Application
      Categories=Editor;
      StartupNotify=true
      MimeType=image/jpeg;
      
    • Replace user in Exec=/home/user/bin/jpeg-wrapper %f with your username, the output of

      echo $USER
      
    • Replace the icon name in Icon= with a name or path of your choice

    • Use MimeType=image/jpeg to specify the mime types of files for which the decision is to apply. Separate multiple mime types via ;

      Get the mime type via

      mimetype your_file
      

      e.g. for a text file

      text/plain
      
  2. The wrapper script

    • Create a new script

      mkdir -p ~/bin
      nano ~/bin/jpeg-wrapper
      
    • Add the code below

      #!/usr/bin/env bash
      image_path=$(dirname "$1")
      my_special_path="$HOME/tmp"
      open_with_special="gimp"
      open_with_standard="eog"
      
      if [[ $(mimetype -b "$1") == "image/jpeg" ]] && [[ "$image_path" == "$my_special_path"* ]]; then
              "$open_with_special" "$1"
      else
              "$open_with_standard" "$1"
      fi
      
    • Change my_special_path to your artwork folder. All subfolders are also considered.

    • Change open_with_special to your special application (e.g. gimp)

    • Make your wrapper script executable

      chmod +x ~/bin/jpeg-wrapper
      
  3. Restart Unity/GNOME Shell, for the GNOME Shell e.g. Alt-F2, type r and Enter

  4. Associate one or more file types with the desktop file

    • Open your file manager and right click on a file for which the decision is to apply

    • Click Properties

    • Activate the tab Open With

    • Select the entry Special or Standard

    • Click Set as default

  5. Enjoy ;)

Script checked here.