Creating Icons for Mac Apps

I found this so helpful on StackOverflow, I thought I’d re-post it here for my personal reference. The snippet below is used to take a 1024-by-1024 pixel PNG file named Icon1024.png, and create smaller resolution copies in an iconset directory, which can be used later when you deploy your app.

mkdir MyIcon.iconset
sips -z 16 16     Icon1024.png --out MyIcon.iconset/icon_16x16.png
sips -z 32 32     Icon1024.png --out MyIcon.iconset/icon_16x16@2x.png
sips -z 32 32     Icon1024.png --out MyIcon.iconset/icon_32x32.png
sips -z 64 64     Icon1024.png --out MyIcon.iconset/icon_32x32@2x.png
sips -z 128 128   Icon1024.png --out MyIcon.iconset/icon_128x128.png
sips -z 256 256   Icon1024.png --out MyIcon.iconset/icon_128x128@2x.png
sips -z 256 256   Icon1024.png --out MyIcon.iconset/icon_256x256.png
sips -z 512 512   Icon1024.png --out MyIcon.iconset/icon_256x256@2x.png
sips -z 512 512   Icon1024.png --out MyIcon.iconset/icon_512x512.png
cp Icon1024.png MyIcon.iconset/icon_512x512@2x.png
iconutil -c icns MyIcon.iconset
rm -R MyIcon.iconset

The next to last line uses the iconutil utility to convert an iconset into an icns file, but it can also work the other way. (See man iconutil for more information.)

Using app2py for OS X Distribution

I thought that .pex files were the way to go for distributing Python applications to OS X users, but it was only partially successful. For one, users needed to reinstall Python with Homebrew, which isn’t difficult, it’s just awkward explaining to people that the Python distribution that ships with Mac isn’t the same Python distribution that exists in the wild. Their next question is invariably, “how will this affect the rest of my system?” I can’t answer that. I can’t guarantee that things will be future-proof.

And then I found app2py (or five dollars) which creates a Mac application bundle out of a Python script. It worked like a charm. The best thing is that you don’t have to write a stupid setup.py file, it writes one for you. That’s five minutes of your precious time you can look at cat videos with, or whatever.

You can install this using pip, so that’s cool. Next, you do as the tutorial explains,

py2applet --make-setup MyApplication.py

This will create a setup.py file. Finally, for deployment, you build the distribution with a non-Mac Python installation. (If you haven’t already run brew install python you’ll want to run that.) My brew installation of the Python landed somewhere in /usr/local/Cellar/....

/usr/local/Cellar/python/2.7.8_2/bin/python setup.py py2app

This will create a standalone Python application that you can distribute painlessly to your Mac colleagues.

Simple Reservoir Algorithm in Python

I needed to sample lines from a file, and I found this SO post on on the Reservoir Algorithm. The code below lets you sample a single line from a file in O(n) time. What makes this code special, besides it’s linear time, simplicity and elegance, is the fact that the file you’re sampling from doesn’t need to fit in memory.

import random

def random_line( filename ):
    hdl = open( filename, "r" )
    line = next( hdl )
    for num, aline in enumerate( hdl ):
        if random.randrange(num + 2):
            continue
        line = aline
    return line

Write to a File in Swift2

Swift2 came out yesterday! For free! I thought that Swift2 would have built-in support for I/O, but it looks like you need to import Cocoa first.

import Cocoa

let data = NSString( string:"Hallo, Welt!" )
let destPath = "/Users/connorjohnson/myFile.txt"
var filemgr = NSFileManager.defaultManager()
do {
    try data.writeToFile(destPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
    print("Error: \(error)")
}

Working with Strings in Swift

In this post I’ll discuss replacing characters in a string, splitting a string using some value, and using regexes to count the number of matches in a string, and locating the first match in a string. I’m using XCode 6.4 with Swift 1.2. If you’re not sure which version of Swift you’re running, you can call the following from the command line,

$ xcrun swift --version
Apple Swift version 1.2 (swiftlang-602.0.53.1 clang-602.0.53)
Target: x86_64-apple-darwin15.0.0

Continue reading Working with Strings in Swift