iOS Shortcuts App to make Calendar Events
End of the week Thinking about Automation
I’ve got a cup of tea in my hand and I’m wondering what I should do for the day. There is a little puzzle I would like to solve. This is an iOS or Mac automation problem. Each month I get an email in about the English-language films on in Platja d’Aro and I would like to have a way of automating taking the name of the film and the date and creating a calendar event. I’d like to do this with the Shortcuts application.
What would be good is if I could just send the text of the whole email to the beginning of the automation. Shortcuts would find the relevant information I want and take it to the end of the process. It will add the location information for the cinema and also add the travel time to the event. If it was just one film per month it may be a little bit easier. However, it has to find two film titles and dates in the text.
My first idea
My first idea was to select the name of the film and the date manually and copy it to the clipboard. I would then share that to a shortcut which will do the rest of the job.
I was wondering if I could use Regular Expression to find matches for the text. The films always are on a Thursday and that word is the first word of a line of text in the email. Could be a way of getting my code to find the date in the email. It should also be possible to use regular expression to find text which is a date. More difficult to find titles because each month they vary and don’t stand out from the rest of the text in a meaningful way.
When I have this information the next trick will be to put it into a calendar. It’s possible to use the Add New Event calendar action, but I think I’m going to have more luck with using Fantastical and probably using the X-Callback URL encoding to get all of the details into the event I want to create. I’m sure it will take much trial and error to make this work. It’s amazing what you can accomplish though when you persevere.
I might also have a look at doing this automation on the Mac too. On the
Banging your head against a brick wall
That’s what it feels like sometimes when you’re trying to work out how to do some coding. Especially when you are not a coder. To some extent I was doing quite well using Shortcuts on the iPad and iPhone. I could get the correct blocks necessary to make the shortcut kind of work. It seemed as though there were always just a little niggling problems to annoy me. I found Fantastical worked best when I had the title on the first line followed by the rest of information on subsequent lines. The problem came about with the information being presented with the date first, followed by the title. It then didn’t put all of the title into Fantastical. Getting more complicated by the minute.
This led me to find ways of automating getting the two parts of the data onto separate lines and in the right order. I looked at ways of doing this within the Shortcuts app. Then I tried to get that same job done in Drafts and I also ended up working out how to do it in Editorial. It got quite confusing going from one app to another trying to get the same thing done. Had to deal with different notation for the actions in each app. My preference was still to do everything within Shortcuts in order to make it as simple as possible. On the other hand, I was able to get Shortcuts to send data out to Editorial and to bring it back in again after it had been rearranged. Still not working right for me though.
A twist in the story
When I was trying to work this out I did try to use a date which was in the past. When I did this, Fantastical wanted to create the event in the following year. It makes sense because why would you want to create events for something which has already taken place. Even so, I did spend some time adding things into the code to try and stop that from happening. Sometimes it worked and sometimes it didn’t. Waste of time because I will really only do this with new emails containing dates in the near future.
I couldn’t work out why on some occasions it shows a completely different date to the text being given. Fantastical he supposed to be good at using plain language to extract the data to create events. What with the title having to be first, followed by the date and then I had to put in the proper address for the location, it’s clear Fantastical needs the data to be presented in a specific order. Had to throw things out of my automation and try again multiple times.
Appending to the clipboard
What I have found works efficiently is to grab the title which is supposed to be on the first line. That goes into the clipboard first. Then there is a shortcut I found and installed into Shortcuts which will take the date which I have selected and put it on a line after the title already placed in the clipboard. The actions within the shortcut I have been making takes the data and then adds the address for the cinema and throws it all into Fantastical. It works, but still is not as automatic as I would like it to be. Not the final solution as it was not automatic enough.
The road to success for my automation
I didn’t give up, but I did put the problem to one side for a day. Sometimes it’s good to do this to come back fresh and with new ideas. I went out for a drive around Barcelona hunting charging points for my electric car. In the evening time I relaxed and watched television, including the movie ‘Walk the Line’ which was a biopic based on the life story of Johnny Cash. I successfully put the automation problem I’d set for myself right to the back of my mind.
New day and new ideas
I found it was possible to throw in a big lump of text which included two lines which had my data into the beginning of the automation and extract just those two lines. I did this using Regular Expression. There is an action within Shortcuts which will match text and you to use a regular expression to get what you want. I had to do a bit of reading on a website which has all the information and tutorials on using Regex. To be honest it was quite complicated leading to a good bit of head scratching. In the end I was able to adjust one of the examples and try it out in an application called Expressions on my Mac. I had the text pasted into the search area of the application and I kept changing the regular expression until it selected everything I required. I got there in the end!
Working with the captured text
So now I had the text within the shortcut application but I still had the problem of getting the title of the movie in front of the date. If I didn’t do this the automation only put a couple of words into the title area for the event into Fantastical.
I found an action in Shortcuts which would split the text. The first option within this action I used was to split the two titles with dates into two lines. I didn’t need the word Thursday so I had the automation delete that word. I then split the text again using a custom search term. So this put the date on one line and the title on another line. I ended up with a list of four items.
I tried every which way I could think of to give each item in this list a variable. This would allowed me to put the two pairs of title and date together in the right order. Each time I tried the shortcut just kept getting confused. I was getting very confused myself at times. So I decided to try and split the automation into separate parts. This is a good plan because you can have a shortcut run other shortcuts. During my trials of ways to split up the text I found I could use the action ‘Get Item from the List’ to give me each of the four pieces of data. So I duplicated my shortcut so I had four of them. Each shortcut provided one of the four data points.
Bringing it all together in a controlling shortcut
So each shortcut gives me a piece of text. I started a new controller shortcut bringing in to film titles and two dates. This was easily done by running the shortcut for each data point to an action Get Text from Input and then setting a variable. I could then use each of these variables to put the text in the right order for adding an event into fantastical.
My first attempt was to use X callback URL for sending the data to fantastical. I did it this way to add an address for the cinema to the calendar event. It didn’t work the way I wanted it to. It wanted me to confirm by tapping on the add button in fantastical for each movie.
So what did instead was to create a text output by combining three variables. Two of them from the shortcuts running in this controller shortcut and the other variable was the address of the cinema. This worked the way I wanted to work because there is a switch to turn off the editing within Fantastical. At the end of all this the shortcut runs perfectly without any extra input from me.
My shortcut automation in use
I use the email application Airmail 3. There is a small amount of automation available within this app. I created an action in Airmail which sends the content of the email to my newly created shortcut in the Shortcuts app. It all runs in the background and I don’t need to do anything else. We have a winner!