Page 1 of 1

Example - Adding Indigo Data to Calendar

PostPosted: Mon Apr 24, 2023 9:35 am
by DaveL17
Level of Difficulty: Moderate
Knowledge of Shortcuts: Moderate


This example shows one way to create a new Calendar event from Indigo with included data. This kind of automation is probably most useful for tracking things that happen infrequently--you could easily overwhelm your calendar with something that happens multiple times per day. This is a bare minimum example that's meant to show the necessary pieces. It assumes that all the necessary pieces are set up on your Indigo server machine (access to Calendar, etc.)

You'll need three things at least:
  • Some way to cause the Shortcut to run - in this case, an embedded script that's fired using an Indigo Action.
  • An Apple Shortcut to process the incoming data and create the Calendar event.
  • An Apple Calendar to add the data to - I'd recommend a separate calendar (I've uncreatively called mine "Indigo").

Screenshot 2023-04-24 at 10.15.20 AM.png
Screenshot 2023-04-24 at 10.15.20 AM.png (189.65 KiB) Viewed 1120 times


Add the following code to the Action's Execute Script action:

Code: Select all
import os
 
val = indigo.variables[123456].value  # The value to pass. In this case, a variable value.
os.system(f'/usr/bin/shortcuts run "Post Event to Calendar" <<< \"{val}\"')  # Will return 0 on success; 256 on error.
Obviously, you'll need to tailor the code to fit your situation (embedded scripts need to complete within 10 seconds, otherwise they should be added as linked scripts). "Post Event to Calendar" is the precise name of the shortcut (you might call yours something else, for example). Note that "Show Compose Sheet" is unchecked, otherwise it will open a dialog on the host machine and expect additional input/revision.

This example shows the event being added as an all day event; it could be modified to include a timestamp or time(s) to make the event more specific. You could also add more data to the payload to include things like location and notes (it would require passing the data in a recognizable format like JSON.).

screenshot_2023-03-13_at_6.56.59_pm.png
screenshot_2023-03-13_at_6.56.59_pm.png (117.92 KiB) Viewed 1120 times


A version of this example is also posted to the Apple Shortcuts Wiki

Re: Example - Adding Indigo Data to Calendar

PostPosted: Mon Apr 24, 2023 2:49 pm
by DaveL17
Level of Difficulty: Moderate
Knowledge of Shortcuts: Moderate

This shortcut is available in the Indigo File Library

Here's an example to show how you can pass a more complex object to the shortcut.

Screenshot 2023-04-24 at 3.37.36 PM.png
Screenshot 2023-04-24 at 3.37.36 PM.png (18.93 KiB) Viewed 1082 times


Note the construction of the inputs (the shortcut is very particular regarding the format of the inputs). There iares likely other ways to do it.**

Code: Select all
import os
import json

# If key 'AllDay' is set to "True" it will override the Start and End times.
# Double braces are required at the ends of the payload to ensure they're included (and not mistaken for a format specifier).

title = "My Title"
location = "My Location"
notes = "My Notes"
start = "11:00 AM"
end = "1:00 PM"
all_day = "False"  # a string

payload = f'{{"Title":"{title}", "Location":"{location}", "Notes":"{notes}", "Start":"{start}", "End":"{end}", "AllDay":"{all_day}"}}'
os.system(f'/usr/bin/shortcuts run "Post Event to Calendar" <<< \'{payload}\'')

  • A Title element is required, but the other elements are optional depending on your event data. For example, Start and End times aren't required if you create your events as "All Day" events.
  • Emoji characters are allowed in text fields (i.e., Title, Location, Notes).
  • Start and End times can be descriptive. For example, Start: "11:00 AM" and End: "Tomorrow at 1:00 PM"

Screenshot 2023-04-24 at 3.35.28 PM.png
Screenshot 2023-04-24 at 3.35.28 PM.png (62.88 KiB) Viewed 1082 times


** Here is another option to format the data for transmittal to the Shortcut:

Code: Select all
payload = {
    "Title": "My Title",
    "Location": "My Location",
    "Notes": "My Notes",
    "Start": "11:00 AM",
    "End": "1:00 PM",
    "Calendar": "Indigo",
    "AllDay": True  # a boolean
    }

payloadj = json.dumps(payload)

result = subprocess.run(["/usr/bin/shortcuts", "run", "Post Event to Calendar"],
                        input=payloadj.encode("utf-8"),
                        capture_output=True,
                        timeout=10
                        )
                       
print(f"Return Code: {result.returncode}")
print(f"Output: {result.stdout.decode('utf-8')}")
print(f"Errors: {result.stderr.decode('utf-8')}")

Re: Example - Adding Indigo Data to Calendar

PostPosted: Tue Apr 25, 2023 2:36 am
by McJohn
Hi Dave,

Thanks for this very interesting information, nice job!
It this also possible the other way around?
Get data from the Apple Calendar app into Indigo?

John

Re: Example - Adding Indigo Data to Calendar

PostPosted: Tue Apr 25, 2023 3:49 am
by DaveL17
Hi John - thanks. Yes it is possible. See this post for one way to do it: https://forums.indigodomo.com/viewtopic.php?f=298&t=27014

Re: Example - Adding Indigo Data to Calendar

PostPosted: Tue Apr 25, 2023 8:44 am
by McJohn
Thanks! Nice job Dave!
I'm going to play with it on these rainy and cold days here.

John

Re: Example - Adding Indigo Data to Calendar

PostPosted: Tue Apr 25, 2023 5:31 pm
by DaveL17
I added another option for sending data to the Shortcut to the example above using the subprocess() library.