More fun with applescript; a play/pause button for Mixcloud

I’ve been using Mixcloud in the office lately. Progressive house mixes are quite nice for work as they are long and not very distracting. However, when something does catch my attention it’s a pain to find the tab and click on the big “pause” button. The solution only took a minute or two:

#! /usr/bin/osascript
tell application "Mixcloud"
activate
end tell

tell application "System Events"
tell process "Mixcloud"
keystroke space
end tell
end tell

This requires the Mixcloud website to be wrapped into an individual application, which is easily accomplished via Fluid. Doing this also has the advantage protecting the music from stuttering when single-threaded Safari is suffering under load.

Finally, the script is bound to shift+F4 using Quicksilver. (It needs to be saved with the “.applescript” extension for quicksilver to recognise it as an executable script.) A few other fun keybindings I use are F4 for iTunes play/pause, F3 to open a periodic table and F6 to open a de-stressing Google Image Search for fluffy kittens.

It would be an improvement if F4 could detect whether iTunes or Mixcloud is playing and start/stop them intelligently. Suggestions are welcome!

Advertisements

Fun with Applescript; automating obstinate programs

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 "LibreOffice"                                                              
activate                                                                                    
end tell                                                                                    
                                                                                            
tell application "System Events"                                                            
tell process "LibreOffice"                                                                  
click menu item "Save" of menu "File" 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 "VESTA"                                                                    
activate                                                                                    
end tell                                                                                    
                                                                                            
tell application "System Events"                                                            
tell process "VESTA"                                                                        
click menu item "Export Raster Image..." of menu "File" of menu bar 1                       
delay 1                                                                                     
keystroke return -- Agree to default filename                                               
delay 1                                                                                     
key code 53 -- "Escape" the scaling dialogue                                                
delay 1                                                                                     
keystroke return -- Get rid of congratulatory "you made a file!" dialogue                   
delay 1                                                                                     
keystroke "w" 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!

Valence Kohn-Sham eigenstates of rectangular S4
Kohn-Sham valence eigenstates of rectangular S4. Data from all-electron density functional theory calculation with PBE0 hybrid functional as implemented by the FHI-aims quantum chemistry code. Blue borders indicate occupation by a single electron.

Workflow: Chemical engineering undergraduate with a netbook

A few people have recently made remarks or asked about my netbook-based note-taking and workflow. I found a few blogs and articles helpful while developing my own system, so I thought I’d share the details here where other students or academics might stumble on them and find a useful idea or two.

Last year I finished an undergraduate master’s degree in chemical engineering. For the first three years of my degree I had trouble with note-taking and revision. The main causes of this were:

  • My own poor handwriting
  • The variety of formats in which we were provided with notes and resources
  • The ease of losing miscellaneous scraps of paper
  • Disagreement with lecturers on the best way to study their material

I was somewhat wary going into my fourth and final year that things were only going to get more difficult. It was time to sort this out. What I came up with was highly successful; not only did I have an excellent set of notes for revision, but I was able to work on design projects and coursework using a range of computers, fairly seamlessly.

Netbook: ASUS Eee PC 1001P

This was an affordable machine (I paid ~£240 for it in mid-2010) with a robust design, excellent battery life and sharp, matt 10″ screen. The screen is undermined by a distracting, glossy bezel, so I covered this with matt insulating tape. This has the bonus feature of covering the webcam when not in use. I tried to track down a netbook without Windows installed, but this didn’t appear to be an option for the current generation of netbooks.

Operating system: Ubuntu 10.04 Netbook Edition

Having dabbled with running linux in virtual machines on my iMac, I was keen to get stuck in with a more dedicated machine. Ubuntu Netbook Edition was an obvious choice, with a highly netbook-oriented user interface, but with the large community and package repositories of Ubuntu. I have resisted upgrading from version 10.04 as I understand there have been several UI changes, and I am very happy as it is. There were a couple of snags setting it up, most notably the erratic brightness control and unsupported wireless drivers. I found a very helpful blog, edited a configuration file, installed the wireless drivers with ndiswrapper, and I was up and running. I also encountered a few problems as a result of using wubi; while a nice toy for testing out Ubuntu on a windows machine, it is not very trustworthy for long-term use, and I would advise against it.

Once I had Ubuntu Netbook Edition up and running it has been a exceptionally smooth, consistent experience. The Software Centre is quite slick but I generally prefer to use apt-get from the command line. This post isn’t really the place to go into the relative merits of operating systems and package management, suffice to say that I currently plan to stick with Ubuntu and its spin-offs, largely due to the ease of installing software.

Annotating slides: Xournal

I try wherever possible to use software that is cross-platform. However, to the best of my knowledge, Xournal is only available on Linux and related platforms. It is a fairly simple package, designed for use with a stylus as a pseudo-notepad. However, it also offers very flexible PDF annotation. Where lecture notes have been made available in advance, I have converted them to PDF (if necessary) and opened them as a background in Xournal. From there I can type notes and sketch diagrams (usually in a contrasting colour and font) as though scribbling on a print-out, but with a few key advantages:

  • If the block of text is larger than expected, I can re-position or re-size it
  • Can correct mistakes cleanly
  • Can insert duplicate slides if need to take more notes than there is room for
  • Can insert blank slides to account for extended rants
  • No need to print in advance; can download and set up while in the lecture if necessary

The final step of the process is to export to PDF. From there, the notes can be studied from virtually any device.

Technical note-taking and report-writing: LyX

LyX is a free, cross-platform and user-friendly front-end for LaTeX. LaTeX is a typesetting system which is very popular among mathematicians and scientists due to its flexibility, power and relative ease in typesetting complex equations. However, working in pure LaTeX is rather intimidating. Here is a simple document prepared using LaTeX code, the LaTeX code used to generate it, and the LyX environment for the same piece of work:

LyX provides a more accessible interface for LaTeX, with many shortcuts for common functions, an accessible interface with plenty of clickable buttons, and you edit an abstract preview of the document. The spacing, font and page layout are not accurate, and should not distract you from the task at hand. Critically, maths and symbols are interpreted, so you can see what your equation will look like as you edit it. The shortcuts, while optional, are quick and ingenious. You have to see or try it in action, really – perhaps I will branch out into video and screen capture some day. By using LyX with keyboard shortcuts I was able to keep up with those lecturers who like to “chalk-and-talk” their derivations.

Diagrams: Inkscape and GIMP

Not much depth is needed here on this one; if you aren’t aware of these two programs you should probably check them out. They’re not quite as powerful as Adobe’s offerings, but they are capable of more than many people give them credit for. If you need to draw or trace a diagram, crop an image, remove some background elements and tweak the colour balance, these two programs will be more than sufficient.

Backup and file-sharing: Dropbox

The slight loss of control and security concerns are compensated by a very slick implementation. Essentially: there is a folder on your computer. The same folder is on any other computers you own. And the internet. It holds up to 2Gb (with various ways of expanding this). It’s a real folder, and you can use aliases, command line, file managers etc. however you like. Enjoy. In combination with other cross-platform software, this leads to many delightful situations where it simply does not matter which computer you are using.

General thoughts

My workflow has changed somewhat as a postgraduate student, but I maintain that this was a suitable and effective setup for an undergraduate engineer, and I would recommend it to others. Feel free to post below if you have any questions or comments! Finding the perfect workflow has become something of an obsession, and is analogous to the guitarist’s quest for “tone”. Hopefully some of the ideas here will help people on their own adventures.