Here it is. It is the very same smart rule linked in this thread. I’ve changed the paragraph count verification from “=1” to “<=2” because some formatted notes contain an empty line (not all of them though).
I’ve noticed that the problem seems to occur for the notes where the group’s name, name of the formatted note containing the link and name of the PDF being linked to are all different. As far as I can say the script should be fixing that though (renaming the PDF to the group’s name, which is OK with me).
Another thing is many of the filenames are non-english and quite a few are in Japanese. This really shouldn’t be a problem, but who knows.
You could enclose the lines inside the repeat loop in a try ... on error ... end try block and show an alert in case of an error. An issue of the script would explain why not all items are processed although shown in the sidebar.
I’m not getting any errors. It seems to me the Smart Rule isn’t even executing the script for the remaining records, even though they show on the Smart Rule’s list…
Did you modify the script as suggested? How does the source now look like?
I lifted the error handling code from one of the examples attached to DEVONThink. I tried applying it at different stages, with no difference. The last version I tried was this:
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
try
if (count paragraphs) of (plain text of theRecord as string) ≤ 2 then
set currentLocation to (parent 1 of theRecord)
repeat with deadRecord in ((children of currentLocation) whose (type of it) is not formatted note)
set name of deadRecord to (name of theRecord as string)
move record deadRecord to parent 1 of currentLocation
move record currentLocation to trash group of (database of theRecord)
end repeat
end if
on error error_message number error_number
display alert "DEVONthink" message error_message as warning
end try
end repeat
end tell
end performSmartRule
Did you check that the script receives a complete list of records to process? If that’s the case, then it’s a logic issue of the script.
How to check that? I’m not savvy enough with the Apple script to go beyond applying and modifying examples to suit my needs.
But generally speaking as I assign a new group to “clean” to the Smart Rule I do get a list of records that match the criteria and the script does work for some of those. The remaining are stuck unaffected by the script in any way.
So one possibility is that only some of the files are passed to the script as the Records. If this is the case, why? As far as I understand the Smart Rule applies to all Formatted Notes that have one link in them. All the formatted notes in the list meet that criteria. Most of them are just one paragraph so the script should process them…
I went to check the files that have been processed and deleted (they are still in the Trash) and there’s no visible difference between ones which the script does process and the ones it refuses to touch.
This is unlikely but the script could display an alert showing the number of records.
I’ve added following line to the script, just after the tell:
display alert "Records" message (length of theRecords) as informational
The script has shown me the exact number of items in the list displayed by the SmartRule. In other words, all the items get parsed and passed to the script but for some reason they fail to meet criteria of having less than 2 paragraphs… even though they have just 1 paragraph mostly.
Unless the failure is somewhere else - with the Japanese names etc. Though I do have some English named notes which the script fails to process anyway… but it may be failing on fist note and quitting… but why no error message then?!?
Records skipped due to this line…
if (count paragraphs) of (plain text of theRecord as string) ≤ 2
…don’t cause any errors. Wouldn’t it make more sense to check whether the word count is 0 or why does the script check the number of paragraphs?
This exact line is the problem but for different reason. I finally started to see error messages in the log. Is this how Devonthink handles display alert as warning thing? I expected a popup window.
Anyway, the log window shows following error:
21:25:57: Clean Evernote Imports
on performSmartRule (Can’t get plain text of {content id 66802 of database id 2, content id 68791 of database id 2, content id 66855 of database id 2, content id 67595 of database id 2, content id 67198 of database id 2, content id 67216 of database id 2, content id 67575 of database id 2, content id 67317 of database id 2, content id 67505 of database id 2, content id 67849 of database id 2, content id 69267 of database id 2, content id 68019 of database id 2, content id 69160 of database id 2, content id 67918 of database id 2, content id 69247 of database id 2}.)
So there are 15 items on the list I’m experimenting on and there are 15 items where the script fails to convert the notes to plain text.
The formatted notes are generally a simple html files which show as a single paragraph note with a single link… sometimes there’s an empty line before the link, making it 2 paragraphs.
The notes which the script succeeded to process are exactly the same.
What could be the reason the script cannot turn these notes into plain text?
No, that’s not an alert.
That’s impossible to tell without an example file.
On the Finder side of things files are unlocked and I have read/write access to them. Either way, that shouldn’t cause the problem with plain text conversion.
As for the contents, the first is the file the script fails to process, the second is one it processed successfully:
-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="DT:isEditableNote" content="Yes"/>
<style>
</style>
</head>
<body><div><a href="x-devonthink-item://69206A05-2C0B-429F-B126-BB2B82D71152">ECVL.pdf</a></div></body>
</html>
-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="DT:isEditableNote" content="Yes"/>
<style>
</style>
</head>
<body><a href="x-devonthink-item://E3432D1A-27E0-47DB-BD58-77EB3A83163C">MC-510_1615456-8G.pdf</a></body>
</html>
According to BBEdit both are HTML, UTF-8 with Unix LF. The div tag is the only noticable difference, but it has been generated automatically on import from Evernote and it doesn’t affect how DEVONThink displays these files.
EDIT: I went to check other files… not all of them but after a dozen or so of files - none of the files that were processed successfully had a div tag, while all files the script failed to process had the div tag.
The question is how to fix this? Is there a way to make the script work despite the tag? I’d rather avoid removing the tag with BBEdit or some other batch process because it potentially can mess things up even more.
In the first case the number of paragraphs is actually 3. The conversion to plain text is handled by the WebKit framework of macOS, therefore please don’t ask me why
Much more reliable would be to really count the number of not empty lines:
set numLines to 0
set AppleScript's text item delimiters to ASCII character 10
set theText to plain text of theRecord
repeat with theLine in text items of theText
if length of theLine is greater than 0 then set numLines to numLines + 1
end repeat
if numLines ≤ 1 then
-- insert code here
end if
1 Like
But isn’t the error saying that the script can’t get the records’ plain text? If that is the problem, counting lines in something that can’t be retrieved in the first place does perhaps not help?
Interestingly, the first example in the OP’s post here https://discourse.devontechnologies.com/u/Krzysztof_ZP is converted just fine to a formatted note in DT. The second one not at all – it just gives a mostly empty file.
So apparently (?) the div element in the first document derails the conversion process, given that there are no other visible differences. Maybe a WebKit bug?
Unfortunately the original script is a quick & dirty hack which works only under certain conditions, e.g. assuming that parent 1 is the desired location and not a tag etc.
The conversion of the above example files works fine for me over here in a basic script without the move record commands as I don’t have any Evernote notes to process 

In fact, I had no problem retrieving the plainText from the first example (the one with the div) here either.
So something else is off.
It’s too late over here, so I’ll get back to testing it tomorrow. Thank you for the help.
I’m seeing no issue with the smart rule or its script here, even with the modification to ≤ 2.
I’ve replaced the original if loop with the above and it seems the script is performing OK with all the records right now. Going to do further cleaning later, but hopefully nothing more will pop up.