Script to batch tag multiple items

Hi,
as a lot of people outthere I use DT in conjunction with BE and I imported references from BE to DT as a rtf file for every single reference (exporting the whole library as a formatted bibliography, importing it in DT and then splitting the file into multiple records).
The output of each record in DT is like this:

Now I’m wondering if it’s possible (through scripting) to convert the keywords (between the “*”) into tags.
Ideally the process would be:
-select DT entries
-get the tags for each entry
-apply the tags

Any help appreciated as I’m a dumbscripter :confused:

Less cryptic, please. What’s “BE”?
:wink:

Sorry:
BE stands for Bookends (a reference manager ). Anyhow the point is To be able to get text between asterisks in multiple entries in Dtpro and then subdivide this text using as delimiters the semicolon. Could you help?
Thx

Here is a quick and dirty script for a starter:
Assumptions:

  • each record contains exactly two asterixes with the tags in between separated by semicolons.
  • No hierarchical tags
tell application "DEVONthink Pro"
	
	set theSelection to the selection
	repeat with theRecord in theSelection
		-- extract the text between the two asterixes
		set theText to plain text of theRecord
		set Start to offset of "*" in theText
		set theTags to (texts (Start + 1) 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
		
		-- create a group for ech tag (if necessary) and replicate the file to it
		repeat with theTag in theTagList
			set theDestination to create location "/Tags/" & theTag
			replicate record theRecord to theDestination
		end repeat
	end repeat
	
	
end tell

The only problem I could not solve: The script creates a new group “Tags” at the root level instead of using the native “Tags” group. I tried getting the “tags group” property of the database and use it instead, but no luck. Maybe Christian or some more advanced coders can help with this.

Try and use it on your own risk.

Johannes

Thanks a lot/dankeschön!
I modified a little bit your code trying another approach: just applying tags to records (without creating replicants) and apparently it works: I’ve tried it in a new test database. Maybe there’re some inconsistencies or redundances but as I said I’m only a script-modder :cry:

Thanks a lot/dankeschön!
I modified a little bit your code trying another approach: just applying tags to records (without creating replicants) and apparently it works: I’ve tried it in a new test database.

Here it is:

tell application "DEVONthink Pro"
	
	set theSelection to the selection
	repeat with theRecord in theSelection
		-- extract the text between the two asterixes
		set theText to plain text of theRecord
		set Start to offset of "*" in theText
		set theTags to (texts (Start + 1) 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

Maybe there’re some inconsistencies or redundances but as I said I’m only a script-modder :cry:

It seems I overlooked the obvious :wink:
I took the last part from an other script that dated back to pre-tag releases where the tag property of a record was read only.

Your modification has one issue (beside the empty repeat loop left over from my script):
It changes the unsorted order in the main-location (the items tagged with the script will be at the end after running the script). This would cause trouble for my workflow.

Johannes

Do you mean the unsorted order of Tag group?
Didn’t do intentionally :confused: Anyway the alphabetical sort is what I use

Are you a BookEnds user? This script was intended to have reference keywords from BE in DTPro

Thanks

The order within the Tags group does not bother me. But your script re-applies the parents of the file and so the sort order there is lost. If you don’t use unsorted order, you should be fine.

No. But I had done some string extracting things a few days before so I thought, it shouldn’t be so hard to get that part working and learn about tagging via scipt and its issues :wink:

Johannes

Did this get resolved, is there a working script to Select a bunch of files and then Add a Tag (to all of them)? And is this on the DT feature request list? Would be most useful.
Merci & Thanks.

Ok, found a “batch-tag” script here: Help > Support Assistant - Download Extras - Scripts. - “Add Tags to Selection”.

Works Ok (thanks). But there is no drop-down menu of existing tags, so they need to be typed out individually, and errors can be made. But still useful. Perhaps that function can be added later?

Bear in mind, it is not uncommon to have several hundred Tags. This may not scale very well for a drop down.

Right, but we could use the auto-complete functionality, as presently available for single-item tagging in DT. Quicker, easier, and more accurate (than free-hand).