DT4 - Activity Record script broken

I have used this script from @pete31 to have a group that shows every doc I hit on a given day. I don’t look at the results daily, but if I need to remind myself of something, it is helpful. The Smart Rule gives me the following error:

On performSmartRule (Expected end of line, etc. but found number.)

I have looked at the code and with my extremely limited AppleScript knowledge, cannot figure out what it means. Any insight is appreciated. Thanks. Code for script is below:


property sortByLastOpened : true
property theExcludedDatabaseNames : {"_temp"}

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			repeat with thisRecord in theRecords
				if (name of database of thisRecord) is not in theExcludedDatabaseNames then
					
					set theHistoryRecord_Name to my formatDate(current date) & space & "Activity Record"
					set theResults to search "kind:markdown {any: name==" & theHistoryRecord_Name & " name==" & theHistoryRecord_Name & ".md}"
					
					if theResults ≠ {} then
						set theHistoryRecord to item 1 of theResults
						set theHistoryRecord_Text to plain text of theHistoryRecord
					else
						set theGroup to create location "/Daily Activity Record" in database named "FLI"
						set theHistoryRecord_Text to "<style> a {text-decoration: none;} </style>" & linefeed & linefeed & "# " & theHistoryRecord_Name & linefeed
						set theHistoryRecord to create record with {name:theHistoryRecord_Name, type:markdown, plain text:theHistoryRecord_Text, exclude from see also:true, unread:false} in theGroup
					end if
					
					set thisRecord_ReferenceURL to reference URL of thisRecord
					
					if thisRecord_ReferenceURL ≠ (reference URL of theHistoryRecord) then
						
						--added to original script:
						set {year:y, month:m, day:d, time:t} to (current date)
						set theTime to my secsToHMS(t as integer)
						set theDatabase to the name of current database
						--end addition
						
						set thisMarkdownLink to (theTime & ": " & "**" & theDatabase & "** : " & "[" & my escapeLinkName(name of thisRecord) & "](" & thisRecord_ReferenceURL & ")") as string
						if theHistoryRecord_Text does not contain thisRecord_ReferenceURL then
							set plain text of theHistoryRecord to theHistoryRecord_Text & linefeed & "* " & thisMarkdownLink & space & space
						else if sortByLastOpened then
							set plain text of theHistoryRecord to my updateOrder(theHistoryRecord_Text, thisRecord_ReferenceURL, thisMarkdownLink)
						end if
					end if
				end if
			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 performSmartRule

on formatDate(theDate)
	set theYear to year of theDate as string
	set theMonth to ((month of theDate) as integer) as string
	if (count theMonth) < 2 then set theMonth to "0" & theMonth
	set theDay to day of theDate as string
	if (count theDay) < 2 then set theDay to "0" & theDay
	return theYear & "-" & theMonth & "-" & theDay
end formatDate

on escapeLinkName(theText)
	set d to AppleScript's text item delimiters
	repeat with thisCharacter in {"[", "]", "(", ")", ">"}
		set AppleScript's text item delimiters to {"\\" & thisCharacter, thisCharacter}
		set theTextItems to text items of theText
		set AppleScript's text item delimiters to "\\" & thisCharacter
		set theText to theTextItems as text
	end repeat
	set AppleScript's text item delimiters to d
	return theText
end escapeLinkName

--added to original script: this gets the current time
on secsToHMS(secs)
	--from <http://macscripter.net/viewtopic.php?id=27203>
	tell (1000000 + secs div hours * 10000 + secs mod hours div minutes * 100 + secs mod minutes) as string ¬
		to return texts 2 thru 3 & ":" & texts 4 thru 5 & ":" & texts 6 thru 7
end secsToHMS

on updateOrder(theText, theReferenceURL, theMarkdownLink)
	set theDelimiters to {"(" & theReferenceURL & ")" & space & space & linefeed, "(" & theReferenceURL & ")" & space & space}
	set theTextItems to my tid(theText, theDelimiters)
	set theTextItems_modified to {}
	repeat with i from 1 to (count of theTextItems)
		set thisTextItem to item i of theTextItems
		set thisTextItem_Paragraphs to paragraphs of thisTextItem
		set thisTextItem_Paragraphs_Count to (count of thisTextItem_Paragraphs)
		if thisTextItem_Paragraphs_Count > 0 then
			if item -1 in thisTextItem_Paragraphs contains "x-devonthink" then
				set theTextItems_modified to theTextItems_modified & thisTextItem_Paragraphs
			else if thisTextItem_Paragraphs_Count > 1 then
				set theTextItems_modified to theTextItems_modified & items 1 thru -2 in thisTextItem_Paragraphs
			end if
		end if
	end repeat
	return my tid(theTextItems_modified, linefeed) & linefeed & "* " & theMarkdownLink & space & space
end updateOrder

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid
type or paste code here

Define hit.

And post a screencap of the output of this script.

Anything I look at. Does not need to be specifically opened. Just highlighted in the item list.

And the screencap?

Yes it just occurred to me to send that. The result is a markdown doc that looks like this:

Is that supposed to be a bulleted list?

Yes. It’s just a simple bulleted list that links to whatever document I have looked at on a specific day and lists the time it was accessed. That screenshot you looked at is the source view.

:thinking:

Not sure why yours works. When I run it in script editor, I get the error and in this portion of code

tell (1000000 + secs div hours * 10000 + secs mod hours div minutes * 100 + secs mod minutes) as string ¬
		to return texts 2 thru 3 & ":" & texts 4 thru 5 & ":" & texts 6 thru 7
end secsToHMS

It is the numeral 2 that is highlighted.

I disabled it and pointed it elsewhere (Inbox), and still get the same error. Setup is as it always has been:

Because I wrote my own from scratch :wink:

It’s not super-optimized since I obviously have a lot more to do than script right now, but does that essentially look correct?

Hah! Well you are a better AppleScripter than I. If I can’t figure out how to get it to work again, it won’t be the end of the world. But it was useful when I needed it.

If you’re talking about your screenshot, yeah that is what the output would look like if it were working for me.

Moving stuff around, I noticed the little mentions icon next to the file name, so SOMETHING was happening. Well, I just found that a new folder with the Daily Activity Record is now in my Global Inbox. So it was working the whole time, the resulting document is now just saving elsewhere. Whoops. Now I just need to figure out why the change in location.

Trying to change the location from Inbox to the appropriate database broke it again. Now I get: on performSmartRule (Expected class name but found identifier). :rofl: this is driving me nuts.

PM me.