Skip to content

Instantly share code, notes, and snippets.

@Swiss-Mac-User
Created January 16, 2024 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swiss-Mac-User/eae5bbc64e739a7490765de624665033 to your computer and use it in GitHub Desktop.
Save Swiss-Mac-User/eae5bbc64e739a7490765de624665033 to your computer and use it in GitHub Desktop.
Shell script to insert a VALARM block to every event in an iCalendar file (.ics), making imported events show an alert x hours before the event
#!/bin/bash
# Inspired from: https://x.com/SwissMacUser/status/1349065380514426883
# Personally used to add event reminders to the downloadabl iCalendar files
# of the yearly Stadt Zürich digital Entsorgungskalender at
# https://www.stadt-zuerich.ch/ted/de/index/entsorgung_recycling/entsorgen/persoenlicher_entsorgungskalender.html
# Check if the minimum number of arguments is provided
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <path-to-ics-file> <hours-to-alert-before> [backup]"
exit 1
fi
# Assign arguments to variables
FILE_PATH=$1
HOURS_BEFORE=$2
MAKE_BACKUP=$3
# Validate HOURS_BEFORE to be an integer
if ! [[ "$HOURS_BEFORE" =~ ^[0-9]+$ ]]; then
echo "A second parameter 'hours before event' must be a number (0-99)."
exit 1
fi
# If 'backup' option is provided, create a backup file before modifications
if [ "$MAKE_BACKUP" == "backup" ]; then
cp "$FILE_PATH" "${FILE_PATH}.bak"
echo "Backup of the original file created: ${FILE_PATH}.bak"
fi
# Add VALARM blocks to every event in the .ics file
awk -v HOURS_BEFORE="$HOURS_BEFORE" '/END:VEVENT/ {print "BEGIN:VALARM\nTRIGGER:-PT" HOURS_BEFORE "H\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM"} {print}' "$FILE_PATH" > temp.ics
# Replace the original file with the new one
mv temp.ics "$FILE_PATH"
echo "The .ics file has been updated with an alarm -${HOURS_BEFORE} hours before each event."

How to use this script

First obtain an iCalendar (.ics) file, for example from the Stadt Zürich Entsorgung & Recycling.

  1. Download the script file add_alarms_to_ics.sh from here
  2. Open your Terminal / Command Line Interface app (on macOS: Terminal.app for example)
  3. Use the following or a similar command to make the script executable:
    chmod +x /path/to/add_alarms_to_ics.sh
  4. Run the script on the desired iCalendar (.ics) file, with this command:

    `/path/to/add_alarms_to_ics.sh "/path/to/icalendar.ics" 9 "backup"

Script options

  • "/path/to/icalendar.ics" = you may also just drag'n'drop the file into your terminal/cli window
  • 9 (or any other number) = set Reminder -X hours before the events start
  • "backup" (Optional) = backup the original iCalendar .ics file before modifying it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment