AppleScripting TextExpander

TextExpander version 3.3 [download] introduces AppleScript support, primarily oriented toward automating snippet and group management. TextExpander includes AppleScript support for:

  • creating and deleting groups
  • editing group properties
  • creating and deleting snippets
  • editing snippet properties
  • adding (importing) groups from files or from URLs
  • enabling/disabling expansion

These capabilities should be broad enough to allow for some interesting scripts to be created. As you develop scripts, you may encounter some limitations. Your feedback will help determine future expansion of TextExpander’s AppleScript vocabulary. Here are a couple of sample scripts to get you started. The first one creates a new group and then creates two snippets within it:

tell application “TextExpander”

–– create a new group

make new group with properties {name:”My New Script Group”}

set newGroup to the result

tell newGroup

–– create a snippet, then set the major properties one by one

make new snippet

set newSnippet to the result

set the abbreviation of newSnippet to “tastytreat”

set the plain text expansion of newSnippet to “Toasted marshmallows are a tasty treat”

set the label of newSnippet to “Snippet from AppleScript”

–– create a snippet, setting properties immediately

make new snippet with properties {label:”Second Scripted Snippet”, abbreviation:”scrptwo”, plain text expansion:”Eat Two Marshmallows”}

set secondSnippet to the result

end tell

end tell

The second script checks for the existence of a URL-based group. If no such group is found, that URL is imported:

 

–– This script checks to make sure that a group based on a particular URL is present.

–– If not, it imports that group

set success to false

set groupURL to (“https://smilesoftware.com/te/example.textexpander” as text)

–– note coercion above: TE group source is type text, not URL

tell application “TextExpander”

set sourceList to the source of every group

if sourceList contains groupURL then

–– the desired group is already present

set success to true

else

set newGroup to import URL group using URL groupURL

–– the returned group is a placeholder for URL contents, which are downloaded asynchronously

repeat with i from 1 to 10

delay 1 –– allow some time for download, then poll the group placeholder

tell newGroup

–– find the number of snippets in the group

–– unless the group is supposed to be empty, zero snippets indicates download failure

count snippets

set snipCount to the result

if snipCount is not equal to 0 then

set success to true

exit repeat

end if

end tell

end repeat

end if

end tell

if not success then

beep –– real error handling goes here

end if

 

Again, we welcome your feedback on TextExpander’s new AppleScript capabilities.