Monday, April 27, 2015

Automate tasks with AppleScript, Fake, and the keychain

You can make life a lot easier on your Mac if you learn how to automate things. The standard approach to do this on Macs are to use Automator (see How to use Automator: What Automator is and how it works from Macworld UK). If you include AppleScript, Fake, and the keychain in your toolchain, you can achieve even more. As an example I will develop an automated task to create a new email alias using a web portal. This example is used since it includes user input (email alias), access of password in keychain, filling in and submitting forms on web pages, and return data to the user though the clipboard. An example using Automator is also available.

AppleScript

The main parts of this example are an AppleScript script and a Fake workflow. We also expect that the password of the user is stored in the keychain [1]. The first thing we do is to fetch the password for the web portal from the keychain:

  set domain_name to "mydomain.com"
  set user_name to "myusername"
  set pwd_cmd to "security find-generic-password"
  set pwd_script to pwd_cmd & " -a " & user_name & " -w"
  set pass_wd to do shell script pwd_script

The next step is to prompt the user for the email alias (including converting the input to lower case [2]):

  set dialog_txt to "New email alias (<alias>@" & domain_name & "):"
  display dialog dialog_txt default answer ""
  set email_alias_case to text returned of result
  set email_alias to _string's lowerString(email_alias_case)

The typical use for the user of a new email alias is to type the new email address in to a web form. To make this easier for the user we'll copy the new email address to the clipboard:

  set the clipboard to email_alias & "@" & domain_name

The final part of the AppleScript is to execute the Fake workflow. We have to transfer three parameters (variables) to the workflow:

  tell application "Fake"
    set variable with name "emailAlias" to email_alias
    set variable with name "userName" to user_name
    set variable with name "passWd" to pass_wd
    activate
    open "Users:aa:Applications:MakeEmailAlias.fakeworkflow"
    delay 1
    run workflow with variables { \
      emailAlias:email_alias, \
      userName:user_name, \
      passWd:pass_wd \
    }
    wait until done
    close front document
    quit
  end tell

The only thing missing in the AppleScript code above is loading of the text string manipulation library _string.scpt [2]:

  set _string to load script alias ( \
    (path to library folder from user domain as text) & \
    "Scripts:Libraries:" & "_string.scpt")

Fake workflow

The Fake workflow receives 3 parameters (variables) from the AppleScript; the new email alias (emailAlias), the username (userName), and the password (passWd) to log in to the portal. In the Fake workflow we use these variables to fill in the correct values at web form. The following Fake workflow is an example, and it as to be updated based on the actual web portal you are using. The example workflow consists of 4 steps (and 10 Fake workflow actions):

  1. Go to login web page:
    • Load URL: example.com
  2. Log in with username and password (and wait a second):
    • Set Value of HTML Element:
      • with id: user
      • to: {userName}
    • Set Value of HTML Element:
      • with id: password
      • to: {passWd}
    • Click HTML Element:
      • for XPath: /html/body/div/...
    • Delay:
      • for: 1.0 seconds
  3. Select web page where email aliases can be added:
    • Click HTML Link:
      • with text: email@mydomain.com
  4. Add email alias (and wait tw seconds):
    • Focus HTML Element:
      • with id: newalias
    • Set Value of HTML Element:
      • with id: newalias
      • to: {emailAlias}
    • Click HTML Element:
      • with id: submit
    • Delay:
      • for: 2.0 seconds

In the example above we use different approaches to identify the elements on web pages (with id, for XPath, with text). In your case you should use the approach that is easiest for the web pages you are scripting. Fake provide a feature where you can drag the id of an element to the workflow action id value.

Notes

  1. To store and access data in the keychain we use the security command line interface. To add a new account name with password pwd to the keychain you can do the following command:
      security add-generic-password -a name -s service -w pwd
    Then we can print this password with the following command:
      security find-generic-password -a name -w
    (In the first command service is a human readable name describing your service.)
  2. In the examples above I expect the AppleScript library _string.scpt from Brati's Lover AppleScript library.

Friday, March 20, 2015

Including source code in papers and on the web

