Getting UI Scripting to work

Has any one had any success in being able to script the Devonthink interface using UI scripting.

This is a simple example to highlight a PDF document using UI scripting that works when run from the Applescript Editor but not when run from Devonthink’s Script menu. When run from the script menu, it hangs for about 15 seconds with the script menu highlighted and then exists without an error message and without apparently doing anything)

try
	tell application id "com.devon-technologies.thinkpro2"
		do_menu("DEVONthink Pro", "Format", "Highlight") of me
	end tell
	
on error errMsg number errNum
	display alert (localized string "An error occured when highlighting") message errMsg as warning
end try

on do_menu(app_name, menu_name, menu_item)
	try
		-- bring the target application to the front
		tell application app_name
			activate
		end tell
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					tell menu bar item menu_name
						tell menu menu_name
							click menu item menu_item
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_menu

(the do_menu subroutine comes from http://www.macosxautomation.com/applescript/uiscripting/ and I have found it the most reliable way generally to deal with UI scripting of menus)

Obviously you could do this from the menu if this is all you wanted to do, but in my case I would like to be able to do this from within a script that does several things.

I have searched through the forums but I havent found any other examples of UI Scripting so any ideas would be appreciated.

Frederiko

This seems to be one of those scripts that works from within the global Scripts menu (system-wide) but not from DEVONthink’s script menu. I was able to install the script in ~Library/Scripts/Applications/DEVONthink Pro. Before it would operate the first time, Gatekeeper asked me to allow SystemUIServer to control the computer – a setting in System Preferences > Security & Privacy > Privacy > Accessibility. With that done, the script operates as-is from the global Scripts menu. The Gatekeeper issue might affect DEVONthink. I don’t know.

Thanks korm for putting me on the right track. I have managed now to hack the Quotation and Annotation script
[url]QuoteHighlight&Annotate script] to highlight a pdf at the same time as making the annotation.

Its still very hackish because moving a script template out of the Devonthink hierarchy breaks other things as well and you have to use Fastscripts to attach a keyboard shortcut to it once its in the general applescripts folder. I need to tidy it up before its posted

Frederiko

You know, this can all be done in Keyboard Maestro with two actions:

  • Type the command-shift-L keystroke (i.e., highlight selected text)
  • Select menu item (i.e., the menu where the annotation template can be found on your machine)

… just saying

For those people who run into UI Scripting issues in the future, these are the instructions for giving UI Scripting access to the accessibility interface :

http://macosxautomation.com/mavericks/guiscripting/reset.html