Getting a TextExpander snippet into a KM variable

Anyone have a suggestion for how I could get the output of a TextExpander snippet into a KM variable? I'm aware that I could reproduce it in KM itself but it's somewhat complex, includes two other snippets and I want to use it on iOS with TextExpander; my goal is to not have it in two places so that I'll inevitably forget to update one or the other.

I know I can use AppleScript to tell it to expand but short of prompting for input, pausing, having KM type the snippet, having it hit okay I'm not sure how to capture the expanded string rather than outputting success or failure.

Maybe do something with osascript? Not sure how to approach that though.

I have seen the Do TextExpander Snippet plugin but it essentially gets me no closer since it just tells TextExpander to expand the snippet in whatever has text focus.

Hey Chris,

AppleScript is probably your solution.

Here's what I would do with Typinator:

tell application "Typinator"
   set TypinatorExpansion to plain expansion of rule "cs " of rule set "Names"
end tell

--> Christopher Stone

tell application "Keyboard Maestro Engine"
   setvariable "tinExpansion" to TypinatorExpansion
end tell

Drop TextExpander on the Applescript Editor or better yet Script Debugger (The demo reverts to SD-Lite after 20 days, but it's still way more powerful than the Applescript Editor.)

TextExpander's syntax will be a bit different than Typinator's, but you can probably figure it out. If not let me know.


1 Like

Thank you so much Chris! Apologies for the slow reply, I had some unexpected stuff come up.

That was exactly what I needed, with some modifications due to TextExpander's dictionary. Here's the working code:

tell application "TextExpander"
	set TextExpanderExpansion to expand string "/teGenerateToday"
end tell

tell application "Keyboard Maestro Engine"
	setvariable "Today_Template" to TextExpanderExpansion
end tell

For anyone else who might want to reproduce things in the future there were some bumps in the road.

Using the plain text expansion property on the snippet object does not actually expand the snippet. If you have date/time components, nested snippets or anything else dynamic they will not be expanded. The property would more accurately be called contents instead of expansion.

The expand string command is thankfully what I wanted but it's also buggy, it will expand the dynamic components but for some reason the string returned omit's newline characters up until the first nested snippet which will expand, white space intact. To get around this I created a superfluous utility snippet that just contains a nested reference to the snippet I actually want, using this works perfectly. I have submitted a ticket to TextExpander so hopefully it won't be an issue in the future but if you run into it, this is how I managed to work around the issue.

1 Like