I've written many papers and a lot of web pages that includes source code. Since Python code was an important part of my Dr. thesis, I even wrote a tool to generate a pretty-printed Python code for LaTeX (this was written in 1997, but since I've been using it ever since it has had a few minor bug fixes since then; however, it needs Python 1.6 and it was the first Python program I ever wrote!). Just by accident (being sidetracked when I was checking out Middleman extensions) I rediscovered Pygments yesterday. I know I've seen it before, but I had forgotten all about it. This time I checked it out, and I think it will finally replace my home-written tool. Since I write most of my papers in LaTeX and I use BSDmake to do the magic, I have included a few new rules in my Makefile (using my old ltk.mk file):

PY2TEX =	pygmentize -f latex -P "verboptions=frame=single" -O linenos
SRCSTYLE =	srcstyle.sty
STYLETYPE =	default
MKSTYLE =	$(PY2TEX) -S $(STYLETYPE)

.include	"$(HOME)/lib/mk/ltx.mk"

$(LTXSRC):	$(SRCSTYLE)

$(SRCSTYLE):
		$(MKSTYLE) > $(.TARGET)

In the LaTeX document I have to include the generated style file and the fancyvrb package (and for most styles the color package). If the source code included in the document is in the file src/mypycode.py, then you will find this in the LaTeX document (ltx.mk expects to find source code files in the src directory):

\usepackage{fancyvrb}
\usepackage{color}
\usepackage{srcstyle}
...
\begin{figure}
  \begin{minipage}{0.9\linewidth}
    \input{src/mypycode.tex}
  \end{minipage}
  \caption{The caption text describing mypycode.}
\end{figure}

Pygments can be used to generate TeX, LaTeX, HTML and many other formats. It supports a wide range of programming languages, and can easily be extended to other formats and other programming languages. It is highly configurable and flexible, and the command line way of using it is the simple approach. For full control and flexibility you write your own tool using Pygments as a Python library (and it supports both Python 2 and 3).

The styles, programming languages, formats, and filters available can easily be listed with these commands:

pygmentize -L styles		# styles
pygmentize -L lexers		# programming languages
pygmentize -L formatters	# formats
pygmentize -L filters		# filters
pygmentize -L			# all

Tuesday, June 3, 2014

The Swift (short) history

The language announced at WWDC yesterday came as a big surprise to almost everyone. At Chris Lattner's Homepage you can find a short summary on the history of the Swift programming language. He started work on the language in July 2010, and it became a major focus for the Apple Developer Tools group 3 years later. Other people started to contribute to the project in late 2011. He mentions Objective-C, Rust, Haskell, Ruby, Python, C#, CLU as inspirations for the language itself, and Bret Victor's ideas and Light Table as inspiration for Xcode Playgrounds (Playgrounds allow you to edit the code listings and see the result immediately). He says that Playgrounds and REPL (Read-Eval-Print-Loop) were a personal passion of him. REPL is an interactive version of Swift built into the debugging console of Xcode. You can use Swift syntax to directly evaluate and interact with your running app in the Xcode console.

A new language: Swift from Apple

I've just developed my first application in Swift, the new language announced by Apple yesterday. I downloaded the Swift book on my iPad during the Keynote, and started programming immediately. When I got hold of Xcode beta 6 I got it running in seconds.

My first impression: Swift is a modern language. It is closely related to popular scripting languages, like Python and Ruby, but is is a compiled language. It is also a type safe language, but when the type is obvious, the programmer doesn't have to bother with it (the compiler infer its type). The Swift syntax is easy to read and not strange and new. Important features that often make the syntax of a language complex and unreadable is actually quite clear in Swift (including generics, protocols and extensions). This is a class Amplifier implementing a protocol VolumeControl:

protocol VolumeControl {
    var level: Int { get }
    mutating func increase() -> Int
    mutating func decrease() -> Int
    mutating func mute()
}

class Amplifier: VolumeControl {

    var level: Int {
    didSet {
        if level < 0 { level = 0 }
    }
    }
    var step: Int
    var source: String

