Trying to highlight pdf within applescript and issues with system events

I have a program that works with the selected text of a pdf within DT3 and I want the program to also highlight the selected text in the pdf. I haven’t found a good way to do this, so I am trying to use a mac keyboard shortcut attached to the DT3 ‘highlight yellow’ command from within the script using system events and keystroke. I have a program below that works when I run it from scriptdebugger (i.e., the pdf selection is highlighted), but won’t work when I attach the script to its own keyboard shortcut and run it in DT3. I think it has something to do with which application is active when, but I don’t know enough to figure out the issue.

Oddly, if I close and reopen DT3, I can successfully invoke the script using the keyboard shortcut exactly once (i.e., the pdf selection is successfully highlighted), but it won’t work if I invoke it again.

Here is the script that tries to call my shortcut for Highlight Yellow, which is shift-ctrl-y (note that it doesn’t work if I remove the line to activate the application DEVONthink 3):

on run {}
	tell application id "DNtp"
			tell application "DEVONthink 3" to activate
			tell application "System Events" to keystroke "y" using {shift down, control down}
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
	end tell
end run

Any advice? If there is an easier way to highlight a selection of pdf text from within applescript, I am open to that also. Thanks in advance.

Why don’t you simply activate outside of the try block? I don’t know if nesting tell blocks for the same do or don’t work.

What program are you referring to?

PS: Using GUI scripting has always been a fragile method of doing things.

1 Like

I suppose they say use program to mean script.

Possibly… so I’ll add another question: :slight_smile:

@samrose, what is it you’re specifically trying to do?

I am trying to highlight selected text in a pdf via applescript. I wanted to do it within the script because I am doing other things to the selected text at the same time.