Tuesday, April 1, 2014

use AppleScript and Automator to retrieve homework assignments

When I receive my Linear Algebra homework assignments, it's on a simple HTML web page that looks like this:

For the longest time, I have written the assignments down in my planner by hand, but I recently figured out a much better way to do this. Using a combination of Automator and some AppleScript, I was able to create an application to pull data from this web page, format it with appropriate due dates, and create reminders on Apple's stock 'Reminders' app. When the scripts run, I end up with a nice, segmented list of Reminders for my homework that ends up looking like this: 


To make things a bit more generalizable, I have it set up so that you pull up the assignment page in Safari and then run the service. It is dependent on having the assignment page in the current tab in Safari. If it is not currently open, it won't work at all.

To start, open Automator and create a new service. Call it whatever you want. I called mine "Get Homework"

First thing you want to do once you have created the service is create a new Text variable as a placeholder for the due date of the assignment. Go to 'Variables' > 'Text & Data' > 'Text'. I called mine 'dueDate'. It doesn't matter what you call it or what the value is. 

Then you'll want to set up Automator so that it pulls the text from the webpage and filters the text to look for dates. This is accomplished by using the actions: "Get Current Webpage from Safari," "Get Text from Webpage," and "Extract dates from Text".  

Note: You'll also want to use the action "Copy Text to Clipboard" right after you use "Get Text from Webpage" and before you use "Extract dates from Text" You'll see why later...

So in the end, your workflow should look like this:


Now here's where AppleScript comes in. I need to clean up the string that was generated by the "Extract dates from Text" action. Right after "Extract dates from Text", create a "Run AppleScript" action that accepts the input from the previous action. This script will take the input from "Extract dates from Text", which in my case is always something like "on Thursday, 03/20/14". Now, Applescript can't typecast that string into a date object without removing the "on Thursday, " characters. Fortunately, in my scenario, my professor stays consisting in displaying the due dates. It's always the standard "XX/XX/XX" after a comma and a space. I use the following script to format the date appropriately so that it can be stored in my 'dueDate' placeholder. 


From there, the generated string should be stored in 'dueDate' via the "Set Value of Variable" action.
Now, this is where the clipboard comes in. After the "Set Value of Variable" action, create a "Get Contents of Clipboard" action that ignores the previous action's input. After that, Create a "Filter Paragraphs" action. For me, I filter paragraphs that begin with "Section". That will give me a list of three text items with values like "Section 5.2: 8, 10, 12, 16, 22". 

Now I also want to pass through the 'dueDate' to my next AppleScript. An interesting thing about Automator is that if you generate a value right after generating a different value, the second item gets tacked onto the end of the list. With this in mind, I place a "Get Value of Variable" action right after the "Filter Paragraphs" action. This will give me a list with all the items in the list above, plus my properly formatted due date.


From there, create another "Run AppleScript" action that accepts input from the "Get Value of Variable" action. In that action, I use the following code to typecast 'dueDate' from a string object to a date object. Then I loop through the remaining list items, creating a reminder for each one with a the due date. After that, I create the general Homework reminder with the same due date which reminds me to rewrite my homework to make it look pretty for grading.



Now save the service. You should be able to open the assignment page and select 'Safari' > 'Services' > 'Get Homework' and your reminders should be generated. 

This concept should be generalizable enough so that you can adapt it for your own purposes. Obviously the implementation will change slightly based on how your professor structures his or her assignment page. 

No comments:

Post a Comment