Company logo with the letters 'NotTooBad Software' TextSmith Blog

Parsing Unicode property data files

TextPicker  Patterns  

When developing the word boundary recogniser for the Patterns framework, I needed to access data in Unicode property data files from Swift. These files look something like this:


# Total code points: 88

# ================================================

0780..07A5    ; Thaana # Lo  [38] THAANA LETTER HAA..THAANA LETTER WAAVU
07B1          ; Thaana # Lo       THAANA LETTER NAA

For all the Unicode property data files you could possibly want, see here and here.

where the hexadecimal numbers/ranges at the beginning of the line and the property name (“Thaana”) are the interesting parts. So we need to find all hexadecimal numbers that are at the beginning of a line – optionally followed by “..” and another hexadecimal – followed by spaces, a semi-colon, a single space, the property name and another space.

How to copy a Time Machine backup

macOS  

Cloning a Time Machine backup from one external hard drive to another is surprisingly difficult.

First – what didn’t work:

But SuperDuper managed it (in a little over 3 days) – using the default settings (Backup – all files). And it’s free for basic functionality.

 

References:

Design spec for TextPicker

TextPicker  

It’s hard to find examples of simple descriptions of simple applications online. Most seem to be meant for larger organisations. Here is the design specification for TextPicker, which the designers at draftss.com used when they designed the UI.

A Mac application for extracting text.

Background

Programmers, data scientists, prosumers and others often need to extract specific information from raw text, and may also wish to automate this or include this functionality in software they are making. Current methods are find/replace, regular expressions, writing parsers manually, or command line tools like grep, sed, awk, etc. These methods can be very complex, even for experienced programmers, and may often take longer to get working than just copying the desired information manually.

SwiftShell 3

SwiftShell  

SwiftShell 3 is finally out of beta, with a much improved readme.

So much so it may even be too long now.

Moderator: parsing commandline arguments in Swift

Swift  

There are a lot of command line argument parsers written in Swift available on Github, so when I needed one for a client project I was surprised I could not find any which fulfilled these requirements:


Nor could I find any where it would be relatively easy to add these features, since most just loop through the arguments from first to last and have one big function trying to find out what to do with each of them. That becomes messy quickly.

FileSmith: Type-safe file handling in Swift

Swift  

Dealing with file paths in Swift is cumbersome. Using only the standard library leaves us with Strings, which we can join together or split by /. It gets the job done but it’s not pretty, and we need a separate type so our methods can accept file paths exclusively and not just any old String. Foundation gives us this in NSURL/URL, which are also used for internet URLs so their method names are very general and long. E.g. url.deletingPathExtension().lastPathComponent to return the file name without the extension.

Combine markdown files and convert to HTML in a Swift script.

SwiftShell  

Swift 3

Sam Burnstone recently wrote about how to convert a simple shell script to Swift. Here’s the same shell script rewritten using SwiftShell and FileSmith.

Building the Swift compiler and Foundation on Linux

Swift  

Before building the Swift compiler it might be a good idea to check https://github.com/apple/swift to verify the build is currently passing. And to free up as much memory as possible first you can shut down the graphical interface with sudo service lightdm stop.

(has been tested on Ubuntu 15.10):

Move files to the trash with a Swift script

SwiftShell  

Swift 3

I really don’t like using the ‘rm’ shell command – one misplaced character and you can do some serious damage. But when working in the Finder I don’t think twice about deleting files, because I know I can always get them back from the trash. So here is a Swift shell script which does exactly that – it moves files to the trash instead of deleting them permanently.

The syntax is very simple – all parameters refer to file system items which should be moved to the trash:

trash file.txt a_folder
trash *.m *.h

Cross-platform command line arguments syntax

Uncategorized  

I’m rewriting Moderator (yet another command-line argument parser), and with Swift now being available for both OS X and Linux (Ubuntu) it should support a syntax which enables applications to fit in on both platforms.

POSIX* is I think the closest thing to a standard for this, so it will be the basis, with some modifications (The Python documentation also has some good insights).

* OS X is POSIX compliant and so is Linux (mostly).

Splitting text read piece by piece

Swift  

Swift version 2.1.

In the previous post we implemented lazy splitting of collections, very useful for say splitting large texts into lines. But in SwiftShell I need the same functionality for text which is acquired piecemeal, like the output of a long-running shell command read sequentially, when needed. Because shell commands which are piped together in the terminal should get to work right away, and not just hang around waiting for the previous command to finish. Like this:

Both scripts start at the same time. The left one uses the functionality implemented below, while the right one reads the entire input into a string first, and therefore has to wait for the ‘linemaker’ command to finish before doing any actual work.

Splitting text and collections lazily in Swift

Swift  

Swift version 2.1

There are already methods for splitting collections in the Swift Standard Library, but they do all the work immediately and return the results in an array. When dealing with large strings, or streams of text, I find it better to do the work lazily, when needed. The overall performance is not necessarily better, but it is smoother, as you get the first results immediately instead of having to wait a little while and then get everything at once. And memory usage is lower, no need to store everything in an array first.

SwiftShell 2.0 Readme

SwiftShell  

I finally got around to updating the SwiftShell 2.0 readme with some actual usage instructions:

SwiftShell: An OS X Framework for command line scripting in Swift.

How to use Swift for shell scripting

SwiftShell  

To be honest I’m not very good at shell scripting. It’s very useful for automation so I would like to be, but I just don’t like the syntax. For instance, this is how you check if a variable is greater than 100:

#!/bin/bash

if [ $var -gt 100 ]
then
    <do some stuff>
fi

Redesigning an API - Swift 2.0 style

SwiftShell  

SwiftShell (an OS X framework for shell scripting in Swift) is currently using the |> operator to combine shell commands, streams and functions, and |>> to print the results:

// SwiftShell 1

run("echo piped to the next command") |> run("wc -w") |>> standardoutput

// print out line number and content of each line in standard input
var i = 1
standardinput.lines() |> map {line in "line \(i++): \(line)\n"} |>> standardoutput

But Swift 2.0 is here, and it’s clear the way forward is protocols, method chaining and error handling. And being more explicit about what is going on. So for SwiftShell 2 I’m planning something like this:

Swift: mixing default values and variadic parameters.

Swift  

Update:

As of Xcode 7 beta 6, Swift no longer requires variadic parameters to be last in the function definition. Also argument labels are no longer required when combined with parameters with default values. So this all works fine now:

public func run (executable: String, _ args: String ..., stdinput: String = "default")  {}

run("cmd", stdinput: "not default", "arg1", "arg2")
run("cmd", "arg1", "arg2", stdinput: "not default")
run("cmd", "arg1", "arg2")

The rest of this post is deprecated.

New theme and new host

Uncategorized  

Did not post anything last week, as I have been busy moving the site over to BlueHost and setting up a new theme. All because I wanted Swift code highlighting, and WordPress.com doesn’t support that. This of course took a lot longer than anticipated, but it’s finally done and if I do say so myself the new theme is simpler and easier to read. So all in all it was worth it.

Parser combinator operators in Swift

FootlessParser  

This is part of a series on FootlessParser, a parser combinator written in Swift.


Parser combinators must be one of the best things to come out of functional programming. They let you define intuitive parsers right in the code, without any need for pre-processors.

Like this:

let parser = function1 <^> parser1 <*> parser2 <|> parser3

where function1 and parser3 return the same type.

parser will pass the input to parser1 followed by parser2, pass their results to function1 and return its result. If that fails it will pass the original input to parser3 and return its result.

FootlessParser: updated documentation and readme

FootlessParser  

The FootlessParser project is finally becoming usable and now has brand-new documentation generated by jazzy. The readme has also received some attention and is now actually useful:


FootlessParser is a simple and pretty naive implementation of a parser combinator in Swift. It enables infinite lookahead, non-ambiguous parsing with error reporting.

Also check out a series of blog posts about the development and documentation from the source code.

Making a Numeric Type in Swift

Uncategorized  

Fabián Cañas demonstrates something similar to mixins in Swift (for operators):

By defining types (and protocols) around capabilities, and writing functions that target those capabilities, we end up with cleaner and remarkably reusable code when compared to implementing functionality targeting a specific type. The bulk of our final implementation has nothing to do with distances, time intervals, apples, oranges, or anything else. It reads more like a set of statements of fact that any future programmer could adopt if they so chose.

Swift: Associated Types

Uncategorized  

Russ Bishop has a clarifying post about Swift protocols and generics:

Type parameters force everyone to know the types involved and specify them repeatedly (when you compose with them it can also lead to an explosion in the number of type parameters). They're part of the public interface. The code that uses the concrete thing (class/struct/enum) makes the decision about what types to select. By contrast an associated type is part of the implementation detail. It's hidden, just like a class can hide its internal ivars. The abstract type member is for the concrete thing (class/struct/enum) to provide later. You select the actual type when you adopt the protocol, not when you instantiate the class/struct. It leaves control over which types to select in a different set of hands.

But I do think he is too harsh on the functional programming hipster kids.

Add Result type and operators

FootlessParser  

This is part of a series on FootlessParser, a parser combinator written in Swift.


Add Runes and LlamaKit using Carthage

https://github.com/kareman/FootlessParser/commit/6142452334dae45a5aae65e0f54264f1ea3f533d

Footlessparser is using operators for map ( <^> ), flatmap/bind ( »- ) and apply ( <*> ). Luckily the Runes framework has already defined these, and implemented them for optionals and arrays.

Each parse returns a Result, which has either a tuple containing the output and the remaining unparsed part of the input, or an error description if parsing fails. The Result enum is in the Llamakit framework, later replaced by antitypical/Result.

Set up an open source Xcode framework project

FootlessParser  

This is part of a series on FootlessParser, a parser combinator written in Swift.


I find these steps helpful when setting up any new open source framework project in Xcode.

Create project

https://github.com/kareman/FootlessParser/commit/414e3961d3bb2a2df8a257804534578bc7a06461

Create a new project in Xcode, and select OS X framework if it is for both iOS and OS X. The iOS framework target can be added later, besides OS X frameworks are more practical for unit testing. No simulators needed.

Do not select to add it to Git right away. Because of this.

And that’s it. I so do love a fresh project.

Introduction to FootlessParser

FootlessParser  

This post is part of a series on FootlessParser, a parser combinator written in Swift.


The goal is to define parsers like this:

let parser = function1 <^> parser1 <*> parser2 <|> parser3

where parser will pass the input to parser1 followed by parser2, pass their results to function1 and return its result. If that fails it will pass the original input to parser3 and return its result.

Writing a parser combinator in Swift

FootlessParser  

This post is part of a series on FootlessParser, a parser combinator written in Swift.


Before Swift my only contact with functional programming was a couple of half-hearted attempts at reading Erlang, all of them resulting in me running away screaming, clutching my aching head. But learning functional concepts in Swift proved far easier, probably because the syntax is closer to what I was used to. So I read Functional Programming in Swift and everything was well and good until one of the last chapters, “Parser Combinators”, and the headache was back. Luckily I managed to stay quiet and in place this time. I downloaded the code for the chapter, turned it into a framework and hacked away until I had implemented a CSV parser, but I still didn’t really understand it.

Adding an XCode project to Git

Uncategorized  

When creating a new project in Xcode it is best to leave the “Create Git repository” box unchecked because it will immediately add all files to Git, including those that are specific to you and are of no interest to anyone else. Instead you can run this script from the project folder, which will add only the files that should be under version control:

How to use a Mac without hands

Accessibility  

Many people are, like me, unable to use a keyboard and mouse/trackpad for longer periods of time. The solution, as always, is to use your head.

Moving the Mouse Pointer

iTracker doesn’t automatically start controlling the mouse pointer when launched, even if you tell it to in the preferences. Nor will it do anything when you click the icon in the menu bar and select “Track” from the menu. It will only start working when you press the global keyboard shortcut for pausing and resuming iTracker (Cmd-Shift-T by default).

iTracker is the only Mac head mouse software I know of that actually works. It uses the web camera on your Mac to locate your face, see which way it is moving, and move the mouse pointer accordingly. Sounds weird, but it quickly becomes second nature. After adjusting speed and sensitivity you should be able to reach all corners of the screen just by turning your head.

TextSmith - an application for writing

Uncategorized  

In The ultimate word processor, I described my ideal application for writing. I just wrote a much more thorough description of its key features and basic workflow.

It’s a bit hard to classify, but here goes:

But the best description is probably just “Writing Application” (as in; an application for writing).

The ultimate word processor

User Interface  

The ultimate writing software should let you:

In May 2004, Adam C. Engst wrote about WriteRight: The Writer’s Word Processor where he laid out his idea of the perfect word processor. Over 6 1/2 years later there is still no such thing. Scrivener is a major leap forward but I feel it is still lacking in some areas, so here I have outlined my own ideas for the perfect writing software. Unfortunately all the good names are taken. The mediocre ones too. So I give you:

Even better UI: Scrivener

User Interface  

Continuing from last week’s ideas for Scrivener’s corkboard, here are some suggestions for the rest of the application.

Preview when configuring a compile

When it’s time to get my writings out of Scrivener, there will often be a lot of back and forth to get the output just the way I want it. It would save a lot of time if there was an instant preview when configuring the compile settings.

Even better UI: Scrivener – The Corkboard

User Interface  

Scrivener is an excellent application and the closest anything has come to a great word processor (as opposed to a page layout application, which is what most word processors really are). Fiction and non-fiction writers alike love it, and I fully recommend it for writing anything longer than a blog post. But it’s so full of functionality and customisation options that it can be a bit unintuitive at times. So here are a few ideas for improvements.

How to import voice memos into Things

Uncategorized  

I usually write new ideas into the Things app on my iPhone straight away, but sometimes it’s better to just dictate into the Voice Memo app instead of typing. Here’s how you automatically add those voice memos to the Things inbox:

Auto-saving in OS X

User Interface  

As I mentioned a week ago, I have a strong dislike for the “Save” dialogue boxes that pop up every time you close a document. It seems someone at Apple, and by someone I mean you–know–who, agrees with me. The next version of Mac OS X will include auto-saving and applications will automatically resume where they were when they closed.

Dumbest question ever

User Interface  

Some survey once showed that the dumbest question known to mankind is “Are you sleeping?” I don’t get it. That seems like a perfectly valid question. If you get no answer it means “yes” and if you get any answer at all it means “no”. Couldn’t be simpler.

No the dumbest question ever must surely be this one:

Dragon Dictate 2.0 for the Mac

Dictation  

I have been a bit harsh on MacSpeech in the past, but since then they have been bought by Nuance (makers of the excellent Dragon NaturallySpeaking dictation software for Windows). If there is anyone who can finally release a decent dictation application for the Mac, it’s them. And recently they released the rebranded Dragon Dictate 2.0. Looking forward to finally not having to launch Windows in Parallels Desktop every time I want to dictate something, this of course filled me with much joy and hope. But judging by MacSpeech’s own forums this version has caused a lot of problems for many people, first and foremost because of a bug which crashes the application when you launch it! After making absolutely sure that I can indeed get my money back within the first 30 days if the application is not working properly and am therefore in no way running the risk of giving even more money to MacSpeech for a faulty product, I bought and installed it. The aforementioned bug was easily avoided by leaving the start-up mode to “Idle - Microphone off”.

Dictating on a Mac with Dragon NaturallySpeaking

Dictation  

As I have mentioned before I’m not a big fan of MacSpeech Dictate. So I use Dragon NaturallySpeaking in Windows XP running under Parallels Desktop 4 when I need to dictate something. It’s working very well, but it is of course very tedious to have to copy and paste everything I dictate from Windows back into my Mac applications. Luckily Tim Harper has made tightvnc-dns, a Windows application that reroutes the generated keystrokes from Dragon NaturallySpeaking back to the Mac side. You can’t use Dragon to edit the text you dictate this way, but for shorter pieces of text it is very practical.

Icons for your iPhone settings

iPhone Development  

bjango.com has an interesting article about how and why many developers are moving their settings into the app itself. They even have some icons you can use, both for the tab bar and the toolbar.

Finally released my first App

iPhone Development  

Today I finally released my first iPhone app ‘Repetitions’. It’s going to be very interesting to see how it does. After reading about how other apps have done I’m not very optimistic, especially since the release date of my app was set to 25 August (when it was accepted by Apple) instead of today when it was actually released. Which of course means my app now appears on page 6 of the “Health and Fitness” category in the AppStore. Where no one will find it. But I will be releasing an update soon anyway, which should put it on the first page at least for a couple of days.

Added mySettings to GitHub

iPhone Development  

I finally added mySettings to GitHub, using the marvel that is hg-git. It allows me to continue using mercurial, and to push and pull from git repositories. Here is the project on GitHub. The main place for mySettings will still be on BitBucket, I’m just uploading the code to GitHub to see if it will attract more users and contributors.

Customising a table view with mySettings

iPhone Development  

I’ve been working on my iPhone app lately (for performing physical exercises like the ones you get from a physiotherapist, but more about that later) so I haven’t written anything for a long time. I thought I’d rectify that by showing how to create highly customised UIs with fairly little coding. I recently added support for delegates to mySettings and that opened up a lot of possibilities, even with only one method in the delegate (for now).

As an example, here’s the configuration screen for my app:

Configuration screen

Another way to create Settings views

iPhone Development  

Craig Hockenberry has also created an API for Settings views. But unlike mySettings his API creates the views from code instead of plists. Like this:

- (void)constructTableGroups
{
NSMutableArray *cells = [NSMutableArray array];
IFTextCellController *textCell = [[[IFTextCellController alloc] initWithLabel:@"Text" andPlaceholder:@"Placeholder" atKey:@"sampleText" inModel:model] autorelease];
[cells addObject:textCell];
IFSwitchCellController *switchCell = [[[IFSwitchCellController alloc] initWithLabel:@"Switch" atKey:@”sampleSwitch” inModel:model] autorelease];
[cells addObject:switchCell];
tableGroups = [[NSArray arrayWithObject:cells] retain];
}

Check it out at http://furbo.org/2009/04/30/matt-gallagher-deserves-a-medal/ .

My Dream Gadget: A Really Big iPod Touch

Uncategorized  

My idea of the perfect device would essentially be an iPod touch with a screen the size of an A4 sheet of paper. It would be brilliant for surfing the web and reading e-books and PDF’s. Anyone who’s tried to do that on an iPhone or an iPod touch would appreciate not having to scroll for every paragraph. And in addition to running iPhone apps it should be able to remote control a Mac. Just imagine sitting in the living room and using Front Row on this thing. Not to mention playing an adventure game. Only problem is with a touch-screen that big it would probably end up costing as much as a desktop computer. So we probably won’t see it for a couple of years.

But eventually it will be made. It’s just too obvious and idea for it not to.

A UIPickerView with labels

iPhone Development  

I recently needed a picker view with labels (like the one in the timer tab in the Clock app) to select minutes and seconds for a time interval. So I made the following subclass of UIPickerView:


#import

/**
A picker view with labels under the selection indicator.
Similar to the one in the timer tab in the Clock app.
NB: has only been tested with less than four wheels.
*/
@interface LabeledPickerView : UIPickerView {
    NSMutableDictionary *labels;
}

/** Adds the label for the given component. */
- (void) addLabel:(NSString *)labeltext forComponent:(NSUInteger)component;

@end

mySettings: A Settings API for the iPhone

iPhone Development  

Update: Unfortunately I am not able to do any more development on this or any other projects. I am hoping that someone else will take over the project and update it.

A lot of iPhone apps have their own settings views similar to the ones in the Settings App on the iPhone home screen. But to my surprise I couldn’t find any general API for it. So I made my own. It uses a plist configuration file like the one used by the Settings App, with some added options (and some removed ones, but they will hopefully be implemented in the near future).

Welcome

Uncategorized  

Hi, and welcome to my blog. It will almost exclusively be about programming (iPhone-programming to be specific), so if that’s not your particular cup of tea then you’re probably better off with this one. I’m aiming for two or three posts a month, so hopefully I won’t be contributing too much to the unread count of your RSS reader.

PS: I know I’m using the default theme for WordPress, but I really like it and I’ve only seen one other blog using it. If I should happen to come across a better one I might change, but for now I’m happy with this one.

Want to hear about new posts?