Fun with TextExpander and Scripting

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:

     p.p1 {margin: 0.0px 0.0px 0.0px 20px; font: 12.0px Verdana}
     p.p2 {margin: 0.0px 0.0px 0.0px 20px; font: 12.0px Verdana; color: #5e6161}
     p.p3 {margin: 0.0px 0.0px 0.0px 40px; font: 12.0px Verdana; color: #0433ff}
     p.p4 {margin: 0.0px 0.0px 0.0px 40px; font: 12.0px Verdana}
     p.p5 {margin: 0.0px 0.0px 0.0px 40px; font: 12.0px Verdana; color: #4f8f00}
     p.p6 {margin: 0.0px 0.0px 0.0px 60px; font: 12.0px Verdana; color: #4f8f00}
     p.p7 {margin: 0.0px 0.0px 0.0px 80px; font: 12.0px Verdana}
     p.p8 {margin: 0.0px 0.0px 0.0px 60px; font: 12.0px Verdana}
     p.p9 {margin: 0.0px 0.0px 0.0px 80px; font: 12.0px Verdana; color: #4f8f00}
     p.p10 {margin: 0.0px 0.0px 0.0px 100px; font: 12.0px Verdana; color: #4f8f00}
     p.p11 {margin: 0.0px 0.0px 0.0px 100px; font: 12.0px Verdana}
     p.p12 {margin: 0.0px 0.0px 0.0px 120px; font: 12.0px Verdana; color: #4f8f00}
     p.p13 {margin: 0.0px 0.0px 0.0px 120px; font: 12.0px Verdana}
     span.s1 {font-variant-ligatures: no-common-ligatures}
     span.s2 {font-variant-ligatures: no-common-ligatures; color: #4f8f00}
     span.s3 {font-variant-ligatures: no-common-ligatures; color: #0433ff}
     span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}
     span.s5 {font-variant-ligatures: no-common-ligatures; color: #812fdc}
     span.s6 {font-variant-ligatures: no-common-ligatures; color: #012fbe}
     span.Apple-tab-span {white-space:pre}

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”

return

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”

else

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,