TextExpander Applescript to Retrieve DT3 Data

I am trying to experiment with TextExpander to retrieve data from Devonthink using Applescript - this was inspired by a recent Forum discussion on MPU with @korm on a related question with Javascript

This script within TextExpander works correctly in its viewing/test mode:

image

The question I have is how I get the response to appear when I execute the TextExpander snippet.

I read the TextExpander documentation at TextExpander Help: AppleScripting TextExpander and tried this:

I get this error:

image

I know I am close - what am I missing so I can return the Applescript response as the output of the snippet?

1 Like

I don’t use TextExpander but doesn’t it highlight the line it’s erroring on?

It does not highlight it, but by trial and error I know the problem is this line:

Expand abbreviation "jsdt3"

That line seems necessary based on the documentation. But interestingly I cannot find either “Expand” or “abbreviation” in the current Applescript dictionary for TextExpander.

So I expect there may be some newer or alternate way to specify what text should actually be inserted as the Macro response.

I cannot find either “Expand” or “abbreviation” in the current Applescript dictionary for TextExpander.

Why did you use those commands then? :thinking:

Because that is my interpretation of the documentation here Scripting TextExpander - but I am certainly open to other ideas

image

Move the TextExpander tell block out of the DEVONthink one. The editor is definitely showing weird formatting in your screen captures.

Same error

(And though the formatting looks weird - it works fine in the TextExpander preview if I do not using the expand instruction.)

image

Try writing the script in Script Editor instead.
Like I said, I don’t use TextExpander.

Interesting - that gives us another clue

So the documentation is wrong?? Or something changed?

So the documentation is wrong??

It’s certainly possible. The scripting dictionary is manually created.

Or something changed?

No idea. Again… I don’t use the app. :slight_smile:

OK I will ask TextExpander support - thank you

Open a new script in Script Editor and paste this…

tell application "TextExpander"
expand abbreviation ";fbreply"
end tell

According to the info you posted above, this would appear to function. Does it compile correctly?

Same error

Yeah, I’d ask the TextExpander folks on this one.

1 Like

Will do - thanks for the effort trying

You’re welcome.

As Bluefrog mentioned it’s always a good idea to write scripts in a dedicated script editor app and to paste the working code into other apps. Doing so immediately showed that abbreviation isn’t part of the current TextExpander version and that the linked documentation is not for the current version (as you meanwhile know).

You don’t need to expand the abbreviation inside itself via AppleScript - in fact I doubt that it’s possible at all to let an expansion trigger itself via AppleScript as it would probably endlessly trigger itself.

And you don’t need to activate DEVONthink to get its selection.

This works over here:

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Nothing selected."
		set Bigname to ""
		
		repeat with theRecord in theRecords
			set theName to name of theRecord
			set Bigname to Bigname & theName & linefeed
		end repeat
		
		return Bigname
		
	on error error_message number error_number
		if the error_number is not -128 then
			activate -- make sure to add an "activate" if you want to show dialogs
			display alert "DEVONthink" message error_message as warning
		end if
		return
	end try
end tell

If you want to know why an expansion that uses AppleScript doesn’t yield the desired result include an error Handler (as I did above) that uses activate to bring the app in question to the front. Without the activate the dialog would be shown anyway but chances are you won’t see it until you activate the app again.

Adding something that tells you that an error occurred is a good idea as without it it’s probably getting quite confusing: You’ll hear TextExpander’s sound but there’s nothing inserted.

1 Like

@pete31

You are indeed a scripting genius

Works perfectly

Many many thanks

1 Like

@pete31

Looks like you solved something which TextExpander says is unsolvable at the moment - Congrats

Thank you for contacting Smile Support. I’m sorry to hear that you’re having trouble with your snippet but those AppleScript commands did not make it into the most recent version and there is not currently an alternative. I’ve relayed your request to put this functionality back into TextExpander to our engineers and they will investigate doing that for a future update. If you’d like, you can move back to the last version where these AppleScript commands were available:

https://cdn.textexpander.com/mac/TextExpander_6.5.6.zip

(Note: You can drag this version into your Applications folder which will overwrite the existing application. You can also rename it before dragging to have both versions on your computer. However, only one version can run at a time. Your snippets are safe in a different location and on the TextExpander server.)

Also, your case is now linked to the open request so we will reach out when the issue is resolved. Let me know if you have any other questions, comments or concerns.

Thanks for using TextExpander from Smile!

This looks very interesting, and fantastic that the DT users are so willing and able to help!

But, if you don’t mind the question — could you maybe explain what you are actually aiming to do with the above? I’m sincerely curious, btw! I’m a heavy TE user as well, but would never have thought of inserting data from DT into a TE snippet — assuming that’s what’s being tried here?