Fun with TextExpander and Scripting

Philip Blog, TextExpander, TextExpander Tips

TextExpander 5 adds support for JavaScript snippets, which can be run on all of your devices – Macs, iPads, and iPhones. Here is a sample script which rounds the current date to the nearest 5 minute interval:

// Rounded date
// Rounds up or down to the most recent 5 minute interval
// For example, 08:18 rounds to 08:20, and 08:17 rounds to 08:15
// Thanks to William Alba for submitting

currentTime = new Date();

hours = currentTime.getHours();
minutes = currentTime.getMinutes();

minutes_rounded = 5 * Math.round(minutes/5);

// handle cases when minutes round to 60, and when hours increment to 24

if (minutes_rounded == 60) {
  minutes_rounded = 0;
  hours = hours + 1;
if (hours == 24)
  hours = 0;

// 24-hour format requires leading zero

if (hours < 10)
  hours = "0" + hours;

if (minutes_rounded < 10)
  minutes_rounded = "0" + minutes_rounded;

hours + ":" + minutes_rounded

TextExpander itself is scriptable via AppleScript and JavaScript for automation. User Vlad Ghitulescu wanted to create a large number of snippets for inserting the day and time in OmniFocus. For example, he wanted to type “,di09” and have it expand to “Dienstag 09:00”. Did I mention that Vlad works in German? Anyway, we worked with him to create a script to build the group he needed:

set greetWords to {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, ¬

“Friday”, “Saturday”, “Sunday”}

— set greetWords to {“Montag”, “Dienstag”, “Mittwoch”, “Donnerstag”, “Freitag”, “Samstag”, “Sonntag”}

set abbreviationPrefixes to {“mo”, “tu”, “we”, “th”, “fr”, “sa”, “su”}

— set abbreviationPrefixes to {“mo”, “di”, “mi”, “do”, “fr”, “sa”, “so”}

tell application “TextExpander”

set newGroup to make new group with properties ¬

{name:”Label with Date & Time”}

tell newGroup

if (count greetWords) is not (count abbreviationPrefixes) then

display dialog “The number of greetWords must match ¬

the number of abbreviationPrefixes”


end if

repeat with index from 1 to 6

set greetWord to item index of greetWords

set abbreviationPrefix to item index of abbreviationPrefixes

repeat with x from 6 to 22

set newAbbreviation to “,” & abbreviationPrefix

if x is less than 10 then

set newAbbreviation to newAbbreviation & “0”

end if

set newAbbreviation to newAbbreviation & x

if x is less than 10 then

set snippetText to greetWord & ” 0″ & x & “:00”


set snippetText to greetWord & ” ” & x & “:00”

end if

set newSnippet to make new snippet with properties ¬
{abbreviation:newAbbreviation, plain text expansion:snippetText}

end repeat

end repeat

end tell

end tell

The original script was written for use in German, so I’ve commented the German words and abbreviations and replaced them with their English equivalents. Feel free to swap the commenting if you’d prefer them in German, or feel free to change them for other languages.

If you’ve been having fun scripting with TextExpander, come join the TextExpander Tips Google+ Community and share.

View this article on our Blog

Category: All posts, TextExpander, TextExpander Tips,