Sublime Text 2 Command-line MicroTip

March 15th 2013 · Mac Computer

I’ve struggled with a long-standing bug that causes the Sublime Text 2 command-line launcher to fail sporadically. In order to use the command-line launcher, the documentation directs you to create a symbolic link to the command-line utility stored in the application bundle:

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

Then, assuming you have a bin directory in your home directory, and it’s in your path, you can open files in ST2 from the command line:

subl file_i_want_to_edit.txt

The problem is that the subl command is buggy. If ST2 isn’t running, or if it is running and there’s an open window, everything works well. The file is opened and it appears in a new tab. But, if ST2 is running and doesn’t have any open windows, the subl command will silently fail. ST2 will open a blank window with no tabs and no content.

There’s a simple hack that gives you 90% of what you want from the subl command. You can create an alternate command that works 100% of the time to open files. Create a one-line shell script that looks like this:

#!/bin/sh
open -a "/Applications/Sublime Text 2.app" $@

Save it wherever you like and use that script to open files. This isn’t a replacement for the subl command. That command supports command-line arguments like ‘-w’ to allow ST2 to be used as an editor for Git and other tools. This script is purely for opening files from the command-line.


Alfred 2 Workflows

March 4th 2013 · Computing , Mac Computer

Alfred 2 will be coming out soon. It’s big new feature is the ability to easily create “workflows”, plug-ins that extend Alfred’s capabilities.

I’ve been having a lot of fun creating new workflows and extending the work of others. The workflows are in my GitHub account for anyone interested in forking or examining them.

Here’s a current list of the workflows in GitHub:

character-palette

Very simple workflow to display the OS/X Character Palette for selecting obscure characters. Responds to the keyword ‘character’.

Binary download

create-histlist-task

Create tasks in the application The Hit List. Task text follows the format:

<task text> [% <hit list category>] [! <priority>] [| <note text>]

Responds to the keyword ‘ta’.

Examples:

ta Do something             - Create the task 'Do something' in the inbox
ta Do something else %work  - Create the task 'Do something else' in the Work folder
ta More work !1             - Create the task 'More work' in the inbox with priority 1
ta Stuff | Notes about it   - Create the task 'Stuff' in the inbox with 'Notes about it' as a note

Binary download

date-and-time

Very simple workflow to display the current date and time, and, optionally, copy it to the clipboard.

Responds to both keywords ‘date’ and ‘time’.

Binary download

google-autocomplete

This is based almost entirely on David Ferguson’s workflow. It is rewritten in Ruby but only makes two small changes: it provides the raw search text as an option if there are no Google search results and it times out the Google search result request.

Responds to the keyword ‘g’.

Binary download

kill-process

Search for and kill a process. This lists all the processes that match the text typed. Selecting a process does a ‘kill’ on it. Holding down the alt modifier key does a ‘kill -9’ and holding down the cmd modifier does a ‘kill -HUP’.

Responds to the keyword ‘kill’.

Binary download

menu-bar-search

This is based on the workflow by Jeroen van der Neut. It was his excellent idea and he provided the Applescript to extract menu items.

This workflow lets you control the front-most application by triggering menu actions. The text entered is used to display matching menu items. The change from Jeroen van der Neut’s workflow is to cache the menu items.

This workflow is a work in progress and has rough edges. The first is that caches aren’t aged – they’re permanent. The second is that menu item names extracted from Applescript don’t always match the displayed text, e.g., a menu item might say ‘Turn feature off’ or ‘Turn feature on’ based on the feature status but the extracted menu text is ‘Toggle feature’. The result is that the menu item can’t be activated by the workflow.

Binary download

network-info

This is based on David Ferguson’s IP Address workflow. The difference is that it provides interface names and mac address.

The workflow responds to the keywords ‘ip’ and ‘mac’. For the ‘ip’ keyword it lists all interfaces and the local IP addresses. It also displays the external IP address. For the ‘mac’ keyword it lists all interfaces and the local MAC addresses.

If you select an IP address or MAC address it will be copied to the clipboard. Holding down the cmd modifier key will also paste it in the front-most application.

Binary download

recent-documents

Based on the workflows by Clinton Strong and David Ferguson. The difference is it consolidates the OS/X recent documents list and the Alfred recent documentst list.

Triggering the workflow lists all of the recent documents. Selecting one will open it in the default application. Holding down the cmd modifier key will reveal it in Path Finder or Finder (depending on whether Path Finder is installed).

Responds to the keyword ‘recent’.

Binary download

running-apps

Lists all of the running OS/X applications (not processes, what OS/X considers as a running app). Selecting one will activate the application. Holding down the alt modifier key will trigger the built-in ‘quit’ Alfred command.

Responds to the keyword ‘running’.

Binary download

search-pubmed

Searches PubMed at ncbi.nlm.nih.gov. Provides two alternates search modes. The first does a keyword search of PubMed articles. Selecting an article displays the article information. The second uses the PubMed search suggestions. Selecting a suggestion performs a PubMed search using that search text.

Responds to the keywords ‘pubmed’ for article searches and ‘pubmed2’ for search suggestions.

Binary download

show-desktop

Simple workflow to hide all applications, showing the desktop.

Responds to the keyword ‘show desktop’.

Binary download

time-machine

Simple Time Machine status and control. Shows the Time Machine completion status and provides support to start and stop Time Machine backups.

Responds to the keywords ‘tmac status’ to show status, to ‘tmac start’ to start a Time Machine backup and to ‘tmac stop’ to stop a Time Machine backup.

Binary download

top-processes

Lists the top processes, aka the command-line ‘top’. Selecting a process will activate the ‘kill-process’ workflow to kill the selected process.

Binary download

urlencode

Encodes and decodes text for use in a URL.

  • Keyword ‘urlencode’ requires text and encodes it for use in a URL, copying it to the clipboard.
  • Keyword ‘urldecode’ requires text and decodes it from a URL, copying it to the clipboard.

Binary download

vmware-control

Based on the Parallels Control workflow. Provides a mechanism to control VMWare VMs.

  • Keyword ‘vm list’ lists the knows VMs and their current status. Selecting one copies the VM path to the clipboard.
  • Keyword ‘vm ip’ lists the IP addresses of running VMs. Selecing one copies the IP address to the clipboard.
  • Keyword ‘vm start’ lists the stopped VMs. Selecting one starts it.
  • Keyword ‘vm stop’ lists the running VMs. Selecting one stops it.
  • Keyword ‘vm reset’ lists the running VMs. Selecting one resets it.
  • Keyword ‘vm suspend’ lists the running VMs. Selecting one suspends it.
  • Keyword ‘vm pause’ lists the running VMs. Selecting one pauses it.
  • Keyword ‘vm unpause’ lists the running VMS. Selecting one unpauses it.
  • Keyword ‘vm snapshot’ requires a snapshot name and lists the running VMs. Selecting one creates a named snapshot of that VM.
  • Keyword ‘vm revert’ requires a snapshot name and lists the running VMs. Selecting one reverts to a named snapshot of that VM.

Binary download

volume-control

Simple workflow that provides volume control. Keyword ‘max’ sets the volume to max. Keyword ‘medium’ sets the volume to mid-level. Keyword ‘mute’ mutes the system volume.

Binary download


Getting the Grails standalone plugin to work with Grails 2.0.1

March 28th 2012 · Computing , Java

I needed to create a small, self-contained web application that could be run from the command-line. I’ve been using Grails a lot lately so I looked into the plugins available. There were two that fit the bill, the Jetty standalone plugin and the Standalone App Runner plugin.

Initially I used the Jetty plugin simply because I found it first. It works but has a significant drawback, Jetty unpacks the entire WAR file before running the application. On the Windows box it was being used on this takes several minutes before the application starts. In addition, once I upgraded the application to Grails 2.0.1, the Jetty plugin broke. So I looked into alternatives.

The Standalone App Runner plugin uses Tomcat 7 as an embedded application server. It bundles your application into a standard WAR file and then builds a jar file that contains embedded Tomcat and your application’s WAR file. Unfortunately, the plugin isn’t working correctly. Your application will start cleanly but the first time a web page is accessed you’ll see this error:

    java.lang.NoClassDefFoundError: org/apache/juli/logging/UserDataHelper

The problem is that the tomcat-embed-logging-juli dependency isn’t present in the embedded Tomcat wrapper. Luckily the fix is easy.

First, intall the Standalone App Runner plugin as usual. Once it’s installed, you’ll need to edit two of the plugins files:

  • ~/.grails/2.0.1/projects//plugins/standalone-1.0/dependencies.groovy
  • ~/.grails/2.0.1/projects//plugins/standalone-1.0/scripts/BuildStandalone.groovy

If you’re not on Unix the tilde (~) is short-hand for your home directory. And, of course, ‘’ is whatever project you installed the plugin into.

In the plugin’s ‘dependencies.groovy’ file, add the following block to the end of the file:

    runtime('org.apache.tomcat.embed:tomcat-embed-logging-juli:' + tomcatVersion) {
        transitive = false
    }

This new text comes after the runtime entry for ‘org.eclipse.jdt.core.compiler’. This may not be strictly necessary but I haven’t tested it without adding the runtime dependency.

