Cli script to add an appimage to the menu

Forum Forums General Tips and Tricks Cli script to add an appimage to the menu

  • This topic has 3 replies, 3 voices, and was last updated Mar 28-9:21 am by PPC.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #79659
    Member
    PPC

      A while back I tried to create a script that added appimage files to the menu – it kinda of worked, but I recently reworked it and it seems to work 100% of the times – the adding the appimage to the menu part. The icon part is a bit hit or miss, it still requires a bit more work.
      My aim is to add this little extra to FT10’s zzzfm configuration – simplifying the usage of appimages – you right click an appimage and have 2 extra options: – run appimage (that basicaly makes the file executable and runs it) and – add appimage to menu (that runs this script).
      It was tested with several appimages: gimp, onlyoffice, audacity, a some more…

      for now you have to run the scrip in the terminal and, launching the script, a space, and the full path to the appimage file you want to add to the menu.

      Dependency: you do need to have 7z installed

      When running the scrip, answer “a” to any question 7z asks…

      As always, please test at your own risk:

      #!/bin/bash
      #script to generate a valid .desktop file to launch an appimage
      
      #Check if a file was provided:
      var="$@"
      if [ -n "$var" ]; then
          echo "This script is being run on a file (make sure it's an appimage file)..."
      else
          echo "There was no appimage file to add to the menu"
          exit
      fi
      
      #Start main script:
      for FILE1 in "$@"
      do
      
      ###make sure that the appimage is executable
      chmod a+x $FILE1
       
      ###### extract .desktop from appimage: 
      cd /tmp
      7z e $FILE1 usr/share/applications/ -y
      echo $FILE1 > /tmp/appimage_path
      ### extract png icon from appimage:
      #list appimage's contents
      7z l $FILE1 > /tmp/list.txt
      #find lines that contain only ".png" and "usr/share":
      grep -n ".desktop" /tmp/list.txt > /tmp/lines_with_desktop.txt
      grep -n ".png" /tmp/list.txt > /tmp/lines_with_png.txt
      grep -n "usr/share" /tmp/lines_with_png.txt >/tmp/lines_with_png_and_usr_share.txt
      var=$(cat /tmp/lines_with_png_and_usr_share.txt)
      #strip everything up to "usr":
      var=$(echo "${var#*usr}")
      echo $var
      icon_path=$(echo usr$icon)
      
      #icon's location inside appimage:
      echo Icons location inside appimage: 
      cat /tmp/lines_with_png_and_usr_share.txt
      echo .desktop files inside appimage:
      desk_file=$(cat  /tmp/lines_with_desktop.txt| awk '{print $NF}')
      echo $desk_file
      
      #extract the .desktop file:
      eval 7z e $FILE1 $desk_file -o/tmp/appimage
      
      done
      
      #Alter path of the extracted .desktop file to launch the appimage itself:
      desk_file=$(cat  /tmp/lines_with_desktop.txt| awk '{print $NF}'| grep "\.desktop"|head -n 1)
      ORIGINAL_EXEC=$(egrep "^Exec=" /tmp/appimage/$desk_file | cut -d'=' -f2| sed -n '1p'|cut -d' ' -f1)
      appimage_location=$(cat /tmp/appimage_path)
      original="Exec=$ORIGINAL_EXEC"
      sed -i "s:$original:Exec=$appimage_location :" /tmp/appimage/$desk_file
      
      #Try to get appimage's icon:
      icon=$(cat /tmp/appimage/$desk_file| grep "Icon="| cut -d'=' -f2| cut -d'.' -f1)
      sleep 1 && echo Icon on desktop file is: $icon 
      sleep 1
      
      mkdir $HOME/appimage_icons/
      cd $HOME/appimage_icons/
      7z e $FILE1 $icon.png -y
      7z e $FILE1 $icon.svg -y
      
      if [ -f "$HOME/appimage_icons/$icon.png" ]; then
          icon="$icon.png"
        else
          if [ -f "$HOME/appimage_icons/$icon.svg" ]; then
          convert $icon.svg $icon.png
          icon="$icon.png"
          fi
       
       #replace icon on the generated .desktop file to the extracted one
       sed -i "s:Icon=.*:Icon=$HOME/appimage_icons/$icon :" /tmp/appimage/$desk_file   
          
      fi
      
      #Save the created .desktop file in the /usr/share/applications folder with the prefix appimage (for easier management)
      gksu cp /tmp/appimage/$desk_file /usr/share/applications/appimage_$desk_file
      ##cp /tmp/appimage/$desk_file ~/appimage_$desk_file
      

      Edit: I altered the code above, and replaced the original version with one that *should* correctly extract and use most .png icons used in the .desktop files. so far it has 100% success rate…

      • This topic was modified 1 year, 1 month ago by PPC.
      • This topic was modified 1 year, 1 month ago by PPC.
      • This topic was modified 1 year, 1 month ago by PPC.
      #80014
      Member
      jamieshah24
        Helpful
        Up
        0
        ::

        AppImages have limited integration with your desktop environment. You can manually add a launcher to your menu, but there is also a tool in development that can integrate appimages

        Manual approach

        You can manually add a launcher for your AppImage in the dash. Once it is there, you optionally will be able to pin it to the Dash/Dock.

        Commonly, the AppImage provides an icon and a .desktop launcher file that you can use for a start. Retrieve it from within the AppImage file as follows:

        Make sure the AppImage is running.
        Using the command mount in a terminal, find out where the AppImage is mounted in your file system.
        For example, in my little test, I see an entry for an AppImage appearing as

        LibreSprite-8ac9ab1-x86_64.AppImage on /tmp/.mount_LibreSyxGgsk type fuse.LibreSprite-8ac9ab1-x86_64.AppImage (ro,nosuid,nodev,relatime,user_id=1000,group_id=1000)
        in the terminal output of mount. It reveals that the AppImage is mounted under /tmp/.mount_LibreSyxGgs.

        Navigate to the folder where the AppImage is mounted using your file manager. In most cases, you will find icons there and a .desktop launcher, in the main folder or in a subfolder.
        Copy the icon file to the folder ~/.local/share/icons
        Copy the .desktop file to ~/.local/share/applications
        Open the copy of the .desktop file you created, and edit the Exec= line (and also the TryExec=line if present) to include the full pathname of your AppImage. Check on the Icon= line if it corresponds with the basename of the icon file you copied (just the basename, no need for the extension). If needed, update. You can also provide a full path to the specific icon file.
        This will cause the .desktop file to be picked up in your application menu, i.e., the Application Overview in standard Ubuntu with Gnome Shell.

        Automating using the tool AppImageLauncher

        If you work frequently with AppImages, then you could explore the tool “AppImageLauncher”. That tool is a regular application, designed to associate with .AppImage files. When you click an AppImage file, the tool will automatically copy it to a standard folder (by default ~/Applications) and create a desktop launcher for it.

        Software requirements and conventions used
        Software Requirements and Linux Command Line Conventions
        Category Requirements, Conventions or Software Version Used
        System Ubuntu 20.04.2
        Software no specific software is required, but our examples use kiwix
        Other Privileged access to your Linux system as root or via the sudo command is not required except in an optional step where we install an icon theme.
        Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
        $ – requires given linux commands to be executed as a regular non-privileged user
        What is an Appimage file?
        An appimage file is a compressed image of an application and any libraries it uses. When you execute an appimage file, it is temporarily mounted on your file system in order to run. Traditionally, applications are installed by using the package manager. In the case of Ubuntu, that would be apt. This is a very convenient method of installation for the end user, but for developers it can be a lot of extra work to package their applications separately for each distribution’s package manager. Developers are able to package their application into an appimage file once and it will run on any distribution. As a result, you may find that some software is only available in the appimage format for your distribution.

        When you download the appimage file, there is no installation and no root privileges necessary. Appimages make no changes to your system, and they are portable universal binaries that includes all dependencies and libraries within it. We previously covered Snaps and Flatpak which provide similar features. These features are often seen as benefits of appimages, but depending on how you prefer to launch applications, they may also be a drawback. Typically, when you install an application via the distribution’s package manager, it neatly integrates into the system and an application launcher is created for you.

        On the other hand, when you download an application that is distributed as an appimage file, it is just another file on your computer. In order to open the application, you need to make this file executable and launch the application by specifying the path to it on the command line or double clicking the file in your file manager, which in the case of Ubuntu would be nautilus. If you want an application launcher, then you need to create it yourself.

        Download the appimage
        One of the benefits of the appimage format is that you can download the application directly from the developer’s website no matter what distribution you are using. For the purposes of this tutorial we will download the Kiwix appimage from the official Kiwix website. Kiwix is a free and open source application that lets you do download all of Wikipedia and read it offline. It has grown to allow downloading and offline reading from other sources as well, but those details are beyond the scope of this article.

        On the official download page there are download links available for Linux, Windows, macOS, Android, iOS, and browser extensions. If you click the link for Linux, then you will be able to download the latest version as an appimage. To download and run this appimage on the command line all you need to do is enter the following commands to download the file, make it executable, and run it.

        $ wget https://download.kiwix.org/release/kiwix-desktop/kiwix-desktop_x86_64.appimage
        $ chmod +x kiwix-desktop_x86_64.appimage
        $ ./kiwix-desktop_x86_64.appimage

        Although an appimage can be downloaded to and ran from any directory, in order to keep the filesystem well organized, let’s move it to a more appropriate directory before we make an application launcher for it.

        $ mkdir ~/bin && mv kiwix-desktop_x86_64.appimage ~/bin/
        Creating an application launcher
        One of the great features of Ubuntu 20.04 is that you can easily launch applications by clicking Show Applications in the dock panel and then clicking the application in the Applications Window. Alternatively, you can press the Superkey(often the windows key on pcs and left command key on macs), type the name of the application and then press enter to launch it. In order for an application to show up in this Applications Window, it must have a desktop entry in an appropriate directory. These desktop entries are files that specify how to launch the application and end in the .desktop file extension.

        System wide applications have desktop entries located in /usr/share/applications. However, writing to this directory requires root privileges and since one of the benefits of appimages is that they don’t require root privileges, we will create a desktop entry in the ~/.local/share/applications directory. The ~/.local/share/applications directory is for desktop entries for the current user. Creating a .desktop file here will make the application launcher available to the current user.

        With your favorite text editor, create a file called kiwix.desktop in ~/.local/share/applications.

        $ vim ~/.local/share/applications/kiwix.desktop
        Next, enter the following into the the file and save it.

        [Desktop Entry]
        Name=Kiwix
        Comment=Read Wikipedia offline
        Exec=/home/username/bin/kiwix-desktop_x86_64.appimage
        Icon=kiwix
        Terminal=false
        Type=Application
        Categories=Education;
        The first line specifies that this is a desktop entry. The second line indicates the application name that you will see in the applications window. The third line consists of a comment that can be viewed as a tooltip. The fourth line specifies the path to the executable file. Here you should replace username with your actual username of course. The fifth line indicates the icon to use. Basically on depending this I have first worked on best ERP partner in Kolkata and related things. You can either specify the path to a custom icon or use an icon that is part of an icon theme pack. The example above does the latter. In order for it to display an appropriate icon, you must be using an icon theme that includes an icon for this application.

        On Ubuntu the “numix-icon-theme-circle” is an icon theme that includes a kiwix icon and be installed with $ sudo apt install numix-icon-theme-circle. The sixth line specifies whether this application runs in the terminal or not. The seventh line tells the system whether this is an Application, Link, or Directory. The final line specifies the category the application falls under for application menus that separate application launchers into different categories.

        Now that you have created and saved the Desktop Entry, you should see the application in the Applications Window and should be able to launch it from there. You can optionally right click on the icon in the dock panel and click Add to Favorites if you would like this application launcher to remain in the dock at all times.

        Alternative Method
        Many appimages include their own .desktop file within the image. If you don’t want to create the file from scratch yourself, then you could locate the one that is included with your appimage, copy it, and edit its content as needed. Below we will look at an example of how to do this using the kiwix appimage.

        First, go to the directory where you saved the appimage file and execute it as described in the Download the appimage section of this article. Now that you have executed the appimage it will be temporarily mounted on the file system. To find out where it is mounted issue the following command.

        $ mount | grep .appimage

        We get the following output which tells us that the expanded image is mounted in the /tmp/.mount_kiwix-HhmzJR diectory. The exact name of the temporary directory will differ each time the appimage is launched.

        kiwix-desktop_x86_64.appimage on /tmp/.mount_kiwix-HHmzJR type fuse.kiwix-desktop_x86_64.appimage (ro,nosuid,nodev,relatime,user_id=1000,group_id=1000)
        Now that we know where the decompressed appimage is mounted to we can see if any .desktop files are included in it with the following command.

        $ find /tmp/.mount_kiwix-HHmzJR -iname “*.desktop”
        We receive the following output.

        /tmp/.mount_kiwix-HHmzJR/kiwix-desktop.desktop
        /tmp/.mount_kiwix-HHmzJR/usr/share/applications/kiwix-desktop.desktop
        As we can see there are two .desktop files provided within the appimage. We can copy either of these and edit it’s contents to suit our purposes.

        $ cp /tmp/.mount_kiwix-HhmzJR/kiwix-desktop.desktop ~/.local/share/applications/
        With your favorite text editor, edit the kiwix-desktop.desktop file in ~/.local/share/applications.

        $ vim ~/.local/share/applications/kiwix.desktop
        When opening the file to edit you will see the following contents.

        [Desktop Entry]
        Type=Application
        Name=Kiwix
        Comment=View offline content
        Icon=kiwix-desktop
        Exec=kiwix-desktop %F
        Terminal=false
        MimeType=application/org.kiwix.desktop.x-zim;
        Keywords=zim;
        Categories=Education;
        X-AppImage-Version=2.0.5
        Edit the Exec and Icon values so that the content looks like the following example.

        [Desktop Entry]
        Type=Application
        Name=Kiwix
        Comment=View offline content
        Icon=kiwix
        Exec=/home/username/bin/kiwix-desktop_x86_64.appimage
        Terminal=false
        MimeType=application/org.kiwix.desktop.x-zim;
        Keywords=zim;
        Categories=Education;
        X-AppImage-Version=2.0.5

        Hope this article helps you properly.

        #80018
        Member
        ModdIt
          Helpful
          Up
          0
          ::

          Appimages make no changes to your system, and they are portable universal binaries that includes all dependencies and libraries within it.

          mmm. not really correct, before abandoning appimage I found several which will not work correctly due system lib Versions not as expected.
          Latest Gimp was one, probably still is. I just boot antiX testing live with latest gimp these days.

          Config and cache files are setup outside of the compressed appimage, usualy but not always just in home and left behind when an appimage is deleted.

          Many appimages can be opened/decompressed after changing ending to zip.

          I intensly dislike the appimages which have to be mounted or run to inspect contents.
          At least browsers could totaly remove any semblance of user privacy if manipulated.
          For many appimages ther is no checksum and signature published.

          #80022
          Member
          PPC
            Helpful
            Up
            0
            ::

            I usually define Appimages as the Linux equivalent to Windows “Portable Apps”: a single self extracting compressed file that includes almost every single dependency the application needs to run on most Linux OS’s

            As far as I know no single “universal” package can really run on every single existing Linux OS in existence:
            -Snaps require a certain init system that is not used in some Distros (antiX is one of those distros)- so it’s not really “universal”
            -Flatpaks used to require a certain init system when I began using antiX, but that changed shortly after I began using it. Currently some applications offered in flatpak format do not install in antiX 19, for example- so… also not really “universal”
            -Appimages try to be as independent from the OS as possible- they don’t require a certain init system, and they include most dependencies they require, but some systems do not have installed the dependencies that the person that created the appimage expect to already be in the OS, so, are not included in the appimage it self. Some recent appimages require more recent versions of the dependencies than antiX 19 has available, so… they are also not really universal.

            Every single “Universal” file system probably needs to store some kind of configuration file – and that is done somewhere in the Home folder – the nice/secure part is that only the Home folder is used, no files are installed anywhere else – that’s why you can run Appimages as a ordinary user, no “sudo” or “su” required…

            I enjoy appimages because I find it the simplest way possible to add a new application to the system- I have some games in that format (like Xonotic and Speed Dreams), and I also have large apps like Gimp, in that format. I test new LibreOffice versions via the .appimages officially provided, without the need to reinstall that applications…

            It’s a matter of “taste”- some consider that a certain Universal package is the future- Canonical seems to be betting Ubuntu’s future on using Snaps almost exclusively and some Linux personalities think that flatpaks are the future, other think it’s appimages…

            The nice thing about Universal packages is that they streamline (ex: snaps are all available on a central “Store” and flatpaks can have multiple “repositories”, but most are available in Flathub) , may make simpler, the process of publishing a new application (and installing it)- that’s one of the reasons some Commercial Developers don’t create versions of their applications/games – Linux is a tiny market – it has some 2-5% of Desktop market share- and that small percentage has at least 3 major different packages (Debian’s, Red Hat’s and Arch’s)- so having to create just one Package that runs on most Linux OS’s is a plus…

            For ideal integration of an application with the system, it’s, for now, always better to use the OS’s default package manager…

            P.

            • This reply was modified 1 year, 1 month ago by PPC.
            • This reply was modified 1 year, 1 month ago by PPC.
            • This reply was modified 1 year, 1 month ago by PPC.
          Viewing 4 posts - 1 through 4 (of 4 total)
          • You must be logged in to reply to this topic.