Posterous theme by Cory Watilo

Filed under: accentuate.us

Announcing accentuateus-0.9 (Haskell implementation of the Accentuate.us API)

Introduction

As some of you know, I co-created the Accentuate.us project with Kevin Scannell and have been hacking away at various client implementations ever since (including our flagship Firefox client).

Haskell

So, since I have been working on so many implementations of the API, it only seemed natural that I give some attention to my favourite language.

This is my first release on Hackage and it is available at http://hackage.haskell.org/package/accentuateus. I appreciate any feedback you guys have so that I can improve both it and any future code I write.

Code

The source code is available on Spearhead’s GitHub.

Usage

The API implementation boils down to Accentuate.us' three calls:

  • langs — Accepting an optional locale and mandatory version number (0 if unknown), langs will tell you whether or not you are in sync with the API servers' current version. If not (supplied version != server version), it will supply a new list of (ISO-639, Language Name) pairs. When possible, we will return the language names localized to the supplied locale.
  • accentuate — This is the magic call in the service: given a language, optional locale (to localize any error messages), and input text, it will use statistics to add diacritics.
  • feedback — The other part of the Accentuate.us service that we really want to make more use of is feedback. Since we use statistics, the diacritic restorations will not always be correct (especially for the more under-resourced languages we support). By supplying a language name and corrected text, we will add it to our training text so that results will improve by user contributions.

Feedback

Again, this is my first upload to Hackage and I’m really excited about it. I appreciate any feedback you guys have. The latest source can be found at https://github.com/spearhead/hs-accentuateus/blob/master/Text/AccentuateUs.hs.

Creating a Mac OS X Service (Part I)

Introduction

I was back working in OS X the other day and got to thinking about an often underused portion of the operating system: Services. Services provide the user with contextual operations: working with text, images, and so forth. One that Apple provides is to highlight a word and, in the context menu or the application's services menu (i.e., Safari > Services), you will be provided with an option to look the word up in the dictionary.

Wanting yet another example of how easy it is to implement the Accentuate.us API, I decided to take on the task and implement an Accentuate.us OS X Service myself. Note that at the moment it is still under development, so it only accentuates into Irish for testing purposes. That is being ironed out and will be committed soon, though its absense will not impact the contents of this Part I post.

Background

I was disappointed by the documentation that I found online. Although Apple's developer documentation did prove useful once the project was underway, I felt that there was too little detail about setting up the project itself within Xcode, which led to much undue frustration. I found much detail online about helper utilities to make services, but little material on coding my own, except for CocoaDev's making services, which had the same issue of not discussing the setup of the Xcode project itself, and Simone's very detailed walkthrough. Unfortunately, after following Simone's step-by-step, I encountered the note at the end of the post admitting that "the bundle doesn't actually work," which was slightly important to the project. There was a workaround mentioned that involved compiling two different targets, but I found that unnecessary and document the solution within this post.

It is important to reiterate that the three services mentioned brought me close to what I needed, but all lacked in explaining the setup itself or did so in an unfavorable way, which is what I will correct in this part of the series. This will successfully lay the foundation for creating an OS X service and get it to the point that it can lowercase any selected text. In subsequent posts, we will dig deep with the Accentuate.us API and see just how easy it is to make a system-wide utility to accentuate or process selected text in a way you desire!

Setup

Actually configuring the project in Xcode was the area that I found most difficult, so for your benefit, I will err on the side of caution and be overly detailed.

02

Create a new Xcode project and under the Mac OS X pane on the left, select Framework & Library. Choose Bundle with the Core Foundation framework and save it as AccentuateUsService.

04
05

The first problem is that the default bundle is called AccentuateUsService.bundle, but we need it to be AccentuateUsService.service. Go to Project > Edit Active Target "AccentuateUsService"... and select the Build tab. Under the Packaging category, change the Wrapper Extension settings from bundle to service.

07_important_later

While we're in the target info, switch over to the properties tab and change the Identifier field to read com.accentuateus.${PRODUCT_NAME:rfc1034Identifier}, which turns out after parsing to be com.accentuateus.AccentuateUsService. This will be useful in a subsequent post.

09
The next problem to tackle is the need for an executable, otherwise the logic for handling the selected text is nonexistent in the build. You can do this by right clicking on the Executables group and selecting New Custom Executable... under the Add menu, which you should fill out as pictured above (AccentuateUsService for the executable name, AccentuateUsService.service for the path).

10

11
Though the executable exists, the binary file at this stage will be useless. To change this, navigate to the Project menu and select Edit Project Settings. Under the Build tab, beneath the Linking category, change Mach-O Type from Bundle to Executable.

14
15
Our work in this service depends on the Cocoa framework, so right click the External Frameworks and Libraries group and navigate to Existing Frameworks... under the Add menu. Select the Cocoa.framework entry and click Add.

16

18
Now, we're getting to the fun part! We need to add our first source file, main.m, which will hold the logic to register the service with OS X. Under AccentuateUsService's Source group, navigate to New File... beneath Add. We'll want to navigate to the Mac OS X Cocoa Class entry in the left pane and then select the Objective-C class as a subclass of NSObject. The file name is main.m and we do not need a header file, so uncheck that box. All other fields can stay at their default, so click Finish.

19

We also need a place for the service logic. Although this could be done also in main.m, or main.m's logic could be in this Service class, I like to separate them out. Follow the same steps as above, but this time use the file name Services.m and do check the Also create "Services.h" box.

main.m

In main.m we initialize the below-implemented Service class and register it with the services provider, making service calls possible.

Services.h

In Services.h, we simply define the accentuate method signature. Note that while the first argument, accentuate, may be whatever you please, both userData and error must remain that. The custom accentuate term, although it may be whatever you desire, must match with Info.plist's NSMessage, as discussed later.

Services.m

Services.m is where the magic happens--it is the logic for accentuate, the method through which all service calls pass. We first check that types does indeed contain an object matching one of NSSendTypes' entries (discussed later when speaking of Info.plist) and that we can extract the supplied text from it. This is particularly useful for handling the processing of multiple types of data, although that is not covered in this particular post. To be safe, we also check that text has been set, otherwise throw an error and abort.

After we are sure that we have been provided well-formed data, we clear pboard and process the text. In this case, we are simply calling lowercaseString which, intuitively, provides a lowercased version of the string. We will in the next post be replacing this with a different call that actually accentuates the text.

After all is said and done, the processed text is written to pboard. That's all there is to it!

20
We're getting close to finish. The next step is to make sure OS X can have an easy time finding your service, so for that we'll need to modify the Info.plist file found under AccentuateUsService's Resources group. Right click and select Source Code File under Open As to follow along directly as I do here. You can edit it in the other modes as well, but I prefer this.

Info.plist

The property values are fairly self-explanatory or well documented in the Mac OS X Reference Library, but I'd like to highlight just a few:

  • LSBackgroundOnly keeps the application from appearing in the Dock.
  • NSMessage is a key item, so I'll say it again: NSMessage. Earlier we named a method in Services.h called accentuate, or whatever silly name you chose while following, and this must be the same.
  • NSPortName is another important entry. I set its value to ${PRODUCT_NAME}, which resolves to AccentuateUsService, because I used that earlier in main.m.

Installation

Phew, we're done with the setup! In future posts, now that this foundation has been laid, we'll be able to jump straight into code, but for now it is time to test out our brand new service!

21
22

You can navigate easily to the code's folder by right clicking on the AccentuateUsService group and selecting Reveal in Finder. Navigate to AccentuateUsService/build/Debug/. Open a new finder window (⌘N) and go to the folder (⇧⌘G) ~/Library/Services/.

24

Copy the AccentuateUsService.service file from the first finder window (in Debug) to the Services folder. You will now need to either log out and back in again or open Terminal (/Applications/Utilities/Terminal.app) and run

$ /System/Library/CoreServices/pbs -dump_pboard |grep AccentuateUsService

This will force OS X to reregister any available services and save you the trouble of login cycling. I added the grep just because -dump_pboard outputs so much contextually irrelevant information.

Please note well that pbs is not guaranteed to stay in OS X and is subject to change functionality, move, or be removed entirely, but it is great for testing.

25
Next you will need to tell OS X that it's okay to actually display this new service. Navigate to Services under the Application's menu (i.e., Finder > Services) and choose Services Preferences.... Under the Keyboard Shortcuts tab, navigate to the Services pane (left) and check Text > Accentuate Text.

Demo

26
27
This is my favorite part: demo time! For this, I open Apple's TextEdit and type a very important letter to my guests, who are unfortunately averse to caps. Thankfully, we wrote this wonderful service that lowers the case of all selected text! Go to the application's menu and select Accentuate Text under the Services submenu (TextEdit > Services > Accentuate Text).

28
If all goes well, your letter should be caps free and your guests quite happy indeed!

Wrap-Up

As noted, this particular installation of the Accentuate.us service posts was more detailed than perhaps necessary because I found a few key steps of the setup in Xcode to be undocumented or at least extremely difficult to find and did not want you to suffer as I did. In the next installment, we'll dive right in with some more code and get a first-hand account of just how easy the Accentuate.us API is.

The phrase in previous code [GPLv3 license information redacted for brevity, included at end of post.] is included here as promised:

 

Upcoming Talks: Photography and Accentuate.us

Strange Loop: Strange Passions

On October 14, I'll be giving an extended version of my July 21st Perl Mongers s/2 years/5 minutes/ of Photography lightning talk for Strange Loop's Strange Passions track. Alex tells me we'll be getting our hands on video thanks to the lovely InfoQ, so I'll have that and slides posted as soon as it comes my way.

Be sure to also check out Matt Follett's Perl 6 Talk, which he'll also be giving at Strange Loop.

Saint Louis University Math/CS Club: Accentuate Us!

Alogo-128

Dr. Kevin Scannell and I will be giving a talk on November 10th to the Math/CS Club, starting around 4pm, to discuss Accentuate.us, a new system allowing you to type quickly and easily in over 100 languages. Video is not yet confirmed, but I'm looking into it.