logo

dmpop

Aircraft photography, spaghetti code, half-baked thoughts

Guestbook Reads
Currently reading: Gli occhi di Venezia, Alessandro Barbero


Turn shell scripts into macOS micro apps with Platypus

Using something like YAD, you can add GUI elements (dialogs, input forms, drag-and-drop panels, etc.) to shell scripts on Linux. On macOS, you can do the same and more with Platypus. This venerable open-source tool makes it possible to transform plain shell scripts into macOS micro apps in a matter of minutes, and I'm here to show you how.

Start with installing Platypus. If you have Brew on your macOS machine, run the brew install platypus command.

Installing Brew
To install Brew, run /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". For more information on what Brew is and what it does, visit brew.sh.

Alternatively, grab the latest binary from the project's Git repository, extract the downloaded ZIP archive, and move Platypus.app into the Applications folder.

Before you can build a macOS app with Platypus, you need a shell script that does all the heavy lifting. For this exercise, we are going to build a super simple droplet app that resizes JPEG files to 1200px on the longest side and reduces their quality to 95% using ImageMagick. The script that does all of this is rather simple:

#!/bin/bash
for f in ${@}; do
    /opt/homebrew/bin/magick "$f" -resize "1200>" -quality 95% "$f"
done

Filenames of the files dropped onto the droplet are passed to the script as a Bash array. So the script iterates through the array items (that is, files) and runs the magick command on each of them.

For this script to work, you need to install ImageMagick, which can be done using the brew install imagemagick command.

To keep things tidy, you might want to create a separate folder for the resulting app and all its resources. Paste the script above into a text file and save it under the shrink.sh name in the created folder.

It's smooth sailing from there.

  1. Launch Platypus, and enter your app's name into the App Name field.
  2. Press Select Script and select the shrink.sh script. If you need to tweak the script after you've added it, you can do that directly in Platypus by pressing Edit. You can also check the script using the Check Syntax button.
  3. From the Interface drop-down list, select None.
  4. Make sure that the Accept dropped items option is enabled, press Settings, enable the Accept dropped files option, and press Apply.
  5. Press Create App and save the app in the desired location.

Platypus screenshot

Now, drag and drop JPEG files onto the app to resize them.

The freshly made app does have one limitation. The resize value is hard-wired into the script, so if you want to resize files to a different size, you need to edit the script, and then rebuild the app. That's not exactly the most efficient way of doing things.

To solve the problem, add a dash of interactivity to the script, so the resulting app prompts you to specify the desired value. Platypus doesn't provide native support for GUI elements like dialog boxes and input fields, but you can use AppleScript to work around this limitation. Here's what the tweaked script looks like:

#!/bin/bash

dimension=$(osascript -e 'set T to text returned of (display dialog "Dimension" buttons {"Cancel", "OK"} default button "OK" default answer "")')

for f in ${@}; do
    /opt/homebrew/bin/magick "$f" -resize "$dimension>" -quality 95% "$f"
done

After you have modified the script, make sure to rebuild the app in Platypus. And one more thing. Once you're done working on the app, save its settings as a Platypus profile by choosing Profiles > Save Profile. This way, you can always pick up where you left next time you need to tweak the app.


Published: 2024-11-27Reads: 85
Back
dmpop

dmpop

Linux user, amateur photographer, aviation enthusiast, clueless developer of ADS-B station, Pellicola and Tinble, author of a bunch of books. I can mix a decent Negroni, too. You can reach me at me@dmpop.xyz