    init(level: Int = 0, step: Int = 1, source: String = "dvd") {
        self.level = level
        self.step = step
        self.source = source
    }

    func increase() -> Int {
        self.level += step
        return self.level
    }

    func decrease() -> Int {
        self.level -= step
        return self.level
    }

    func mute() {
        self.level = 0
    }

    func selectSource(source: String) {
        self.source = source
    }

}

The protocol specifies one attribute with a getter and three methods. The class implements this protocol by providing access to the attribute and implementations of the methods. The didSet on the property is used to ensure that its value is never negative. The default values in the init method arguments means that these arguments are optional. We can create instances of Amplifier like this:

var a1 = Amplifier()
var a2 = Amplifier(level: 3)
var a3 = Amplifier(level: 1, step: 2)
var a4 = Amplifier(level: 10, step: 5, source: "phono")

But more interestingly, we can access the implementation through a protocol (and only have access to what the protocol provides, not everything from the class instance):

var v: VolumeControl = a1
var level = v.increase()
if level > 100 {
    v.mute()
    level = v.level
}

A good place to start is A Swift Tour. More on Swift later.

Sunday, April 20, 2014

How to use Interface Builder to create OS X Cocoa applications with Python

When programming Python I prefer to use native GUI on OS X. Very Simple OS X Cocoa Application using Python and Interface Builder is a good place to start. The example works with Python 3 on OS X 10.9.2 when PyObjC is installed following the instructions in one of my earlier posts: Python 3 and PyObjC.

Perl modules on Mac

I needed a few Perl modules that weren't installed on the standard Perl installation on my Mac (OS X 10.9.2). In How to install Perl modules on Mac OS X in 4 easy steps I found all the details needed. I didn't need to do step 1 (and 1.5) since this is the first thing I do on every new Mac instance. To summarize what I did in 3 steps to install the two modules I needed:

  1. sudo perl -MCPAN -e 'install Bundle::CPAN'
  2. sudo perl -MCPAN -e 'install Text::Iconv'
  3. sudo perl -MCPAN -e 'install HTML::TokeParser::Simple'

Wednesday, January 8, 2014

A suggestion for backup on Mac

A lot of options for backup on your Mac exists. I use Time MachineBackblaze, Crash PlanTransporterCarbon Copy Cloner, and other tools for different types of backup on different Macs. The challenging backup for me are my pictures. I want at least one backup not in my house. The size of this backup is large, and since I am using Aperture, the meta-data of the file-system has to be preserved. Large amount of data and continuous minor updates of the data also suggest that the backup solution should support incremental backups. My first thought was to use two Transporters, one at home and one at my office. This will give me the option to have two (or more) working copies of the Aperture data and everything will just work. However, this doesn't work because the Transporter doesn't support HFS+ (with meta-data), and Aperture uses the features of the file system extensively.

Newer versions of rsync does support HFS+ meta-data. I am currently using version 3.0.9 (3.1.0 has an annoying bug on OS X). OS X 10.9.1 comes with rsync version 2.6.9, and this is to old for our purpose. I installed version 3.0.9 using Homebrew.

The magic option of rsync that we need is -X, preserve extended attributes. We will also use the -a option to transfer the files in archive mode (ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer). You might also add the -H option to preserve hard links and -v for verbose mode. If you are brave you might even add the --delete option. This will remove files at the backup folder that is no longer at the source folder. If you don't use this option you might end up with more files in the backup folder. See the manual pages of rsync for more details.

To back up a folder (the source folder with my Aperture library) I will use the following rsync command:

rsync -avXH --delete source-folder backup-folder

In my case the source and backup folders are located on different computers at home and work. I prefer that the file transfer over the network is using ssh. To do this I add the option --rsh=ssh. So from my home computer I will use the following command to backup my Aperture library to the backup folder of my work computer:

rsync -avXH --delete --rsh=ssh Pictures my.work.com:backup

I had to install rsync version 3.0.9 on both computers and I had to configure the remote computer to use that version of rsync and not the pre-installed version. You can do this command to check what version of rsync that will be executed on the remote computer my.work.com:

ssh my.work.com "rsync --version"