Checking train times with Shortcuts

24 Jul 2024

I live in Jersey City and rely on the PATH to get into the city. During commute windows, headways are conveniently every 5 minutes. Outside of those windows, the Newark↔World Trade Center line plummets to every 20 minutes. There’s no use leaving my apartment whenever I’m ready; I have to plan around the train schedule. The PATH’s app does not make this easy. Real-time station information can be pinned to the app’s homepage, but it takes an seconds or minutes to load. The non-real-time schedule requires multiple taps, each with their own little loading segments to access. If I’m getting ready to go, it’s pretty annoying to figure out what train I should be shooting for.

While I’m at the Recurse Center, I figured I should embrace the hacker spirit and solve this problem with some software. My first thought was an SMS-based solution. NJ Transit allows you to text them with a bus or light rail stop number in exchange for arrival times; I think the MTA has something similar. I planned to build a shortcut which would send the correct text and report the result, so I could trigger the workflow at the press of a button. Unfortunately… the PATH has no such texting-based option. Clearly I’d have to some programming, so I began to search for what sort of API the PATH exposed.

Fool that I am, expecting answers to be so simple. There is no public-facing PATH API, at least not one that’s immediately apparent. Fortunately, someone else has already reverse-engineered the PATH app and published their findings. The reason real-time data takes so long to load in the app is that it’s subscribed to the next update, not retrieving the information directly. It seems there’s no official PATH endpoint to retrieve the most recent train data at all! Thankfully for me, the aforementioned reverse engineer has a cache to store each update and make it available over an HTTP API. Phew.

With an HTTP API in hand, I began to think about how best to use it. Bookmark the URL and read the JSON myself? No, the train times are in UTC and mentally converting them to Eastern would be too much work. Create a tiny webpage that displays the information? Maybe, but it doesn’t seem that convenient to access at a moment’s notice. Write an app? That seemed too heavyweight for my goals. I happened to be on the train while thinking of my next step, and reached for a tool available on my phone: iOS Shortcuts.

Shortcuts is a drag-and-drop programming language Apple makes available for system automation. It comes with a few built-in tools, and third-party apps can make actions available if they’re installed. I was delighted to discover one of the built-in tools could send an HTTP request and another could convert the JSON result into a Shortcuts Dictionary. Some fiddling with loops and variables ensued, and I was left with a script that retrieves the HTTP data and creates simple textual output as a result. You can check out the finished product if you have an Apple device of some kind.

Siri integration with Shortcuts is out-of-the-box, so my solution ended up more convenient than I expected. Now I can long-press my watch crown button and ask for the path times, and after a second or two my watch will read off the upcoming departures from my local station. This is exactly what I wanted: no-fuss train information that doesn’t break my flow as I get ready.