Example - Retrieving Calendar Events

Posted on
Thu Mar 16, 2023 8:24 am
DaveL17 offline
User avatar
Posts: 6599
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Example - Retrieving Calendar Events

Level of Difficulty: Moderate
Knowledge of Shortcuts: Moderate

This example shows how to retrieve Calendar event information using Shortcuts and a short Python script. This is a basic example in that it only shows the mechanism to get the requested events information back to Indigo. What you do with the data after that is up to you. This example doesn't use Indigo's HTTP API or require authentication. It also runs entirely on the Indigo Server machine (i.e., you can run it from your iPhone, but it won't do anything). It's probably best to be at least somewhat familiar with how to build Shortcuts before attempting to use this example.

Python Script
You'll need a way to run the following Python script--whether it be as an embedded script, linked file or something similar.

Code: Select all
import subprocess

def build_list(my_result):
    """ Construct a list of event tuples """
    result_str = result.decode('utf-8')  # Change bytes object into Unicode string
    result_split = [_.split('\n') for _ in result_str.split(' %% ')]  # Split payload into a list of lists, first on '%%' then on '\n'
    zipped = list(zip(*result_split))  # Zip the lists into a list of tuples (the contents are driven by the shortcut)
    return zipped

num_appt = b'3.0'
result = subprocess.run(['/usr/bin/shortcuts', 'run', 'Get Calendar Events'], input=num_appt, check=True, capture_output=True).stdout
appts = build_list(result)
There are some nuances about the way this script works.
  • First, to set the number of requested events, the number must be provided as a bytes() object. Values less that 10 must be provided as a float. If you send b'3', it *won't* work (but b'10' *will* work for some reason).
  • Second, the data that the shortcut returns to the script is text data in bytes() format. This script doesn't account for complex objects like file attachments to events.
The shortcut layout:

Screenshot 2023-03-16 at 8.36.09 AM-2.png
Screenshot 2023-03-16 at 8.36.09 AM-2.png (868.63 KiB) Viewed 650 times

The Shortcuts app isn't currently designed to output "complex" data containers like JSON or ordered lists, so the example uses a simple "hack" to work around this limitation. It puts all the requested information on one line in the list element to ensure that the data are all provided in a single payload. It separates the data elements with [space][percent][percent][space] which is used in the Python script to parse the event data. Setting the event details within the list is a bit tricky (and beyond the scope of this example).

There are some nuances about the way this shortcut works:
  • First, notice the tick mark next to "Provide Output". This option must be enabled or the shortcut won't return anything. Even though it looks like the "Provide Output" setting isn't meaningful because "Use as a Quick Action" is disabled, that's not the case.
  • Second, this shortcut outputs the data as a bytes() object that contains nothing but text. If you're going to try to work with Event attachments or other complex objects, you'll need to base64 encode them (beyond the scope of this example). If you use base64 encoding, you can forego the "Stop and Output" action as base64 encoding outputs by default.
  • Third, "Home" is the name of my calendar. You'll need to change it to one of your named calendars (or set it to examine all calendars).
  • Lastly, for simplicity, this example shortcut doesn't include any error handling.

If the script (and shortcut) run successfully, you'll receive output that looks like this (assuming you haven't changed anything)

Code: Select all
 [('My First Event', 'Mar 20, 2023 at 9:30 AM', 'Mar 20, 2023 at 12:30 PM'), ('My Second Event', 'Mar 21, 2023 at 12:00 AM', 'Mar 21, 2023 at 11:59 PM'), ('My Third Event', 'Mar 24, 2023 at 12:00 AM', 'Mar 27, 2023 at 11:59 PM')]
You can download the shortcut from the Indigo File Library.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests