I’ve seen a few people around the ‘net sharing their TextExpander snippets, so I thought I’d join in. Not familiar with TextExpander? It’s a Mac utility that expands short snippets into full text you’ve defined. There are quite a few programs that do text expansion (see Typinator), but TextExpander currently holds my heart because of its extra features such as shell scripting, completion suggestion and a new Fill dialog for variable input.

Most of my snippets are specific only to me, such as email signoffs and abbreviations for companies I work for. I have a few more general snippets, though, so that’s what I’m sharing. For reference, I’m currently experimenting with triggering only after a Tab press, a la TextMate tab-triggers. That shouldn’t make a difference on many of these, they should work with whatever you have set up.

All of the snippets I’m sharing are available for download. For more detail, read on…

Lorem Ipsum

First, there are the Lipsums. This is a collection of my most-used Lorem Ipsum snippets, great for filling in fields when testing forms, making quick HTML markup for CSS styling, or anywhere you just need to fill up some space. I have snippets for 1, 2, and 3 paragraphs of standard Lorem Ipsum text, as well as a few for HTML-specific lipsum. The king of these is a snippet I pulled from http://html-ipsum.com/ that inserts all of the major elements of HTML markup. It’s great for giving your CSS stylesheet a quick test to make sure you’ve covered the basics.

The Tools collection is a sampling of scripts and snippets, some more useful than others, that I’ve been experimenting with (and, in some cases, making good use of). Some examples:

Hyphenate clipboard

I tend to write out very long, multi-word modifiers that need hyphenation. While it’s more effective to use a System Service, I’ve found that the Automator services are too slow for such simple operations. So I cut the needs-to-be-hyphenated text to the clipboard and type ,,-. When I hit tab, the hyphenated version is pasted, with proper attention to punctuation and leading/trailing spaces. It looks like this:

#!/usr/bin/env ruby -wKU

# get the clipboard using pbpaste
clip = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}
# print it out, hyphenated
print clip.gsub(/\b\s\b/,'-')

Encode email address

This one takes an email address from the clipboard and returns a “mailto:” link with the email address itself encrypted, at least to the point where it’s not human readable anymore.

#!/usr/bin/env ruby -wKU

clipboard = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
print "mailto:#{$2.gsub(/./) {sprintf("&#x%02X;", $&.unpack("U")[0])}}" if clipboard =~ /\A(mailto:)?(.*?@.*\..*)\z/

Paste Markdown references

This one parses the clipboard for any and all urls, pasting the resulting matches as a Markdown reference list. It automatically names the references based on the url’s domain, strips duplicate urls and sorts the list alphanumerically by reference name. If the clipboard is already a reference list, it will sort it and remove duplicates before pasting it. You can then use the references in your Markdown to link to the associated URL, or just make the list look nice in an email or other non-rendered document.

#!/usr/bin/env ruby -rjcode -Ku
clipboard = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
links = clipboard.scan /(?:\[([^\]]+)\]\: )?(https?:\/\/[^ \n\r"]+)/m
norepeat = []
output = []
exit if links.nil?

links.each {|url|
  fresh = true
  output.each {|a|
    fresh = false if a['link'] == url[1]
  }
  next unless fresh

	if url[0].nil?
		domain = url[1].match(/https?:\/\/([^\/]+)/)
		parts = domain[1].split('.')
		name = case parts.length
			when 1,2: parts[0]
			else parts[1]
		end
	else
		name = url[0]
	end

  name = "itunes" if url[1] =~ /(itunes|phobos).apple.com/

	while norepeat.include? name
		name = name =~ / ?[0-9]$/ ? name.next! : name = name + " 2"
	end
	output << {'title' => name, 'link' => url[1] }
	norepeat.push name
}

output.sort {|a,b| a['title'] <=> b['title']}.each { |x| puts "[#{x['title']}]: #{x['link']}" }

Shortening URLs

There are 4 commands in the group for shortening URLs, using bit.ly, go., is.gd and tinyurl, respectively. There are some AppleScript-based commands already available which do this, but I like the string-handling flexibility of Ruby a little better (and it processes a tiny bit quicker, as well).

The basic command looks like this, with slight differences for each service:

#!/usr/bin/env ruby -wKU

require 'open-uri'
require 'cgi'

def entity_escape(text)
  text.gsub(/&(?!([a-zA-Z0-9]+|#[0-9]+|#x[0-9a-fA-F]+);)/, '&amp;')
end

def make_link(text)
  case text
  when %r{\Ahttps?://.*?\.\w{2,4}.*?\z}:
    entity_escape(text)
  when %r{\A(www\..*|.*\.\w{2,4})\z}:
    "http://#{entity_escape text}"
  when %r{\A.*?\.\w{2,4}\/?.*\z}:
    "http://#{entity_escape text}"
  else
    nil
  end
end


url = make_link %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip

print open("http://bit.ly/api?url=#{CGI.escape(url)}").read unless url.nil?

I’ll stop there for now. The Tools set also contains:

  • Clipboard HTML link

    Makes an html hyperlink (code, not rich text) from a url in the clipboard. Uses the Fill feature to request the link text.

  • Markdown Link

    Makes a Markdown format link from a url in the clipboard. Uses the Fill feature to request the link text.

  • Rounded Corners

    Uses the Fill feature to request a pixel radius, and creates cross-browser CSS for rounded corners. There are 5 variations, one for each corner and one for all corners.

  • CSS Reset

    Your typical CSS reset code, in Meyers and YUI flavors.

  • Make URL

    Take whatever text is in the clipboard and provide a best-guess URL for it. Handy if you have a qualified domain and just need the protocol added, or if you have an email address and want it to be a mailto: link.

  • Hashbang

    Instant hashbangs for ruby, osascript and bash.

  • Expand url (bit.ly)

    Expand a shortened bit.ly url to its original destination url.

  • CloudApp Direct Link

    Get the direct link to a Cloud.app upload.

Feel free to download, explore and improve!

TextExpander Snippets v1.1

A collection of TextExpander snippets including lots of Lipsum, improved URL shortening and other useful scripts.

Published 05/07/10.

Updated 05/07/10. Changelog

DonateMore info…