Help with script: retrieving reference URL on import to inbox

I have a Smart Rule that is set to run on import of items to Inbox. It runs an embedded AppleScript. The purpose of the script is to write a markdown link for the imported item to a log file in my system. For that, I need to access the name and devonthink item link.

the smart rule often works, but sometimes, it gives me the error “Expected end of line, etc. but found class name.”

The first few lines of the script are:

-- a smart rule on new imports into Inbox

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theName to name of theRecord
			set theLink to reference URL of theRecord

When view the script in the editor, the error highlights the “URL” word…

Just an idea, and I can’t play with it now. I wonder whether sometimes the reference URL is not yet available when the script runs. I would try putting a delay 1 in before the repeat loop. If, that is, there is no obvious logic to which items are working and which are not.

aw yeah! that did the trick. Nice insight. thanks

1 Like

A delay is usually just a workaround but doesn’t fix the actual issue. Therefore the complete source of the smart rule would be useful plus a screenshot of the smart rule’s conditions.

E.g. one idea coming to my mind is that the smart rule processes several records including both groups and documents due to its conditions and processing the first records of the list invalidates the remaining ones and then causes the error.

Okey doke. thanks.

and the script is:

-- a smart rule on new imports into Inbox

on performSmartRule(theRecords)
	tell application id "DNtp"
		delay 1
		repeat with theRecord in theRecords
			set theName to name of theRecord
			set theLink to reference URL of theRecord
			--set the clipboard to (theLink & " " & theName) as string
			--handle the link
			set otid to AppleScript's text item delimiters
			
			if theLink contains "%3C" then
				set AppleScript's text item delimiters to "%3C"
				set {var1, var2} to {text item 1, text item 2} of theLink
				set AppleScript's text item delimiters to "%3E"
				set var3 to text item 1 of var2
				set AppleScript's text item delimiters to otid
				set theLink to var1 & "%253C" & var3 & "%253E"
			end if
			
			--now handle the name
			set theName to do shell script "echo \"" & theName & "\"|perl -pe's/([^-_.~A-Za-z0-9])/sprintf(\"%%%02X\", ord(\"$1\"))/seg'"
			
			--trim newline from end of name if it's there
			set otid to AppleScript's text item delimiters
			
			if theName contains "%0A" then
				set AppleScript's text item delimiters to "%0A"
				set var1 to text item 1 of theName
				set AppleScript's text item delimiters to otid
				set theName to var1
			end if
			--combined into a URL call
			set theURL to "drafts://x-callback-url/append?uuid=F5998E63-A07D-41C5-AC81-3F12A92BEA3D&text=" & "[" & theName & "](" & theLink & ")"
			open location theURL
		end repeat
	end tell
end performSmartRule


Why haven’t you added and criteria to the smart rule?

PS: Even without criteria and using a UUID from a Drafts file (which I only have for testing), I have no issue with this smart rule.

On a Catalina 10.15.7 Mac at the moment.

PS: I took out the delay before I tested it.

set AppleScript's text item delimiters to "%3C"

I’m guessing you’re processing emails here.

if theName contains "%0A" then

But when do you expect this to occur?

Trying to remember. Possibly to manage the case where I dragged several files from Finder to the Finder’s (devonthink) Inbox shortcut - that is, when multiple files are imported this way, there’s a stray linefeed to be avoided.

Here’s an improved version, noting you should add Tag is not Logged or some matching criteria to avoid reprocessing the files.

Log Imports to Drafts.dtSmartRule.zip (1.5 KB)

And the script in it…

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theName to name of theRecord
			set theLink to reference URL of theRecord
			set recordTags to tags of theRecord
			
			--handle the link			
			set theLink to do JavaScript "encodeURIComponent(\"" & theLink & "\");" in think window 1
			
			--now handle the name
			set theName to do JavaScript "encodeURIComponent(\"" & theName & "\");" in think window 1
			
			--combined into a URL call
			set theURL to "drafts://x-callback-url/append?uuid=CFB45169-A532-44E6-8C52-BC5B0B26E47C&text=" & "[" & theName & "](" & theLink & ")"
			set tags of theRecord to (recordTags & "Logged")
			open location theURL
		end repeat
	end tell
end performSmartRule