Skip to content

Instantly share code, notes, and snippets.

@brunoais
Last active September 19, 2021 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunoais/5302c59e7cc5b6f6903b6078213f27bc to your computer and use it in GitHub Desktop.
Save brunoais/5302c59e7cc5b6f6903b6078213f27bc to your computer and use it in GitHub Desktop.
[Tutorial] PlayOnLinux: Run command line programs while keeping work dir

PlayOnLinux: Run command line programs while keeping work dir

Intro

For this demo, I will provide an example on how to run pingo (image compressor) as a command line program with the help of PlayOnLinux.

Pingo doesn't really need the benefits of PlayOnLinux but other programs may.

In this case, I wanted to use a specific wine version which is not the default in my system and because pingo is very simple to install and have (just a self-conntained executable) it's easier to demonstrate and show how to do.

Here's the TL;DR commands to execute:

Create the running option PlayOnLinux interface

Edit the runner file (here's the example for pingo)

PROGRAM="pingo"
cd "$HOME/.PlayOnLinux/shortcuts"
sed -Ei 's/^cd "/EXEC_PATH="/' "$PROGRAM"
sed -Ei 's@^POL_Wine @cd "$COMMAND_PWD"\nPOL_Wine @' "$PROGRAM"
sed -Ei 's@^POL_Wine @POL_Wine "$EXEC_PATH/"@' "$PROGRAM"

Then create an executable file

cat << 'EOF' > "$HOME/.local/bin/pingo"
#!/bin/bash
COMMAND_PWD="$(pwd)"  /usr/share/playonlinux/playonlinux --run "pingo" "$@"
EOF
chmod +x "$HOME/.local/bin/pingo"

That's it. You can now run pingo normally and, for example, just give the file name

pingo "image.png"

If image.png exists in the current directory, pingo will spot it and compress it.

How it works (from what I can tell)

When playonlinux-bash is run, the current directory in the environment is already replaced. Because it is already replaced, there's no feasible way to get it back. Unless it's sent as a new environment variable. That is what the executable does.

Then, the PlayOnLinux shortcut script has a cd command that leads to the directory where the executable is. However, for an executable such as pingo, the current directory is very useful so the file paths don't have to be absolute, so I use sed to replace the cd command with setting a variable

sed -Ei 's/^cd "/EXEC_PATH="/' pingo

Because the PlayOnLinux executable has lost the path that was set, it needs to be reset. That's what the next line does.

sed -Ei 's@^POL_Wine @cd "$COMMAND_PWD"\nPOL_Wine @' pingo

Now, the current path is fixed and everything is (almost) set, what's missing is the executable. Because the executable is not in the current path, it needs to be called with the whole path. That's what the 3rd run of sed does:

sed -Ei 's@^POL_Wine @POL_Wine "$EXEC_PATH/"@' pingo

The executable in .local/bin gives the environment variable with the current path to PlayOnLinux, so it's available in the startup script.

After this, just execute pingo in the terminal and it will run for the current directory. No need to provide the full path.

I hope this was of help to you too!!!

#/bin/bash
# Name of the program (not path!!!)
PROGRAM_NAME="$1"
cd "$HOME/.PlayOnLinux/shortcuts"
sed -Ei 's/^cd "/EXEC_PATH="/' "$PROGRAM_NAME"
sed -Ei 's@^POL_Wine @cd "$COMMAND_PWD"\nPOL_Wine @' "$PROGRAM_NAME"
sed -Ei 's@^POL_Wine @POL_Wine "$EXEC_PATH/"@' "$PROGRAM_NAME"
cat << 'EOF' > "$HOME/.local/bin/$PROGRAM_NAME"
#!/bin/bash
COMMAND_PWD="$(pwd)" /usr/share/playonlinux/playonlinux --run "$PROGRAM_NAME" "$@"
EOF
chmod +x "$HOME/.local/bin/$PROGRAM_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment