Back to Mobile View

Skip to Content

Revisiting the randomized signature AppleScript, now with API goodness

When I shared an AppleScript last week which created randomized signatures using TextExpander, I promised to revisit it when the iheartquotes.com API started working again. It's working right now, but it seems to go up and down; check this link before you try the script and make sure you get a quote, not an error message.

As before, when used with a TextExpander snippet set to the AppleScript type, this script will provide a random quote, this time pulling it from the Internet rather than our own list. The script is essentially the same, so I'm only going to explain the part which changed; visit the previous article for a rundown on the rest:

set _date to do shell script ¬
"date +'Sent on %A, %b %d at %l:%M %p'|tr -s ' '"

set _quote to ""

try

set _quote to do shell script "curl -s \\
'http://www.iheartquotes.com/api/v1/random?source=macintosh&max_lines=4'"

set {astid, AppleScript's text item delimiters} ¬
to {AppleScript's text item delimiters, return}
set _quote to paragraphs 1 through -3 of _quote as string
set AppleScript's text item delimiters to astid

end try

set _out to "
-Brett

" & _date & "
________________________________
Brett Terpstra | myemailaddress@mywebsite.com
The Unofficial Apple Weblog | http://www.tuaw.com

" & _quote

return _out

The curl call

Here's the part which makes this more interesting than it was before:

set _quote to do shell script "curl -s \\
'http://www.iheartquotes.com/api/v1/random?source=macintosh&max_lines=4'"

First off, that really should be all one line, but is split here for formatting purposes. You can remove the double backslash (\\) and merge the lines together if you like. What this does is use AppleScript's ability to run shell scripts (Unix commands) to make a curl call to retrieve a response from the iheartquotes API. Note the "try" statement surrounding that section; it will just return our regular signature without the quote if anything goes wrong. If it gets a response, it stores it in a variable (_quote) and then trims off the last line, like this:

set {astid, AppleScript's text item delimiters} ¬
to {AppleScript's text item delimiters, return}
set _quote to paragraphs 1 through -3 of _quote as string
set AppleScript's text item delimiters to astid

The first two lines (actually one line) are shorthand for setting "AppleScript's text item delimiters" -- the character that AppleScript uses to separate text items in a block of text -- to the return character and store the current text item delimiter in a variable so we can restore it with the last line. Using the paragraphs command, which automatically uses the return key to separate paragraphs, we turn the quote into a list of paragraphs, starting with the first and ending with the third from last. This is because the last two "paragraphs" are always an empty line and a permalink (even if you use the API's parameters to turn off the permalink). The last part of that line, "as string", is where the text item delimiters come into play, putting the list items back together with a return after each one, reconstructing the original formatting.

Note: You could do the same thing by adding |sed -n '$!p' to the end of the do shell script line, right before the last double quote. I suppose you might as well, as long as you're dipping out to the shell anyway ...

From there it's the same as before, just building a final output string from the pieces we've created. This method provides a much wider variety of randomized signatures than constructing your own list by making use of a small section of the wide array of data on the interwebs. The use of curl in an AppleScript can open a lot of possibilities, such as posting to Twitter in various ways. or shortening urls on the fly. Have fun.



When I shared an AppleScript last week which created randomized signatures using TextExpander, I promised to revisit it when the...