In the ‘BuildStandalone.groovy’ file you’ll need to make two separate changes:

  1. Add an import line to the end of the imports at the top of the file:

     import org.apache.juli.logging.UserDataHelper
    
  2. Modify the buildJar function slightly around line 84. Add a dependency on ‘UserDataHelper’ by appending it to this line:

     for (clazz in [DeployTask, Engine, JspC, LogFactory, JDTCompilerAdapter]) {
    

Making the line look like this:

    for (clazz in [DeployTask, Engine, JspC, LogFactory, JDTCompilerAdapter, UserDataHelper]) {

That’s it. Now when you create the embedded file using ‘grails build-standalone’ the Grails application will work correctly.


Lion issues with Apple Look-and-Feel in Java

July 24th 2011 · Java

After upgrading to Lion from Snow Leopard, I ran into an immediate problem running unit tests for an application that uses JasperReports. JasperReports makes use of AWT and Swing regardless of whether the application calling it has a GUI interface. Normally this isn’t a problem, and it wasn’t a problem in Snow Leopard, but it has become one on Lion.

The error I received was:

java.lang.NoClassDefFoundError: apple/laf/CoreUIControl

The issue is that when the unit tests run, JasperReports tries to instantiate a Swing control and the Java run-time tries to initialize the default Apple Look-and-Feel. I’m not sure whether Lion has secured access to that (preventing unsigned applications from using it) or whether there’s another configuration issue in Lion. Regardless, the fix is simple for my purposes.

The Java default look-and-feel can be overridden at the command-line using a Java property:

-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel

That will switch the Java instance from using the Apple Look-and-Feel to using Metal. BTW, running “headless” won’t fix the problem. Headless means telling the Java runtime not to make use of the GUI.

If you’re using Maven you might find that your unit tests still aren’t working, in that case, you need to tell Maven to pass the properties down to sub-processes:

mvn -DargLine="-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel"

And, in case you’re wondering why you would want to run headless, it keeps OS/X from treating your command-line app as a GUI app if it uses AWT or Swing. The property to trigger headless operation is:

-Djava.awt.headless=true

Why Linux (and Windows) just doesn't cut it

April 19th 2011 · Computing , Linux , Mac Computer

Once upon a time, I did all my development in Linux. It was quite nice: I did my editing in vim and sometimes Eclipse, I typed commands into a terminal window, and managed my software using apt. I was fairly happy with this setup.

With time though, I started to feel the pain of dealing with Windows and its users. I needed to use Microsoft Office for 100% compatibility, suspend and restore reliably, access Exchange without issues and use software that didn’t look like it’s UI was designed by a kindergartner.

So, I abandoned Linux and tried to make Windows work. The command-line was hell, the development tools stunted and broken and it looked even worse then Linux. But I could get Office, and my Exchange issues were gone.

When the pain became unbearable, I went off the reservation and decided to try using OS/X. At first it was a complete pain. The copy and paste keys were different! That’s just change for change sake, right? Until the first time I did a copy and paste in a terminal window and realized the keys didn’t conflict with Ctrl-C.

The window handling just worked. I wasn’t playing around with the window manager du jour and thanks to excellent text editors like TextMate and BBEdit, I didn’t have to deal with vim anymore. And, even better, Terminal.app was pretty good. And if I didn’t like it, there was iTerm and now iTerm 2.

I had a VM of Linux (and Windows) in case I needed them on OS/X, but I never needed the Linux VM and only used the Windows VM when I needed to check code for Windows compatibility.

And, best of all, I could automate anything I wanted to. Thanks to LaunchBar, Automator and AppleScript, I could automate almost everything I wanted to. PDF workflow handling and services are awesome. This was the best decision I’ve made for my workflow and productivity.

Packages

Not everything was wine and roses. I needed software to do my job. IntelliJ became my IDE of choice – that worked beautifully. Microsoft Office 2011 runs well and is completely compatible. Apple Mail (and iCal) has no issues with my companies Exchange server. Printing, which shares a subsystem with Linux, works great in the home but can be hit or miss in support of those giant industrial copy/printers.

And, of course, I wanted command-line tools. Homebrew worked well here for the only three things I ever needed to install: git, imagemagick and python. I’ve installed a few other things using homebrew (nmap and others) but that was just for playing around. It’s actually worked out better then apt-get because I don’t get gigantic lists of packages to update on a regular basis.

When you’ve had to deal with the Linux version of DLL hell, homebrew is a breath of fresh air.

Darwin

And, best of all, I don’t have to give a crap about my kernel version anymore. I don’t have to know or care that it’s darwin vs. Linux 2.6.3.43.1.3.4.6.2. Or which patches my chosen distribution has applied, or whether my distribution is in political favor with the kernel developers or not. It just works.

Though the darwin support for dtrace is pretty amazing.

Workflow

The OS/X terminal is good. If you have issues with it, you’re probably using vim or emacs full screen. Wow. Feel free to use iTerm 2 if you are.

OS/X is highly customizable as well. Managing daemons is far better then most Linux systems due to the modern init replacement. Luckily for Linux, projects like SystemD are catching up.

And with application launchers like LaunchBar, QuickSilver, Sparks, etc. I can work from the keyboard constantly.

Open source

And, as a developer, not having to deal with the open-source politics is a win. I don’t have to defend my betrayal of open-source rights when I choose a closed-source video driver and yet I can still install pretty much any open source project.

And support for future ruby platforms like rubinius and macruby is actually better on OS/X.

Closing thoughts

If everything I said went over your head, and you think I’m crazy, it just means your time hasn’t come yet.

I remember reading Alex Sellier’s counter thoughts on this matter yesterday and being amazed that different people have different viewpoints, both of which are totally valid for them and realizing that this isn’t a zero-sum game. What you believe isn’t the word from God and neither is what I believe. Make your own (hopefully informed) decisions and choose what’s right for you.

I sure do love the Toto blogging engine, though.


Check the gender of a first name

February 8th 2011 · Computing

I needed a very simple mechanism to perform a gender sanity check on a first name to promote error checking on data entry. After some quick Google searches I compiled a consolidated list of first names and the probable gender of the person with that first name.

This method is obviously error prone. It is not intended to be a 100% validation of a gender based on the first name, it’s intended to be used as a red flag when checking a given gender against a given first name.

You can test individual names using the check-name page or you can just download the CSV file and use it in whatever manner you wish.

<< Previous articles