Using Python Scripts with MacOS Shortcuts

After playing with MacOS Shortcuts for a while, I quickly realized that its control flow is robust but not as robust, flexible, or convenient as a scripting language. In addition, I had extended requirements not inherently available in MacOS Shortcuts. I appreciated the significant advantage is integration with MacOS directly and wanted to take advantage of it such as popping a notification and integrating shortcuts into the context menus. In addition, MacOS Shortcuts can feed input into MacOS applications (including scripts), as well as handle their output.

The purpose of this entry is to demonstrate a simple use case of using MacOS Shortucts to call a custom script and handle its output.

Requirement

As a cybersecurity analyst, I need to know the approximate geolocation of an IP address across all applications (online or offline) so that I can quickly assess a cybersecurity event’s risk level.

Tools

This requirement is not fully satisfied by built-in MacOS tools, including MacOS Shortcuts.

  • IP Geolocation Database: Maxmind DB (this free version is 80% or so accurate; a subscription is required for a more accurate Geolocation result)

  • Scripting language: Python

  • MacOS Integration: Apple Shortcuts

Control Flow

Below is a flowchart describing the flow of this simple application.

  1. Initiate the MacOS Shortcut

    1. Highlight an IP Address and start the MacOS Shortcut from the Context Menu; or

    2. Launch a Shortcut from the Menu Bar which will prompt the user the type in an IP address.

  2. The Shortcut passes the parameter to the Python script, which will query the locally stored IP database for the coordinates.

  3. The output of the Python script is returned to the Shortcut which then returns it as a notification pop-up or passes the coordinates to Apple Maps to display.

Relevant code snippets are below.

# The following python modules need to be imported
import ipaddress
import sys
import geoip2.database  # pip install geoip2
import iptoc_config as cfg

#####
## Here is a code snippet of the function that calls the Maxmind GeoIP function to query the database.

def get_latlon(ip_to_check):
    """Returns a tuple of latitude and longitude."""
    with geoip2.database.Reader(cfg.db_city_path) as reader:
        response = reader.city(ip_to_check)
        return response.location.latitude, response.location.longitude

The full code for this script (named “iptoc.py) can be downloaded from GitHub here:

https://github.com/byteBOOSTgit/iptoc/tree/main

NOTE: I use “Anaconda” for Python instead of the built-in Python in MacOS because of specific business requirements. However, you can also use the MacOS Python installation and install the required modules for Maxmind.

Shortcuts

Menu Bar

Below is a screenshot of how to configure the shortcut to be called from the Menu Bar. You will notice how the input prompted and received by the “As for” component is passed to the Python script. The output is forwarded to the MacOS “Notification” facility to display the output.

The following is a video demonstration of the Shortcut in action.

Quick Action (Context Menu)

Below is a screenshot of how to configure the shortcut to be called from the context menu (right-click on the highlighted text).

The following is a video demonstration of the Shortcut in action.

Closing

This trivial example demonstrates how you can use Apple Shortcuts to feed input into complex programs and then integrate the output into the operating system and other MacOS apps.

Previous
Previous

Managing My Atrial Fibrillation in the Apple Ecosystem