Using Shift2CalDAV on MacOS and with iCloud

Demo video of how Shift2CalDAV looks/works

So if you’re like me, you hate having to log in to Target’s site to see when you work next. The app for your phone has no way to export to your calendar, either. That’s where Shift2CalDAV comes in – it can automate logging into myTime and generate ical events and push them to a CalDAV server. An ical event is just a tiny text file with things like start and end time, notes, and location. Currently it requires Google Chrome, a special program called a “WebDriver”, python 3, your Target TM number, the answers to your security questions for Target, and of course a calendar.

The basic steps to getting this working are:

  • Finding your iCloud calendar’s address
  • Creating an app-specific password for your iCloud account
  • Creating a configuration file that Shift2CalDAV reads to know how to find your calendar and how to answer the questions when logging in
  • Getting Chrome and chromedriver, and allowing chromedriver to run through the security settings (if you download it from my GitHub repository – if you download and patch it yourself it shouldn’t be required)
  • Installing Homebrew and some required python libraries
  • Running Shift2CalDAV

Sounds good, let’s start

The first thing you’ll want to do is open Terminal.app. Press ⌘ + Space and start typing Terminal.

Once you see “Terminal” show up, select it to run it.

Upon opening it, you’ll be greeted with a text box that is awaiting commands to run. I recommend creating a new directory just to keep your home folder less cluttered. I personally use “src” but you can use whatever you like (or nothing). The lines below show you how to make a new directory and change (cd) to it so it becomes your current working directory. Anything after a #will be ignored by the command prompt, so it’s fine to copy and paste that part.

mkdir src #make a folder
cd src #make that folder the working directory
git clone https://github.com/TidOS/Shift2CalDAV.git #download the program
cd Shift2CalDAV #go into the program's folder
Don’t worry that yours looks different

Finding your calendar’s URL

#!/bin/bash
BASEURL=`grep "caldav.icloud.com" ~/Library/Calendars/*/*.plist | grep principal | sed 's/http/\nhttp/g' | grep "https" | cut -f 1 -d "<" | rev | cut -d / -f3- | rev`
echo ADDRESS: $BASEURL/calendars/
CALENDAR=`grep "caldav.icloud.com" ~/Library/Calendars/*/*.plist | grep principal | cut -d "/" -f 6`
#echo CALENDAR: $CALENDAR
#clear
for i in `grep "calendars/" ~/Library/Calendars/$CALENDAR/*/*.plist | awk {'printf("%s\n",$2)'} | cut -d / -f 4`; do echo possible name $i; done
note above: the text box is quite long, you’ll need to scroll sideways to get all the text

