LaTeX addresses on the Mac and iOS
January 23, 2020 at 8:38 PM by Dr. Drang
A couple of years ago, as I was first learning how to use Shortcuts,1 I wrote a shortcut that grabbed the name and address of the selected person in Contacts, formatted it for use in LaTeX, and put it on the clipboard. The result was something like
John Cheatham\\
Dewey, Cheatham \& Howe\\
1515 Loquitor Lane\\
Amicus OH 44100
where the key is to end each line (except the last) with double backslashes and to backslash escape an ampersands. The shortcut was run from the Share Sheet, and while I’ve made some minor changes to it, it still serves me well when I’m writing on the iPad.
Recently, though, I realized that I didn’t have anything similar when writing on the Mac. Oh, I have a command-line tool for looking up a contact and outputting a LaTeX-formatted address—it’s an extension of this ancient script—but what I wanted was something that worked from the Contacts app, a system parallel to what I have on iOS.2 I got that through a combination of AppleScript and Keyboard Maestro
Let’s start by looking at the current version of my LaTeX Address shortcut.
Step | Action | Comment |
---|---|---|
0 | This will appear in the Share Sheet for Contacts | |
1 | Get the Street Address from the contact. We’ll pull more info from it later. | |
2 | Assemble the name and address. By default, the Shortcut Input for contacts returns the full name. The Company also comes directly from the contact. The parts of the address come from the Street Address extracted in Step 1. | |
3 | LaTeX treats ampersands as special characters, so we have to escape them. | |
4 | This looks like an empty text field, but it contains a newline character. The magic variable associated with this action has been renamed Newline. | |
5 | Put two backslashes in front of all the newlines. You might think would be simpler to just add the backslashes directly in Step 2, but that wouldn’t work when the Street part of an address is two lines long. | |
6 | Put the LaTeXified name and address on the clipboard. |
If you compare it to the original version, you’ll see that it’s cleaner now, mainly because Shortcuts’ inclusion of parameters in iOS 13 has reduced the need for Set Variable and Use Variable actions. Also, I’ve simplified the text replacement in Step 5 to avoid the messy regular expression I used to use. If you have a need for something like this, you can download it.
So to get the LaTeXified address onto the clipboard when working on iOS, I
- Open Contacts.
- Find the person.
- Open the Share Sheet.
- Choose the LaTeX Address shortcut.
For the analogous behavior on the Mac, I use this Keyboard Maestro macro:
It’s triggered by the ⌃⌥⌘L key combination, but only when Contacts is the frontmost application. It runs an AppleScript to put the LaTeXified name and address of the currently selected contact on the clipboard and then hides Contacts itself.3 Here’s the AppleScript:
applescript:
1: set latexList to {}
2: tell application "Contacts"
3: tell the selection as list
4: set thePerson to item 1
5: tell thePerson
6: set end of latexList to name
7: set end of latexList to organization
8: set addr to formatted address of address 1
9: set latexList to latexList & (paragraphs of addr)
10: if last item of latexList is equal to "USA" then
11: set latexList to items 1 through -2 of latexList
12: end if
13: end tell
14: end tell
15: end tell
16:
17: set AppleScript's text item delimiters to "\\\\
18: "
19: set the clipboard to latexList as string
Basically, it creates a list, latexList
, of lines for the name and address and then combines the list items with two backslashes and a newline as separators. Lines 17–18 define the separator (it has to double each backslash because AppleScript treats them as special characters), and the latexList as string
part of Line 19 combines them. The only other tricky part of the script is Lines 10–11, which strip the country from the end of the address if it’s “USA.” I’m able to get away with this rather simple way of deleting the country from domestic addresses because I standardized on “USA” many years ago and have stuck with it as I add new Contacts entries.
To get a LaTeXified address onto the clipboard when working on a Mac, I
- Activate LaunchBar.
- Start typing the person’s name.
- Press the Return key when LaunchBar shows the match. This activates Contacts with that person selected.
- Type ⌃⌥⌘L to run the LaTeX Address macro.
I would dearly love to be able to run a command directly from LaunchBar after Step 2, never activating Contacts, but I don’t know how. I guess typing Return and then ⌃⌥⌘L isn’t especially onerous.