Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created May 30, 2024 15:06
Show Gist options
  • Save mcupak/c50eb423e5c3b0d785445a15e143f084 to your computer and use it in GitHub Desktop.
Save mcupak/c50eb423e5c3b0d785445a15e143f084 to your computer and use it in GitHub Desktop.
Audio alerts for specific times on Mac OS.
#!/bin/bash
message_volume=30
# Check if at least one argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 \"9\" \"1:30\" \"15:00\""
exit 1
fi
# Function to infer AM or PM and convert to 24-hour format
convert_to_24_hour() {
local time="$1"
if [[ "$time" =~ ^([0-9]{1,2})(:([0-9]{2}))?$ ]]; then
hour=${BASH_REMATCH[1]}
minute=${BASH_REMATCH[3]:-00} # Default to 00 if minutes are not provided
if (( hour == 12 )); then
# Special case for 12:00 PM
printf "%02d:%02d\n" "$hour" "$minute"
elif (( hour >= 8 && hour <= 11 )); then
# AM times
printf "%02d:%02d\n" "$hour" "$minute"
else
# PM times
hour=$((hour + 12))
printf "%02d:%02d\n" "$hour" "$minute"
fi
else
echo "Invalid time format: $time"
exit 1
fi
}
# Convert all provided meeting times to 24-hour format and store them in an array
MEETING_TIMES_24=()
for time in "$@"; do
MEETING_TIMES_24+=("$(convert_to_24_hour "$time")")
done
# Sort the meeting times to determine the latest one
IFS=$'\n' sorted_times=($(printf "%s\n" "${MEETING_TIMES_24[@]}" | sort))
unset IFS
# Determine the last index of the sorted_times array
last_index=$((${#sorted_times[@]} - 1))
latest_time=${sorted_times[$last_index]}
while true; do
# Get the current time in 24-hour format
CURRENT_TIME=$(date +%H:%M)
# Check if the current time matches any of the meeting times
for meeting_time in "${MEETING_TIMES_24[@]}"; do
if [[ "$CURRENT_TIME" == "$meeting_time" ]]; then
original_volume=$(osascript -e "output volume of (get volume settings)")
osascript -e "set volume output volume $message_volume"
echo "$meeting_time meeting starting now"
say "Next meeting starting now"
osascript -e "set volume output volume $original_volume"
# Sleep for 60 seconds to avoid multiple alerts within the same minute
sleep 60
fi
done
# Terminate the script if the current time is past the latest meeting time
if [[ "$CURRENT_TIME" > "$latest_time" ]]; then
echo "All meetings for today are done."
exit 0
fi
# Wait for 1 minute before checking again
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment