TextExpander Snippet: Path to Selected Finder Item

Thanks to this post on the Keyboard Maestro forum:

tell application "Finder"
set finderSelList to selection as alias list
end tell
if finderSelList ≠ {} then
repeat with i in finderSelList
set contents of i to POSIX path of (contents of i)
end repeat
set AppleScript's text item delimiters to linefeed
return finderSelList as text
end if

Update:

Right click on any Finder item, then hold Option. “Copy” becomes “Copy as Pathname”!!

(Thanks @anatomyofashane - you are a genius.)
@masnick

TextExpander: Clipboard to Titlecase with JavaScript for Automation (JXA)

Here's a TextExpander snippet (well, two snippets) for titlecasing the clipboard contents:

#!/bin/bash
echo -n "%snippet:xxxxtitle%" | tr -d '\n'
// https://github.com/gouch/to-title-case/blob/master/to-title-case.js
String.prototype.toTitleCase = function(){
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
if (index > 0 && index + match.length !== title.length &&
match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
(title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
title.charAt(index - 1).search(/[^\s-]/) < 0) {
return match.toLowerCase();
}
if (match.substr(1).search(/[A-Z]|\../) > -1) {
return match;
}
return match.charAt(0).toUpperCase() + match.substr(1);
});
};
var app = Application.currentApplication();
app.includeStandardAdditions = true;
clipboard = app.theClipboard().toTitleCase();
clipboard

(Two snippets are necessary because of a bug that appends a newline to the end of any JXA snippets. This will be fixed in TextExpander 5.0.1, but in 5.0 it's necessary.)

TextExpander Snippet to Reformat U.S. Phone Numbers from the Clipboard

If you give it 10 or 11 digits in any format, this snippet will spit it out in the correct (555) 555-5555 format.

#! /usr/bin/env ruby
# Source: tadman on StackOverflow - http://stackoverflow.com/a/5913838
def formatted_number(number)
digits = number.gsub(/\D/, '').split(//)
if (digits.length == 11 and digits[0] == '1')
# Strip leading 1
digits.shift
end
if (digits.length == 10)
return %Q[(#{digits[0,3].join('')}) #{digits[3,3].join('')}-#{digits[6,4].join('')}]
end
end
text = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}
print formatted_number(text)
view raw phone_format.rb hosted with ❤ by GitHub

(Thanks to tadman on StackOverflow for the bulk of this script.)

You can download this as a TextExpander snippet file here.

Mail.app: Get URL for selected message with AppleScript and TextExpander

I often use message://<message-id@example.com> URLs to reference a specific email in OmniFocus or Evernote: clicking one of these URLs will automatically open up the source message in Mail.app.

There's not a great way to get these very handy message URLs without AppleScript, so based on this SuperUser answer, I created a TextExpander snippet.

Now, when I type xmsg, TextExpander will insert the message URL for the selected message in Mail.app into whatever application I'm working in.

Here's the TextExpander snippet, and here's the AppleScript:

tell application "Mail"
	set selectedMessages to selection
	set theMessage to item 1 of selectedMessages
	set messageid to message id of theMessage
	-- Make URL (must use URL-encoded values for "<" and ">")
	set urlText to "message://" & "%3c" & messageid & "%3e"
	return urlText
end tell