Waking the computer to allow AppleScript to run

I have a number of AppleScript applications that need to run at odd times. These maintenance tasks often attempt to run while the computer is sleeping. Particularly those that rely on UI scripting do not function during this period.

This most flexible way of dealing with this is to manipulate the power management settings directly via the pmset(1) command.

The variety of options available using pmset is staggering and beyond the scope of this post. Here’s what I do to wake the computer up at specific times so that scheduled AppleScripts can run:

~|⇒ sudo pmset repeat wakeorpoweron MTWRFSU 12:29:00
~|⇒ sudo pmset repeat wakeorpoweron MTWRFSU 23:49:00

Now my 12:30 PM and 11:50 PM scripts will run just fine.

Edit 2016-04-18: Actually my scripts don’t run just fine because pmset does allow setting of multiple wakeorpoweron events like this. Only the last one set is retained. However, you can use the root’s crontab to do that as long as you schedule the cron event to deliver the pmset schedule before it’s needed. Here’s the idea:

@reboot pmset repeat wakeorpoweron MTWRFSU 23:49:00
00 12 * * * * pmset repeat wakeorpoweron MTWRFSU 12:01:00
02 12 * * * * pmset repeat wakeorpoweron MTWRFSU 23:49:00

An easier way to automate synchronization of Anki profiles with AppleScript

After waking up this morning with my mouse locked onto the Anki icon in the dock and trying to figure out how to get Activity Monitor up and running so I could force quite my Automator application that I described yesterday I figured it was back-to-the-drawing board.

I’d like to have used the Accessibility Inspector to manipulate the PyQt objects in Anki’s windows, they aren’t exposed in a may that you can script them. But System Events rules all.

When Anki launches it offers a dialog box with profiles to sync (assuming you have multiple profiles.) Using AppleScript and System Events scripting, you can drive the keyboard as it manipulates the PyQt interface. Here’s my solution. Yours may vary depending on where the profile in question lies in the list.

tell application "Anki" to launch
delay 2.0

tell application "System Events"
	key code 125 -- down arrow to point at the profile to sync
	key code 36 -- Enter key
	delay 10.0 -- time to sync
	key code 12 using {command down} -- quit
end tell

Much less painless than Automator.

Scheduling synchronization of Anki databases on OS X

While working on a project to automatically collect statistics on my Anki databases (stay tuned…) I worked out a system for scheduling synchronization from my desktop OS X machine.

Prerequisites

  • LaunchControl is a GUI application that lets you create and manage user services on OS X
  • Anki is a spaced repetition memorization software system

The solution relies on Automator. Normally, I don’t care much for Automator. It has too many limits on what tasks I can accomplish and workflows created with it are often fragile. However, in this case, we take advantage of its workflow recording feature. We’re going to record the process of opening Anki, selecting the profile to sync, then quitting Anki. This sequence of events ensures that the database on the local system is synchronized with the remote version.

Resizing of images for Anki with Hazel and ImageMagick

I use Anki to study foreign language vocabulary. It’s the de facto spaced repetition software for memorization.^[Yes, I’m aware that others exist. I’ve tried many but always have come back to Anki.] When making flashcards for language learnings, I try to use imagery as much as possible. So a card may have a Russian word on one side and just an image on the opposite side. (Since I already know the English word that the image represents, why not try to engage a different part of the brain to help with memorization?)

Writing Hexo filters

Hexo, the static blogging system that I use is very extensible and provides numerous hooks into generation pipeline.

While working on a Russian language blog that’s coming online soon, I had the opportunity to write a filter to render Cyrillic text in a different font than the rest of the body text.

Markup filter use case

I wanted to set the Cyrillic text apart both in color, typeface, and font weight. Although I could have extended Hexo using a new tag, I decided to use a filter so that after rendering HTML anywhere on the blog, items demarcated by double pipes || would be replaced by a new <span>.

Fine-tuning caching for S3-hosted static blogs using AWS CLI

Because the blogging system that I use doesn’t apply finely grained object-level caching rules, I end up with objects such as images that cache appropriately but an index.html page that does not. I don’t want client browsers to hang on to the main index.html page for more than an hour or so because it should update much more frequently than that as its content changes.

It’s possible that I could dig around under the hood of hexo and create a version that applies customized caching rules. Instead, I make a second pass over the content, adjusting the Cache-Control and other metadata according to my needs. For this task I use the Amazon Web Services command line interface AWS-CLI.

Modern textbook design: an architecture for distraction

The design of textbooks in common use at all levels from elementary school through high school are appallingly bad. I’ve come to this conclusion after several years of carefully looking at my sons’ books as they went through public middle and high school. What follows is a critique of very common design “features” in these books in reference to visual information design principles. Since I’m not a subject expert in the content of the disciplines presented, I’ll just refer to the visual design, typography and information design principles in general.

Trump, the conspiracy theorist

One of the most striking features of the GOP front-runner is his special fondness for conspiracy theories. From the (non-existent) connection between vaccines and autism to the “real culprits” behind 9/11, he shows the typical clustered endorsement of multiple conspiracy theories.

The question about whether this a form of pandering or a genuinely held set of perspectives is interesting, though barely relevant. In the former case, the abandonment of reason to achieve a political goal is an egregious fault. In the latter case, it sheds considerable light on crucial decision-making capacities. Endorsement of conspiracy theories requires a set of cognitive biases that fundamentally hobble evidence-based decision making.

The spread of anger in social networks and its implications for political violence

An ingenious study using the massive Weibo network revealed insights into the spread of certain emotions through social networks. Weibo is a social network platform not unlike Twitter. It is also hugely popular in China with millions of users making it an ideal platform for understanding how emotional states between socially-connected users correlate with each other.

But the highest correlation by far was among angry users. Rui and co say anger strongly influences the neighbourhood in which it appears, spreading on average by about 3 hops or degrees. “Anger has a surprisingly higher correlation than other emotions,” they say.

anki_tool: low level manipulation of Anki databases

Speaking of Anki, here’s a Swiss Army knife of database utilities that provides searching, moving and renaming functions from the command line.

On GitHub.

You can do things like this to rename and collect tags:

$ anki_tool mv_tags '(dinosaur|mammal)' animal

Looks cool.