Creating a document in a specific group

Hi,

I’m trying to recreate and adapt the ‘Create Table of Contents’ feature in Markdown (and adding some boilerplate text of my own). I’ve got almost everything working as I want it, but there’s one small detail I’m stuck on.

I would like the new document to be created in an existing group called ‘Journal’ which is at the top level of the database ‘Writing’. So far, all I’ve managed to do is to create it in the root of Writing instead – not in ‘Journal’. I’ve spent some time on the forum looking at relevant replies, but so far I’ve not managed to get it to work properly.

The relevant code I’m trying :

set theDatabase to open database "/Users/david/Documents/Databases/DevonTHINK/Databases/Writing.dtBase2"
		
set theLocation to create location "/Journal" in theDatabase
		
set newJournal to create record with {name:theName, source:dataString, type:markdown} in theLocation

I’ve tried various other combinations, but nothing seems to work and it’s clear I don’t really understand what I’m doing… What am I doing wrong, please?

Please post the entire script. Thanks.
PS: I’m not seeing any issues with this from the snippet you posted.

Here’s the full script.

As mentioned, everything works as expected apart from the creation of the new document in the right group. (BTW the script is part of a Keyboard Maestro workflow which selects the built-in Today Smartgroup, does a cmd-A, then calls the script.)

-- Adapted from script which exports selected DEVONthink records to clipboard for pasting into Tinderbox
-- http://www.eastgate.com/Tinderbox/forum//YaBB.pl?num=1346083940/7#7
-- https://discourse.devontechnologies.com/t/exporting-limited-field-data-for-selected-items/14511/1
-- Thanks to MWRA and Korm for the originals
-- Aim: create a Markdown version of 'Create Table of Contents' in a Journal template in Writing/Journal.


set {year:yr, month:mn, day:dy} to (current date)
set dateString to (yr as text) & pad(mn as integer) & pad(dy)


set theDate to dateString
set theNumber to 1
set theName to theDate & " JXX Daily Journal " & return
set dataString to "# " & theName & "## Documents worked on" & return & return

tell application id "com.devon-technologies.thinkpro2"
	set itemList to selection
	if itemList is {} then
		tell application "System Events" to display dialog "Oops! ... First select some DEVONthink records and try again." buttons "OK" default button "OK"
	else
		repeat with anItem in itemList
	
			set aText to plain text of anItem
			
			--replace any straight quote with curly/smart, and surround with straight double quotes:
			set quotifiedText to quote & my quotify(aText) & quote
			
			set dataString to dataString & theNumber & ". [" & (name of anItem) & "](" & (reference URL of anItem) & ")" & return
			
			set theNumber to theNumber + 1
			
		end repeat
		
		set dataString to dataString & return & "## Commentary" & return & return
		
		set theDatabase to open database "/Users/david/Documents/Databases/DevonTHINK/Databases/Writing.dtBase2"
		
		set theLocation to create location "/Journal" in theDatabase
		
		set newJournal to create record with {name:theName, source:dataString, type:markdown} in theLocation
		set tags of newJournal to "JXX Journal"
		set label of newJournal to 3
		
	end if
end tell

on quotify(txt)
	-- replace straight quotes with curly/smart versions
	--"quoted form" of mwra quotify() <http://www.eastgate.com/Tinderbox/forum//YaBB.pl?num=1265006946/0#4>
	set scrptCom to "sed 's:\"\\([^\"]*\\)\":“\\1”:g' | sed \"s:'\\([d|ll|re|s|t|ve]\\):’\\1:g\" | sed \"s:'\\([^']*\\)':‘\\1’:g\""
	return do shell script ("echo " & quoted form of txt & " | " & scrptCom)
end quotify

return dataString -- (optional) display in AppleScript Editor Result pane

on pad(v)
	return text -2 thru -1 of ((v + 100) as text)
end pad

Thanks for your help, Jim. And thanks to Korm and mwra whose script I murdered to get the far…

Your script works exactly as you described it should. The TOC is created in /Journal, not anywhere else.

I did nothing to your script except comment out the .dtBase2 path (since it’s not mine) and point to the current database instead of theDatabase. Otherwise, the first time I ran your script /Journal was created and then, and subsequent times, the TOC was placed in that group. Be sure to select some documents before running the script

You say you are running the script from Keyboard Maestro – which probably means there is an error in your macro, not an error in the script. It is best practice to get a fully-operational script in place before putting that script into KM. This would tell you, as in this case, that it probably the KM macro that is broken since the script itself is working. Trying to debug a macro and a script at the same time is difficult and sometimes impossible.

BTW – if all you are doing with KM is assigning a short cut to the run the script, you can do that already in DEVONthink without recourse to KM.

-- Adapted from script which exports selected DEVONthink records to clipboard for pasting into Tinderbox
-- http://www.eastgate.com/Tinderbox/forum//YaBB.pl?num=1346083940/7#7
-- https://discourse.devontechnologies.com/t/exporting-limited-field-data-for-selected-items/14511/1
-- Thanks to MWRA and Korm for the originals
-- Aim: create a Markdown version of 'Create Table of Contents' in a Journal template in Writing/Journal.


set {year:yr, month:mn, day:dy} to (current date)
set dateString to (yr as text) & pad(mn as integer) & pad(dy)


set theDate to dateString
set theNumber to 1
set theName to theDate & " JXX Daily Journal " & return
set dataString to "# " & theName & "## Documents worked on" & return & return

tell application id "com.devon-technologies.thinkpro2"
	set itemList to selection
	if itemList is {} then
		tell application "System Events" to display dialog "Oops! ... First select some DEVONthink records and try again." buttons "OK" default button "OK"
	else
		repeat with anItem in itemList
			
			set aText to plain text of anItem
			
			--replace any straight quote with curly/smart, and surround with straight double quotes:
			set quotifiedText to quote & my quotify(aText) & quote
			
			set dataString to dataString & theNumber & ". [" & (name of anItem) & "](" & (reference URL of anItem) & ")" & return
			
			set theNumber to theNumber + 1
			
		end repeat
		
		set dataString to dataString & return & "## Commentary" & return & return
		
		-- set theDatabase to open database "/Users/david/Documents/Databases/DevonTHINK/Databases/Writing.dtBase2"
		
		set theLocation to create location "/Journal" in the current database
		
		set newJournal to create record with {name:theName, source:dataString, type:markdown} in theLocation
		set tags of newJournal to "JXX Journal"
		set label of newJournal to 3
		
	end if
end tell

on quotify(txt)
	-- replace straight quotes with curly/smart versions
	--"quoted form" of mwra quotify() <http://www.eastgate.com/Tinderbox/forum//YaBB.pl?num=1265006946/0#4>
	set scrptCom to "sed 's:\"\\([^\"]*\\)\":“\\1”:g' | sed \"s:'\\([d|ll|re|s|t|ve]\\):’\\1:g\" | sed \"s:'\\([^']*\\)':‘\\1’:g\""
	return do shell script ("echo " & quoted form of txt & " | " & scrptCom)
end quotify

return dataString -- (optional) display in AppleScript Editor Result pane

on pad(v)
	return text -2 thru -1 of ((v + 100) as text)
end pad

FWIW, I put the version of the script I put in my posting above, into a simple KM macro. It works perfectly. (This macro is part of a palette of DEVONthink macros, so it is invoked from that palette by pressing a number key.)

Hi Korm,

Thanks for your reply.

I mentioned Keyboard Maestro simply to provide context. All the macro does is 1) call a named Workplace( (the default Today smart group which covers all open databases); 2) select all the documents; 3) call the script. (PS I’ve just seen your post above: I use exactly the same template technique for my DTPO scripts and annotation – I think I may actually have learnt it from Greg Jones or you on this forum a couple of years ago…)

If I do steps 1 and 2 manually in DTPO and then call the script from the script menu, it has exactly the same effect.

I was going to say that the effect is still that the script doesn’t work as expected: the record is created in the root level of the database.

But then I started doing some more testing and in fact that’s only partially true. The script will sometimes work properly, but only if the database has the ‘Exclude Groups From Tagging’ set to true. If that’s on, the record is created in /Journal. If it’s off, it’s created in the top level. This is with your suggested amendment to use the current database.

Here’s a screenshot of the Today smartgroup (sorted latest first), after I created several new journals – using your ‘current database’ amendment – from a variety of my open databases.

As you can see from the location column, it sometimes works and it sometimes doesn’t: those that work have Exclude Groups from Tagging on.

If you look at the top three entries, they’re all invoked from the Sources database, but only the first and third work properly – the difference is that I toggled Exclude Groups from Tagging off for the second, then back on for the third.

First of all, would it be possible for you to check whether this happens for you?

If it does, is this expected behaviour and how do I get round it?

Many thanks for your help.

Good catch. Yes, when the database is enabled for groups-as-tags (Exclude Groups from Tagging == false) then the script places the TOC in the root and not in /Journal.

I definitely think there is a DEVONthink bug, not a script bug.

The only (inane) workaround I can think of at this time is to add code to the script to toggle the exclude from tagging property for theLocation before and then after you save to that location. Or to permanently disable that group from tagging when you create it or (manually) after you first create it.

Thanks Korm – I’m glad you could recreate it!

I’ll report the issue to support.

It’s an issue of the script. Instead of…


set tags of newJournal to "JXX Journal"

…use this snippet:


set theTags to tags of newJournal
set theTags to theTags & "JXX Journal"
set tags of newJournal to theTags

Thanks, Christian.

I thought because it was a new document and therefore they were no existing tags, I could set them from scratch. I’ll amend the script.

Thanks.

As groups aren’t excluded from tagging and the new document is created in a group, it actually has at least one tag.

I see – that makes sense now. Thanks very much.