Applescript is a rather under-appreciated thing.
I must admit I’m not fond of it’s syntax, which manages to achieve Uncanny Valley-style “natural language” while still being wholly unintuitive. Also, as someone constantly bouncing between OSX and GNU/Linux machines, it makes much more sense to learn a scripting language that I can use on any machine. Still, I have found a couple of difficult tasks recently which are elegantly solved by Applescript.
1. Home working: remote saving of applications
Like many people I use Dropbox to keep files in sync that I need to access at short notice from anywhere.
One of these files is a big LibreOffice spreadsheet, listing details of all the density functional theory calculations in my project. As this includes planned, queuing and currently-running calculations, I frequently need to access this to keep my notation and folder structure straight. One of the benefits of being a PhD student with a computational project is that it offers great flexibility in managing my schedule and working remotely. My workstation (an iMac) is on constantly, and I can log in remotely through an ssh terminal to access my files and tunnel to HPC facilities. However, there is a snag; files are only updated when I save them… There is no great incentive to save frequently when editing a simple spreadsheet on a very stable computer. It is only when I get home that realise that I now cannot edit my catalogue of calculations without running into all kinds of Dropbox conflicts and messy file merges.
Or I could just run this:
#! /usr/bin/osascript tell application &quot;LibreOffice&quot; activate end tell tell application &quot;System Events&quot; tell process &quot;LibreOffice&quot; click menu item &quot;Save&quot; of menu &quot;File&quot; of menu bar 1 end tell end tell
Yes, that is just as filthy as it looks: remotely click “save”. Wait for Dropbox to sync. Open on local machine, get back to work.
2. Generating a lot of pretty pictures
VESTA is one of the best-looking packages out there for visualising molecule and crystal structures.
In particular it uses hardware acceleration well, draws beautiful isosurfaces and has a range of lighting options. Tragically it lacks any kind of scripting interface (as far as I am aware). For the target audience of solid-state and org chemists, this is perhaps not a huge problem. Computational guys like to think big…
Recently I’ve been looking at allotropes of sulfur. Whether and how different allotropes are bonded is quite interesting, as is the significance of spin polarisation. Having carried out hybrid-level DFT calculations, it occurred to me I had a pretty good map of the molecular orbital structure. But I was damned if I was going to open the File dialogue once for every orbital, let alone do that every time I tried a minor variation of the structure or DFT parameters. About an hour of Stack Exchange and experimentation later:
#! /usr/bin/osascript tell application &quot;VESTA&quot; activate end tell tell application &quot;System Events&quot; tell process &quot;VESTA&quot; click menu item &quot;Export Raster Image...&quot; of menu &quot;File&quot; of menu bar 1 delay 1 keystroke return -- Agree to default filename delay 1 key code 53 -- &quot;Escape&quot; the scaling dialogue delay 1 keystroke return -- Get rid of congratulatory &quot;you made a file!&quot; dialogue delay 1 keystroke &quot;w&quot; using command down -- Close file, ready for next one end tell end tell
Again, absolute filth. A few tips and tricks had to be employed here: key code 53 seems to be the preferred way of sending an “escape” key message.
The 1 second delays are probably longer than necessary but leave time for the dialogue boxes to open.
Simple globbing and for loops with bash allowed me to open a stack of .cube files and paste the output images together with Imagemagick tools. The result is, I think, rather fetching (apologies for length). If you studied molecular orbital theory at school, then you should have a fair stab at interpreting this in terms of combinations of conventional sigma- and pi-bonds. Note that of the 16 combinations of valence orbitals, we have four fully-bonding, four fully-antibonding and eight mixed bonding and antibonding molecular orbitals. They aren’t necessarily in the order you might expect, however!