Append Comment and Tags to RTF in Batch

Hi all,

I constructed the following script to append the comments and tags to RTFs. I use this to prep notes for use in Scrivener (b/c the metadata doesn’t carry over when copied).

It’s a bit of a hack, and could probably be improved.

The problem I’m facing is that I would prefer to add the text as new paragraphs. They currently get appended to current text. I’m not sure how to accomplish that with my limited AppleScript knowledge.

I’m also open to any other ways to make this script more efficient.

Thanks for any feedback, and I hope you find it useful.

-- Append Tags and Comment as Text
-- Created by John Mickey on 2017-02-23
-- Based on a script created by Christian Grunenberg on Aug Tue 16 2011.

tell application id "com.devon-technologies.thinkpro2"
try
set theSelection to the selection
set createRecord to true

repeat with theRecord in theSelection
set theTab to open tab for record theRecord

set theWindow to think window 1
set {od, text item delimiters of AppleScript} to {text item delimiters of AppleScript, ", "}
set theTags to tags of theRecord as string -- tags
set theComment to comment of theRecord as string -- comment
-- set text of theWindow to "" -- Mark as modified
set createRecord to false
if type of theRecord is rtf or type of theRecord is rtfd then
open tab for record theRecord in theWindow
set current tab of theWindow to tab -1 of theWindow

tell text of theWindow
set insertName to true
tell text of tab 1 of theWindow to make new paragraph with data ("Tags {" & theTags & ", Source: " & theComment & "}") at end
end tell

end if
save theWindow
close theWindow
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell


Here’s a slightly revised script which is more efficient and inserts line breaks:


-- Append Tags and Comment as Text
-- Created by John Mickey on 2017-02-23
-- Based on a script created by Christian Grunenberg on Aug Tue 16 2011.

tell application id "DNtp"
	try
		set theSelection to the selection
		set theWindow to missing value
		set {od, text item delimiters of AppleScript} to {text item delimiters of AppleScript, ", "}
		
		repeat with theRecord in theSelection
			if type of theRecord is rtf or type of theRecord is rtfd then
				set theTab to open tab for record theRecord in theWindow
				set theWindow to think window of theTab
				
				set theTags to tags of theRecord as string -- tags
				set theComment to comment of theRecord as string -- comment
				
				set theTab to open tab for record theRecord in theWindow
				set current tab of theWindow to theTab -- Rich text scripting needs a visible document
				tell text of theTab to make new paragraph with data (return & return & "Tags: " & theTags & return & "Source: " & theComment) at end
				save theTab
			end if
		end repeat
		
		set text item delimiters of AppleScript to od
		if theWindow is not missing value then close theWindow
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

Thank you so much!!! :smiley:

Hi there to you both (and all the rest),

Would there be any way of potentially reversing this or does a script exist which would read a certain line in the text for tags and apply them in the tag field? I put my tags in the text of notes as I begin with a larger annotation of an entire document and then break it up into separate files. Each important note within the larger annotation file therefore likely has tags that might be distinct from the larger document. I can obviously put these tags into the tag field for each small single note file when I take them from the initial annotation file, but a script that could recognize the tag line (which is basically just Tags: tag1 tag2 tag3) would be fantastic.

Just thought I’d ask in case it existed or was not too difficult to make.

Thanks!

Possible? Yes but it depends on what you have done.
A line of “tag1 tag 2 tag3” isn’t super useful to a computer. Is this three Tags, two tags (one single, one multiword), or a single multiword Tag?
Is there a prefix like “@Tag1 @Tag2” ?
Is it a single line or one Tag per line?

Remember, computers are very dumb and need to have things explained to them explicitly. Never assume a computer will magically know what you mean, any more than you’d expect it from a new assistant helping you. You need to give it info to help identify what you want it to process.

I’ve actually yet to really do anything. Until now I’ve been entering all of my tags manually and am only now trying to make a comprehensive and somewhat standardized document for each article read so that I can customize my annotations via a few simple snippets, clippings, and scripts (most likely using BBEdit). I had been considering the fantastic annotation scripts available for “one thought, one note” (Frederiko, Korm, etc.) but wanted to use other fields customized to my needs, so figured I’d just simplify it as text entry with copy&paste automation and notes snippets depending on the type of note.

So what I’m saying is that I can mostly customize my approach to whatever might work in terms of tags. What I had envisioned was simply typing a snippet and having a text expansion (Typinator) in which one field was for tags. All of my concept tags have ‘x’ at the end of a single word, so I could separate via spaces or semi-colons or the like. And then I also have tags relating to projects or other things that begin with p and q. I do this so that they’re easier to find via Spotlight and so that my DEVONthink documents don’t have too many Wikilinks inside of them because of using rather common words (at least in my field) for tags.

So when I separate the large document into individual notes (perhaps in Markdown or plain text, but also RTF if necessary) while in DEVONthink, it will likely just be a line near the bottom of the note that says something like:
“Tags: anticolonialismx human_rightsx UNESCOx”
or something along these lines. Again, I can pretty much make it into whatever might work at the moment.

Thanks so much for your reply. Any help you or the rest of the community could provide (even if only to point me in a direction) is very much appreciated!