Skip to content

Instantly share code, notes, and snippets.

@SergioRibera
Last active November 4, 2022 16:26
Show Gist options
  • Save SergioRibera/1a61e7e990fd1c6d6da77ec44c6381dc to your computer and use it in GitHub Desktop.
Save SergioRibera/1a61e7e990fd1c6d6da77ec44c6381dc to your computer and use it in GitHub Desktop.
Launch WorkDay application automaticaly

Launch work applications only on workdays

This guide will help you configure the necessary scripts and services so that when you turn on your computer it will automatically check if it is a working day or not, if so it will verify if it is a local holiday to be able to launch the applications and services needed to work comfortably.

Requirements

  • Create an API app (Google Calendar API) in the google developer account
  • Have systemd installer (or other if you know how manage services)

Instalation

IMPORTANT: Read each step of this guide

  • From the "Credentials" tab you can create an API key, you get something like this AIzaSyBcOT_DpEQysiwFmmmZXupKpnrOdJYAhhM
  • Set your API Key into enviroment variables
GOOGLE_CALENDAR_APIKEY=<Your API Key>
# Add these other variables as well, or you can place them as variables in the script below,
# either in the sh or in the service
CAL_BASE_URL=https://www.googleapis.com/calendar/v3/calendars
BASE_CALENDAR_ID_FOR_PUBLIC_HOLIDAY=holiday@group.v.calendar.google.com
  • Create the files in this gist and read their head comments where they indicate the path where they should be and also some recommendations
  • Change the working day start-end in sh script
  • Make the sh script as excecutable with this command chmod +x ~/.config/script/init-workspace.sh
  • Enable service with command systemctl --user enable workapps.service
#
# Make it script as excecutable
# chmod +x ~/.config/script/init-workspace.sh
# $HOME/.config/script/init-workspace.sh
#
#
#!/bin/env bash
START_WORK_DAY="monday" # Change this
END_WORK_DAY="friday" # Change this
CAL_REGIONS=("es.en" "es.bo") # On here add your regions of work, like lang.country, example: es.bo, es.es, en.uk
#
# Necesary variable, not touch
#
U=$2
DOW=$(date +%u)
START_WD=$(date -d $START_WORK_DAY +%u)
END_WD=$(date -d $END_WORK_DAY +%u)
TODAY_AM=$(date -d "00:01" -u +"%Y-%m-%dT%H:%M:%SZ")
TODAY_PM=$(date -d "11:59pm" -u +"%Y-%m-%dT%H:%M:%SZ")
function launch_work_day {
#
# EDIT THIS
#
# Launch Slack
slack -u -s $U &
# Enable and start your VPN if needed
# systemctl start vpnclient &
# Start Email Client
mailspring --background $U &
# Show Welcome Notification to amazing work :D
sleep 2m && notify-send -u NORMAL "System Notification" "Have a good work day"
}
function stop_work_day {
#
# EDIT THIS
#
# Stop all your work services or daemons
# It is for not have trash on not working days XD
systemctl stop vpnclient &
exit 0
}
if [ "$1" = "end" ]; then
stop_work_day
fi
if [[ $DOW -lt $END_WD && $DOW -gt $START_WD ]]; then
HOLIDAY=0
for region in $CAL_REGIONS; do
url="${CAL_BASE_URL}/${region}%23${BASE_CALENDAR_ID_FOR_PUBLIC_HOLIDAY}/events?key=${GOOGLE_CALENDAR_APIKEY}&timeMax=${TODAY_PM}&timeMin=${TODAY_AM}&timeZone=UTC"
res_json=$(curl -s $url)
if echo $res_json | jq -e '.items[]'; then
HOLIDAY=$(echo $res_json | jq -r '.items[] | .summary,.start.date')
break
fi
done
if [ "$HOLIDAY" = "0" ]; then
launch_work_day
else
sleep 2m && notify-send -u NORMAL "${HOLIDAY[0]}" "${HOLIDAY[1]}"
fi
fi
#
# $HOME/.config/systemd/user/workapps.service
# For enable you need run `systemctl --user enable workapps.service`
#
[Unit]
Description=Launch apps use in work only if not holiday
[Service]
Type=simple
ExecStart=%h/.config/scripts/init-workapps.sh start %U
ExecStop=%h/.config/scripts/init-workapps.sh end %U
Restart=always
RemainAfterExit=yes
Environment="DISPLAY=:0" "XAUTHORITY=%h/.Xauthority"
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment