21

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?

Braiam
  • 67,791
  • 32
  • 179
  • 269
  • Only for Nautilus/Files or also for xdg-open? – A.B. Dec 28 '15 at 12:43
  • @A.B.: I would say both if that can be done. –  Dec 28 '15 at 12:50
  • For xdg-open in a terminal you'd have to change the mimetype of your images. – A.B. Dec 28 '15 at 13:24
  • Or you could also use the script in my answer with the complete image path. – A.B. Dec 28 '15 at 13:24
  • 4
    Never use .jpeg as an intermediate format! It incurs generation loss. Why don't you just save the images as .xcf, which is always automatically opened with GIMP? – leftaroundabout Dec 28 '15 at 15:46
  • @leftaroundabout: I don't use it as an intermediate format, it is the final format. However when I want to show someone my artwork it is lower quality if I open it in Image Viewer (the program I want to open everything else in) than it is in GIMP, so I prefer to open them in GIMP. That's why I want this folder to have images opening in GIMP and Image Viewer elsewhere (GIMP takes a while to start up so I don't want it for other images as I don't need them so high quality). –  Dec 28 '15 at 16:59

1 Answers1

21

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.

muru
  • 197,895
  • 55
  • 485
  • 740
A.B.
  • 90,397
  • Does it matter what I change the Name value to? –  Dec 28 '15 at 17:09
  • No, change the name as you would like. – A.B. Dec 28 '15 at 17:11
  • Also, your script has a slight problem... Uhm... All the files in my artwork folder now open fine in GIMP, and the same for all subdirectories, however if a file is not in that area, it just does not open at all. –  Dec 28 '15 at 17:19
  • Let me check it. – A.B. Dec 28 '15 at 17:20
  • Ok, it works fine now that you removed the infinite loop. ;) –  Dec 28 '15 at 17:34
  • You should probably loop on $@ instead of running on just $1. Also, why the exit 0? O.o – muru Dec 28 '15 at 17:37
  • @muru exit 0 removed. A loop isn't necessary, I hope. I use %f in the desktop file. – A.B. Dec 28 '15 at 17:39