You can run ./iCloudCalFind.sh or, the above code, copied and pasted into your Terminal should print out your available iCloud calendar URLs which you will need for the configuration of Shift2CalDAV. You’ll have to take my word for it that this does nothing harmful if you’re not familiar with bash/*NIX commands. Basically it searches in your computer where your synced calendars are stored for a calendar that has “caldav.icloud.com” in the name and then finds the calendars associated with it and prints it out. This will only work if you’re signed into iCloud on your Mac and have synced your calendar at least once. It’s a big ugly block of commands that could (and if I was really distributing this stuff SHOULD) be made into a nice little script but it should work. Hopefully. 😂

On my iCloud account I used while testing this out for this guide, there were two calendars by default. One was called home and one was called work. If you have multiple, take note of the one you’d like to use. They’ll be listed as possible names.


Make an app-specific password

This is required and really helps to protect you in case you don’t trust my code with your password. Go to https://appleid.apple.com and sign in. You’ll be brought to the account management page. In the Security section, choose “Generate Password…” Here, it will ask you to give an app name. It does not really matter what you choose, I would suggest using Shift2CalDAV so you can recognize it later. Save the password it gives you, you will need it to access your iCloud calendar from a non-Apple app. You can revoke the password at any time by choosing Edit on the Security tab, but as far as I know you can not recover the password so if you forget it you’ll have to make a new one.


Creating the credentials file

Now, you can browse to that folder in Finder and we need to make a copy of sample.cfg and rename it to credentials.cfg (or just rename the existing file to credentials.cfg).

Finder after copying sample.cfg to credentials.cfg

Open credentials.cfg in TextEdit. For address, you will put what you got from the script you ran above. It will look something like this

[url]
address = https://p69-caldav.icloud.com/6372266852/calendars/
name = work
user = coolguy2@icloud.com
pass = dips-xyhh-zdvg-xxna

Assuming my iCloud account is coolguy2 and my password is dipx-xyhh-zdvg-xxna. The numbers will be different, and the ones above are fake. You need to use the ones you got from that big nasty block of code above. The password is NOT your normal iCloud password, it’s the one that you generated specifically for this app.

The [secrets] section should be fairly self-explanatory. employeeID is your Target TM number, the one you’d use on a register to ring people up – the one you use to log in to check your schedule now. Here, password is your TARGET password – the one you use to log in to the website now.

The [questions] section is a little less obvious. There are three questions that the site will ask you if you choose to answer security questions as the second part of the two-factor authentication at Target’s site. You’ll need to log in manually a few times to figure out what your three questions are if you don’t already know them. Then, pick out a keyword from the question and use that for the keyword option and your answer to it as the answer field.

In the example, the three questions would be:

  • What is your favorite restaurant? – tarbucks
  • Where were you born? – Minneapolis
  • What is your pet‘s name? – bullseye

Do not worry if your answers have spaces, just include the spaces in your answer.

The last few options are optional. headless, if set to yes will mean you do not see the browser come up at all. Nice for automating/scheduling the script as a task to be run once a week or something. thisweek and nextweek decide if the script will generate a schedule for each of those weeks. I suggest setting both to yes, but thisweek is set to no in the example so change that if you want.


Install Homebrew

Homebrew is a package manager that lets you install more software that you’d normally find on a programmer’s machine. Installing it is easy and listed right at the top of their homepage:

 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install required python libraries

It’s been a while since it mattered what directory we were in inside the Terminal, so make sure you have navigated to the Shift2CalDAV directory. If you’re not sure if you are in the right place any more, you can find the folder in Finder and control-click/right-click the folder and go to Services -> New Terminal at Folder. If you don’t have that for some reason, open a Terminal and type cd ~/src/Shift2CalDAV – assuming you followed my directions at the start.

Once you’re in that directory, run pip3 install -r requirements.txt

pip is a package manager for python, just let it do its thing. and you’ll be ALMOST ready to run the script.


Getting chromedriver

Go get Google Chrome for OS X if you haven’t already. I think if you made it this far in the guide you can handle that! As of the time of writing, Google Chrome is at version 88 and so is chromedriver, which is included in the git repository you cloned earlier. I have already pre-patched the chromedriver in the git repository. If you decide to download chromedriver on your own (certainly don’t blame you, I wouldn’t want to run a random guy’s binaries either), you will need to run: perl -pi -e ‘s/cdc_/dog_/g’ /path/to/chromedriver . If you followed this guide, it won’t be necessary.


ohmgosh is it finally time to run it?

While in the Shift2CalDAV directory, run the script called RUN.

chmod +x RUN #marks the script as executable
./RUN #runs it

If all goes well, the script will run and do its thing. If not, you’ll get a bunch of error messages in your Terminal to try and decipher.


Conclusions

If you have my contact info you can contact me if you get stuck somewhere or are using some OS other than macOS – I’ve successfully run this on GNU/Linux and Windows as well. If you don’t have my info, you can use the comments here on the blog and I will possibly see them and will help if I can.

This was kind of fun to write up. I actually had to edit a good bit of code to accommodate iCloud stuff and accommodate the fact that there exists CalDAV principals with more than one calendar. I had never tried this script out on iCloud before, just my own NextCloud server. I think it will work – I followed the guide on my own account and it did work. There was a lot of trial and error in figuring things out so it’s definitely possible something won’t work as expected. I did not test the homebrew installation steps though because I already had it installed on my system.

As I wrote this, I found a lot of things that could be streamlined if I was going to have people actually use this. I didn’t write it with the intention that anyone would ever have a use for it, but it’s grown enough in functionality that it might be time to automate a lot of what is written above and package things up in a way that a non-tech person could use it because I think people could make use of a tool like this.

Print Friendly, PDF & Email