Purism

Purism

Beautiful, Secure, Privacy-Respecting Laptops, Tablets, PCs, and Phones
Purism

Latest posts by Purism (see all)

I’m not a professional application developer but I know a few programming languages and have contributed to FOSS projects over the years. I like to write my personal programs as simple shell script that tend to run on my Linux laptop or server but never on my old Android phone. This is the first time my phone hasn’t restricted me from developing apps how I like to.

My previous Android phone always felt like a locked box, especially when it came to writing my own programs. I needed to read guides to set up a local phone development environment, learn the language and frameworks used for the platform, and only then could I write a native phone application. Once it was written I’d need to figure out how to sideload it onto the phone or otherwise get it into an official app store. Since I’m not a professional application developer, I never had the time or motivation to overcome that learning curve.

I’ve had a Librem 5 for a couple of weeks and I’ve experienced just how easy it is for me to extend the phone’s capabilities with my own simple scripts. I decided to quickly code a simple application backed by a shell script that takes a screenshot of my phone, thank you to user purismforum who shared the inspirational script in the Librem 5 community chatroom. I was surprised how little work was needed to turn a simple shell script into a desktop app with notifications and a GUI.

The First Shell Script

The first thing I needed was a directory to organize all of my scripts at /home/purism/bin and then I created the first pass of my screenshot script:

#!/bin/bash
SCREENSHOT="/home/purism/Pictures/$(date +%Y-%m-%d-%H%M%S).png"
notify-send -t 1000 screenshot "Taking a screenshot in 5 seconds"
sleep 5
grim "$SCREENSHOT"
notify-send screenshot "Screenshot stored at ${SCREENSHOT}"

This script waits five seconds after it’s run and then uses the command line screenshot tool grim to take a screenshot and store it in /home/purism/Pictures/. I needed to make sure the grim package was installed, but I also needed to install the libnotify-bin package so I could use notify-send to send notifications to the desktop. I then made the script executable and tested it out in a terminal:

sudo apt install grim libnotify-bin
chmod a+x /home/purism/bin/screenshot
screenshot

After confirming that it created a new screenshot in /home/purism/Pictures I created a desktop link for it so it was easy to launch from the desktop. To do this I created a new file called /home/purism/.local/share/applications/screenshot.desktop:

[Desktop Entry]
Name=Screen Shot
Type=Application
Icon=applets-screenshooter
Exec=screenshot
Categories=Utility;

This created a handy little icon on my desktop I could click, then wait five seconds, and sure enough there was a new screenshot in my Pictures directory.

Screenshot desktop icon

Making it Fancy

At this stage the app worked but I wondered how easy would it be to add a fancy GUI to allow the user to set the filename and choose the delay. Normally this would mean that I need to dig into some kind of desktop application development environment, but since the Librem 5 works with standard Linux command-line tools, I could take advantage of the yad tool to create a simple GUI from the command line. The yad program (Yet Another Dialog) is a fork of a similar tool called zenity that you can use to build a GUI program from a shell script. While yad is not installed by default, it was easy enough to install with a sudo apt install yad command.

Using yad I was able to wrap a simple GUI around my program with only a few more lines of code. The first line runs the yad command to show the GUI and get input back from the user and the remaining lines parse that output. The finished program looks like this:

#!/bin/bash
SCREENSHOT="/home/purism/Pictures/$(date +%Y-%m-%d-%H%M%S).png"
INPUT=`yad --title Screenshot --text="Take screenshot after X seconds" --form --field=filename:SFL --field=seconds:NUM "$SCREENSHOT" "5" --focus-field=2`
SCREENSHOT=`echo $INPUT | cut -d '|' -f1`
SECONDS=`echo $INPUT | cut -d '|' -f2`
if [ "$SECONDS" -eq 0 ]; then
  exit
fi
notify-send -t 1000 screenshot "Taking a screenshot in $SECONDS seconds"
sleep $SECONDS
grim "$SCREENSHOT"
notify-send screenshot "Screenshot stored at ${SCREENSHOT}"

The final app looks like this:

Simple screenshot app for the Librem 5

Conclusion

It took a couple of minutes to create the script and a few more to add a GUI to it. The best part is I got to use the tools and environment I already know and love, something I could never do on my old Android phone. You could use the same tools and approach to create all sorts of simple GUI tools. The yad program supports some rather sophisticated UI elements including file dialogs, calendars, and color selectors so you can build even rather advanced GUIs without having to break out traditional developer tools.

Discover the Librem 5

Purism believes building the Librem 5 is just one step on the road to launching a digital rights movement, where we—the-people stand up for our digital rights, where we place the control of your data and your family’s data back where it belongs: in your own hands.

Preorder now

Recent Posts

Related Content

Tags