Make Aseprite always open files in the same window on Linux

If you are using Aseprite on Linux, you may come across this problem where clicking a file from your file manager opens it in a new instance of Aseprite, rather than using the one you have open.  If so, here's a workaround you can do!

Install Xdotool and Xclip

First you need a couple tools we will use.  xdotool lets you simulate commands to send to a program, such as key presses.  and xclip lets us copy and paste content from the clipboard.

# for fedora silverblue we will create a new toolbox to hold our tools
# if you are not using silverblue, skip these 2 commands
toolbox create aseprite
toolbox enter aseprite

# then install them, on ubuntu replace dnf with apt
sudo dnf install xdotool xclip

Create a Bash Script

Next we want to create a script to check if Aseprite is running, and if so open files with it.  (If you are not using silverblue, strip out "toolbox run -c aseprite" everywhere in this script, so it just calls xdotool and xclip directly)

#!/bin/bash

# try to find the aseprite window by name
window_id=$(toolbox run -c aseprite xdotool search --name "Aseprite v1" | head -n 1)

if [ -n "$window_id" ]; then
    # focus aseprite
    toolbox run -c aseprite xdotool windowactivate "$window_id"

    # copy the file path to clipboard
    echo -n "$1" | toolbox run -c aseprite xclip -selection clipboard

    # simulate opening file in aseprite
    toolbox run -c aseprite xdotool key ctrl+o  # simulate "open" (ctrl+o)
    toolbox run -c aseprite xdotool key ctrl+v  # paste file path
    toolbox run -c aseprite xdotool key Return  # simulate press enter
else
    # aseprite not running, launch it

    # if trying to open file, then pass it to aseprite to open
    if [ -n "$1" ]; then
        /var/home/nazgum/apps/Aseprite.AppImage "$1" &

    # else just open aseprite
    else
        /var/home/nazgum/apps/Aseprite.AppImage & 
    fi
fi

After creating your script, save it and make it executable:

# save the script, i named it launch_aseprite.sh
# then make it executable
chmod +x launch_aseprite.sh

Make Aseprite use our Bash Script

Finally, we just need to make it so when we launch Aseprite we are using our new script:

# edit or create the desktop file
nano ~/.local/share/applications/aseprite.desktop
[Desktop Entry]
Name=Aseprite
Comment=Pixel art tool
#Exec=/var/home/nazgum/apps/Aseprite.AppImage %f
Exec=/var/home/nazgum/apps/launch_aseprite.sh %f
Icon=/var/home/nazgum/apps/icons/aseprite.png
Terminal=false
Type=Application
Categories=Graphics;RasterGraphics;
MimeType=image/png;image/jpeg;image/gif;application/x-aseprite;
# update desktop files database to get your changes
update-desktop-database ~/.local/share/applications

And with this all done, now when you launch Aseprite, or double click on images to open them with Aseprite, it will use your existing Aseprite window!