Script to convert TAG in comments to DT Tags

Hi,

I found a script that nearly fits my needs. Before Tagging with DT was possible, I used another tool to TAG my documents. These TAGs are shown within DT in the comment field. Now I’d like to safe my work and transform these comments in DT Tags. I am not an experienced script programmer. I modified this script and now it extracts exactly one tag out of the comments and converts them into a DT TAG. But this is done only for on item. It should delete the converted tag in the comments and process the next tags in the comments if there are any. The tags in the comments are separated with , and a blank. It looks like this: tag(Computer), tag(KnowHow), tag(Something)
etc. The number of tags is variable.

In my script are the following things missing: the iteration to process all the tags in the comment and the deletion of this old tags in the comments.

This is my script so far:
tell application “DEVONthink Pro”

set theSelection to the selection
repeat with theRecord in theSelection
	-- extract the text between the two asterixes
	set theText to comment of theRecord
	set Start to offset of "tag(" in theText
	set theTags to (texts (Start + 4) thru -1 of theText)
	set Ende to offset of ")" in theTags
	set theTags to (texts 1 thru (Ende - 1) of theTags)
	
	-- split into single tags by semicolon
	set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ";"}
	set theTagList to text items of theTags
	set AppleScript's text item delimiters to od
	--
	set newTags to (parents of theRecord) & theTagList
	set the tags of theRecord to newTags
	
	repeat with theRecord in theSelection
		
	end repeat
end repeat

end tell

Thats because of WYCIWG (=What you code is what you get) :wink:

With this you extract the text between the the first “tag(” and the first “)”. To get all of the tags you could modify this into a repeat loop to work through the whole string – but I would approach it slightly different (see below)

This part does nothing at all in your case because you have only one item from the previous part (and because it looks for a “;” as separator). It is a useless leftover from my original script.

Here is how I would approach a script to your task:

tell application "DEVONthink Pro"
	
	set theSelection to the selection
	repeat with theRecord in theSelection
		
		set theTags to comment of theRecord
		
		-- split into single tags by colon
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ", "}
		set theTagList to text items of theTags
		set AppleScript's text item delimiters to od
		
		-- strip away the "tag(" and the ")"
		repeat with y from 1 to (number of items in theTagList)
			set item y in theTagList to texts 5 thru -2 of (item y in theTagList)
		end repeat
		
		set newTags to (parents of theRecord) & theTagList
		set the tags of theRecord to newTags
		
		set comment of theRecord to ""
		
	end repeat
	
end tell

Note that I added a line to remove the old tags from the comment. Remove the line if you don’t want that.

Johannes

Thanks a lot Johannes,

now it works as supposed.

Kind regards
Reinhard