How to call in a Smart Rule's embedded script another external Smart rule script

I have two Smart Rule scripts with performSmartRule handlers which do the same thing just one variable/property is different in these scripts. I list one of the two scripts in its minimal form list at the end of this post.

And I have two Smart Rules searching different DT groups and calling respectively one of these scripts.

So instead of having two scripts I would like to have only one script which I could call from the two rules with a different parameter. The Execute External Script action doesn’t provide parameters to its called script, so my idea is to use the Embedded script action and call in the embedded script somehow out to the performSmartRule handler in the external script with different parameters.

However, my Applescript knowledge is not advanced enough for this.- Hence my question: Any idea how to do this?


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property theOpfToolPath : "~/Dropbox/devel/calibre/calibre2fs/opf2tag"

-- THIS PROPERTY/VARIABLE ONLY DIFFERES BETWEEN THE TWO SCRIPTS
property theOpfDir : "~/Dropbox/devel/calibre/calibre2fs/data/opf"

on getOpfTag(tag, docPath, opfDir)
	return do shell script theOpfToolPath & " -t " & tag & " \"" & docPath & "\" \"" & opfDir & "\""
end getOpfTag

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set thePublisher to my getOpfTag("Publisher", path of theRecord, theOpfDir)
			set res to add custom meta data thePublisher for "Publisher" to theRecord			
		end repeat
	end tell
end performSmartRule

Afaik, AppleScript permits to call functions defined in another file. Maybe searching for AppleScript and library helps?

The problem is, as I see it, to pass the call which should go to the performSmartRule handler in the embedded scriot to a performSmartRule handler in another script plus giving that second performSmartRule handler an additional parameter…

Create a handler like this and save it in ~/Library/Script Libraries/

-- Script library that is called from within an embedded Smart Rule script

on theHandlerWithAdditionalParameter(theRecords, newText)
	tell application id "DNtp"
		try
			repeat with theRecord in theRecords
				set plain text of theRecord to newText
			end repeat
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end theHandlerWithAdditionalParameter

Then in your Smart Rules script call this script library’s handler like this:

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			
			script "Script Library - TEST"'s theHandlerWithAdditionalParameter(theRecords, "Test 1")
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performSmartRule

:warning: Note:

Do not use this example on your records! It overwrites the text.

(didn’t meant you @halloleo)

2 Likes

Thanks a lot @pete31!

Looks like there’s nothing magic about the performSmartRule in the subsequent script - just a normal handler/procedure call. I guess only in the first (the embedded) script the handler has to have the correct name and signature for DT3 to pick it up. :slight_smile:

Again, thanks a lot.

1 Like