6

I have some really large files in my drive as Data. My default editor is gedit. I wish to know if I can change my default editor to something else like say glogg, for files above a certain threshold of size. That is, can I choose my default application depending on the size of file, even the files are of the same format ?

2

You need a desktop file, and a wrapper script:

  1. The desktop file

    • Create a desktop file

      nano ~/.local/share/applications/wrapper.desktop
      
    • Add the configuration below

      [Desktop Entry]
      Name=File size dependent editor
      Comment=File size dependent editor
      Exec=/home/user/bin/wrapper %f
      Icon=gedit
      Terminal=false
      Type=Application
      Categories=Editor;
      StartupNotify=true
      MimeType=text/plain;
      
    • Replace user in Exec=/home/user/bin/wrapper %f with your username, the output of

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

    • Use MimeType=text/plain 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
      
    • If you want to use a editor with a CLI, like nano, than replace false in Terminal=false with true

  2. The wrapper script

    • Create a new script

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

      #!/usr/bin/env bash
      LARGE_FILE_EDITOR="glogg"
      SMALL_FILE_EDITOR="gedit"
      
      MAX_SMALL_FILE_SIZE=1000000
      
      FILESIZE=$(stat -c%s "$1")
      
      if [ "$FILESIZE" -le $MAX_SMALL_FILE_SIZE ]; then
              "$SMALL_FILE_EDITOR" "$1"
      else
              "$LARGE_FILE_EDITOR" "$1"
      fi
      exit 0
      
    • Define your editor for large files in LARGE_FILE_EDITOR, e.g. glogg

    • Define your editor for small files in SMALL_FILE_EDITOR, e.g. gedit

    • Define the maximum file size for the $SMALL_FILE_EDITOR in MAX_SMALL_FILE_SIZE, e.g. 1000000 for 1MB

    • Make your wrapper script executable

      chmod +x ~/bin/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 wrapper

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

    • Click Open With Other Application

    • Click the button View All Applications

    • Select the entry File size dependent editor

  5. Enjoy ;)

| improve this answer | |
  • Although, why is icon not in showing like gedit icon !! Is it possible to inherit the icon of whichever editor is opened ? – user35952 Sep 29 '15 at 12:16
  • No, that's not possible. The desktop file know nothing about the decision in the wrapper script. You can use a universal icon, E.g. a logger icon. – A.B. Sep 29 '15 at 12:18
  • How do I do that ?? – user35952 Sep 29 '15 at 12:19
  • If want to use a icon from your icon theme, use the filename without an extension or use the full path for a custom icon. – A.B. Sep 29 '15 at 12:21
  • But the icon specified as gedit in the wrapper file is not working too ?? – user35952 Sep 29 '15 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.