Triggering Scripts Upon Opening Or Selecting DT Groups/Items

I would like to use scripts triggered upon opening DEVONthink Groups (equivalent to Finder Folders?) but I can’t seem to get them to work. I use a Mac Pro with Lion and DEVONthink Pro Office 2.3.5. The manual and “Getting Started” book do not describe how these scripts are triggered. I have checked this forum and seen scripts using “On Run” and “On Triggered(theRecord)” but can’t figure out if that’s what I need to use in a script to make it work when the group is opened. Does it work the same way as Finder Folder action scripts? As a simple test I tried to attach a pre-existing DEVONthink script (“Astronomy Picture Of The Day”) to a newly created group (in the “Information” window for that group in the “Script” field by using the “Select…” button to select the “Astronomy Picture Of The Day” script from the “…:Library:Application Support:DEVONthink Pro 2:Scripts” Folder. The script runs fine when executed from the Script menu but when I open the group to which the script has now been attached the group opens normally but the script does not execute.
If I need to write a separate script to activate the “triggering” mechanism, please clue me in. Also, what is the simplest way to then execute the “Astronomy Picture Of The Day” script (or any other previously written script for that matter) from within my new script (“run AppleScript” command?)
Also, is there a way to trigger the script to execute when the group is selected (not opened) in DT2?

Thanks in advance!

The standard structure of triggered scripts is this:

on triggered(theRecord)
	try
		tell application id "com.devon-technologies.thinkpro2"
			-- do a bunch of stuff	
		end tell
	end try
end triggered

DEVONthink triggered scripts are attached to a group using the Tools > Show Info > Script configuration box. Unless a triggered script contains the structure shown, it will not execute. You could wrap an existing script in the “on triggered” structure - though additional customization specific to that script might be needed.

Triggered scripts are executed when the group is selected. File system folder action scripts are executed when documents are added to, or removed from, a folder. This is an important difference. If you drag a document to a group’s name in the sidebar or the Group Selector, the script will not be triggered. You must explicitly select the name of the group with the attached script. It’s possible in the file system to send a file to a folder and cause the script to execute, without ever touching the folder in Finder. That is not possible in DEVONthink with attached scripts. (Though it is possible to script this by other, more complex, means.)

Thanks very much. I will try it out. Greatly appreciate the product overall as well as the responsiveness of support services and this forum.

Since this seems to be an extremely powerful and useful scripting feature of DT, please add the code snippet (and possibly an example or two) from your reply above to the official DT manual under the section “Triggered Scripts” to make the manual less generic and more useful. That way people can get started immediately with this powerful feature without the frustration of searching for this basic starting point information.

Please also help me with executing a script from within this triggered script (the “Astronomy Picture Of The Day” Script in the DT Script Menu would be a good example).

MarkB

I’m having trouble with this. I have a group structure as follows:

Finances
Discover

The Discover group is a smart group that pulls in all my discover card statements based on OCR data. The following script works great to rename the files if I select the files in the smart group:

-- First attempt to get soemthing working. Goal: extrate closing date, and rename file as DiscoverItCard_ddmmyyy.pdf
-- Discover card statement contains the line "Open Date: Oct 14, 2014- Close Date: Nov 13, 2014" which has the closing statement date we need.

-- Subroutines
-- Trim text from a string
on trim_line(this_text, trim_chars, trim_indicator)
	-- 0 = beginning, 1 = end, 2 = both
	set x to the length of the trim_chars
	-- TRIM BEGINNING
	if the trim_indicator is in {0, 2} then
		repeat while this_text begins with the trim_chars
			try
				set this_text to characters (x + 1) thru -1 of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	-- TRIM ENDING
	if the trim_indicator is in {1, 2} then
		repeat while this_text ends with the trim_chars
			try
				set this_text to characters 1 thru -(x + 1) of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	return this_text
end trim_line

-- Find the index of an item in a list
on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then return i
	end repeat
	return 0
end list_position

-- Declare variables (mostly because I don't understand scope yet.)
set monthslist to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
set rcdtext to ""
set mycat to ""
set newname to "DiscoverItCard_"
set mydate to "999"
set mymonth to ""
set myday to ""
set myyear to ""
set rcdtext to ""
set searchToken to ""
-- The Discover card statemetns have the closing date preceeded by "Close date: ".
set searchToken to "Close Date: *... [0-9]\\+, *...."

-- Code to trigger when moving into a smart group
-- on triggered(theRecord)
try
	tell application id "com.devon-technologies.thinkpro2"
		try
			-- set theselection to the children of theRecord -- selection
			set theselection to selection
			if theselection is {} then error "Please select some records."
			
			-- Repeat with thisitemhere from 1 to count of items of input.
			repeat with theRecord in theselection
				
				try
					set rcdtext to the plain text of theRecord
				on error
					display alert "No text to look at. -- " & rcdtext
				end try
				
				set mycat to rcdtext as text
				-- Debug display
				-- display dialog mycat
				
				try
					do shell script "echo " & quoted form of mycat & " | grep -o " & quoted form of searchToken
					set mydate to words of (my trim_line(first paragraph of result, "Close date: ", 0))
				on error
					display alert "Date Token not found."
				end try
				
				-- Now that we have the date we want, we need to remove the excess text and formate the date.
				
				set mymonth to item 1 of mydate
				set myday to item 2 of mydate
				set myyear to item 3 of mydate
				
				-- Convert the date to the correct format
				-- Convert Month to correct format
				set monthNum to my list_position(mymonth, monthslist)
				if monthNum < 10 then
					set mymonth to "0" & monthNum as text
				else
					set mymonth to monthNum as text
				end if
				
				-- Convert day to correct format
				if myday < 10 then
					set myday to "0" & myday as text
				end if
				
				set newfilename to newname & mymonth & "_" & myday & "_" & myyear & ".pdf"
				
				-- Debug display
				-- display dialog newfilename
				set the name of theRecord to newfilename
				
			end repeat
		on error
			display alert "No records selected."
		end try
		
	end tell
	
end try
--end triggered

But if I try to trigger it with the smart group selection nothing happens to the files inside the smart group. Any help would be appreciated.

Smart groups don’t have any real children and therefore this does not yet work.

Would a script work for an ordinary (non-smart) group upon triggering the group? For example, I want to try to script the UI to set certain columns in the item list when I select a particular group. I have managed to get the beginning part of this from MacOS Automation, but when I try to place the code inside the triggering-event code provided by @korm above, I get an error in Script Editor when trying to compile the script.

Here is what I am trying:

on triggered(theRecord)
	try
		tell application id DNtp
			-- do a bunch of stuff

				on do_submenu(DNtp, View, Columns, "Customize Metadata…")
	              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
							tell menu item menu_item
								tell menu menu_item
									click menu item submenu_item
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_submenu
			
		end tell
	end try
end triggered

And the error highlights the do_submenu subroutine and throws a syntax error: Expected “error” but found identifier.

Might there be someone who can provide some insight into what I need to change to resolve the syntax error? Unfortunately I cannot find anything in any of the Apple Script guides I know of that answers this question directly.

This

tell application id DNtp

Should be this

tell application id "DNtp"

Also, triggered scripts need to be tried from within DEVONthink since Script Editor does not emulate the “on triggered” context.

Beyond that, there’s quite a bit of erroneous structure in the script. The tell...end tell structures are in the wrong places. You’re calling a function with arguments that make no sense, including trying to call DEVONthink from DEVONthink. I’m afraid your cut & paste job has ended in a bit of a mess, sorry. Needs major surgery.

Perhaps someone here has time to rewrite the script for @nm1?

I seem to have made at least some progress, and your comment about calling DEVONthink from within DEVONthink helped me with that…

What I am trying to do at this point is to force the submenu item “Customize Metadata…” to appear when I open a particular group.

The following script now successfully compiles and saves, but it does not cause anything to happen within DEVONthink when I access the group to which it is attached as a triggered script:

on triggered(the_record)
	tell application id "DNtp"
		try
		tell application "System Events"
			tell process DNtp
				tell menu bar 1
					tell menu bar item menu_name
						tell menu menu_name
							tell menu item menu_item
								tell menu menu_item
									click menu item submenu_item
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end tell
end triggered

I also tried using "DNtp" in the line tell process DNtp in case the process name needs to be quote-enclosed, but that hasn’t made a difference.

With all the nested tell ... end tell structure, ultimately the only thing the script does is this

click menu item submenu_item

Which does nothing because there the script interprets submenu_item as a variable, and that variable has not been provided.

Also, as before, this

tell process DNtp

should be

tell process "DEVONthink 3"

applications, IDs, processes … all of these address quoted entity names.

Even if that syntax mistake is fixed, however, the way it is written, the script does nothing at all. Compiles correctly, though. That’s progress :slight_smile:

Try this. If it doesn’t work reliably uncomment the delays.

By the way Script Debugger (there’s a free edition) which I first heard of from @korm was probably the most important thing to learn AppleScript. If you plan to do more of this menu clicking stuff get a copy of Keyboard Maestro. I first hesitated as I already had several other shortcut apps but in the end none of them could replace Keyboard Maestro’s “Show Menu” action (which means “Click Menu”), so AppleScript is not the best way to go…

-- Click DEVONthink Menu "View" → "Columns" → "Customize Metadata…"

on triggered(theRecord)
	try
		activate application id "DNtp"
		tell application "System Events"
			tell process "DEVONthink 3"
				click menu bar item 8 of menu bar 1
				#delay 0.2
				click menu item 25 of menu 1 of menu bar item 8 of menu bar 1
				#delay 0.2
				click menu item -1 of menu 1 of menu item 25 of menu 1 of menu bar item 8 of menu bar 1
			end tell
		end tell
		
	on error
		display notification "Error"
	end try
end triggered

I agree 100%

Keyboard Maestro can do the job better than AppleScript. Alternative to KM for this sort of UI manipulation is BetterTouchTool – if you have Setapp, BTT is included.