Using external script in smart rule

I’m trying to use the “Download URLs as PDF documents (Paginated)” script in a smart rule (copied to smartrules folder). It runs but doesn’t work.

I’ve added the on performSmartRule lines (2). I’m not an expert so prob something else is needed. Maybe using selection is the issues instead of theRecord (in theRecords).

When I run as is below, it just hangs on the status “Downloading…” xxx. I have to restart DT as it’s stuck in that loop I think. The smartrule is triggered on clipping a webloc kind. I’m clipping a URL that links to a PDF. When the script is run from the menubar on the same webloc selection it works—PDF is generated.

Any help is appreciated!

-- Download URLs as PDF documents (Paginated)
-- Created by Christian Grunenberg on Mon Mar 23 2009.
-- Copyright (c) 2009-2014. All rights reserved.
on performSmartRule(theRecords)
	tell application id "DNtp"
		set theSelection to the selection
		if theSelection is not {} then
			try
				show progress indicator "Downloading..." steps (count of theSelection)
				repeat with theRecord in theSelection
					set theName to name of theRecord
					set theURL to URL of theRecord
					step progress indicator theName
					if theURL begins with "http:" or theURL begins with "https:" then
						set theParents to parents of theRecord
						set theCopy to create PDF document from theURL name theName in (item 1 of theParents) with pagination
						repeat with i from 2 to (count of theParents)
							replicate record theCopy to (item i of theParents)
						end repeat
						set creation date of theCopy to creation date of theRecord
						set comment of theCopy to comment of theRecord
						set rating of theCopy to rating of theRecord
						set label of theCopy to label of theRecord
						set state of theCopy to state of theRecord
					end if
				end repeat
				hide progress indicator
			on error error_message number error_number
				hide progress indicator
				if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			end try
		end if
	end tell
end performSmartRule

I doubt that there’s a selection available in the handler. You have to work with the records passed in as parameter.

Remove those lines (and the rest of the if condition), and change

to repeat with theRecord in theRecords.

As @chrillek said, the records are passed by the smart rule, there is no selection.

Is this smart rule only applied to bookmarks or web documents? In that case using the action Convert to PDF (Paginated) should be sufficient.

Nice. That actually seems to be working for me.

Thanks to @Blanc and @chrillek for help too. I had tried to replace selection with theRecord but wasn’t successful. It just got stuck and had to kill DT. I’ll revisit one day when I know more just to see why.

1 Like