AppleScript for new document

dear all,
I’m a AppleScript newbie and need some support to create the following Apple Script (AS):

tell application “devonthink 3”
create new rtf document in database “ABC”
insert creation date and time
end tell

as an example: creation date and time should be sth like this: 20210127212910
I know that this script doesn’t work but I also wasn’t able to turn it into a real one. It would be great if you can hep me writing an accurate script.

Thank you in advance
Cid

Have you looked at the Automation chapter in the Help?

You would note there is no command to create an RTF document. You create a record with a particular type. From Help > Documentation > Automation > Basic AppleScript Terminology

If you open DEVONthink’s AppleScript dictionary per that section, you’ll find there’s no insert command either.

The command create record with has a list of parameters that can be specified.


Here you can see there is a content property in the list. This is how you can prepopulate the file with some text before you create it.

If you look at the entry for record you will see the properties (and their types) available for a record.

Here is a simple teaching edition script as an example…

set cd to (current date) -- Put the date in a variable.

tell application id "DNtp" -- Talk to DEVONthink
	tell database "1" -- Talk to the database. This is a way to specify the database by name.
		
		create record with {name:"Test Document", content:(cd as string), type:rtf} in incoming group
		-- Create a record with a name, some content, and the type 'rtf'
		-- The cd variable contains a date object so we convert it into a string to be used as text in the document
		-- 'in incoming group' targets the Inbox of the database
		
	end tell -- Stop talking to the database
end tell -- Stop talking to DEVONthink

Here is the document that is created per the example script…

1 Like

Thank you so much, this is very helpful. As I’m no AppleScript expert, it’s tough to create a script myself. However, it seems to be time to dig deeper into AS. : )
Thanks again
CID

You’re very welcome :slight_smile:

PS: Here’s another version that’s a bit more brief…

set cd to (current date) 

tell application id "DNtp" 
	
	create record with {name:"Test Document", content:(cd as string), type:rtf} in (incoming group of database "1")
	-- Here is a more brief version that just talks to DEVONthink but references the Inbox of the specific database as the destination.
	
end tell

I’m trying to do something similar, but I want the document to be in certain folder (group) , meaning I need to specify the path.

Though I’m wondering why in my script editor I don’t have DEVONthink 3.sdef ?

Thanks (and my apology if I’m missing something very obvious)
Cheers

No worries!

  • Shift-Command-O in Script Editor to get to the Open Dictionary panel. You should see DEVONthink listed there.

  • You want to use location, not path, e,g., with a command like get record at.

Thanks a bunch @BLUEFROG , I must admit i’m not skilled in AppleScript so first bullet point was a new learning for me :slight_smile:

For the second I’ve tried location as well as location group but obviously I’m missing something.

This is part of code I use

set moj_loc to "\/00 Test\/10 Inbox"
...
set moj_record to (create record with {name:theQuery, content:(cd as string), location:(moj_loc), type:rtf})

and my goal is to put the new rtf into the following path, but in ends in general inbox :frowning:

What am I doing wrong ? :roll_eyes:

Thanks
Cheers

Please enclose code in three backticks like so
```
code goes here
```
That highlights it correctly, makes it stand out, and also adds the copy button at the top right that allows people to easily copy/paste your code. I’ve modified your post accordingly.

To your questions…
set moj_loc to "\/00 Test\/10 Inbox" the backslashes do nothing there but obfuscate the code. Slashes in a string are nothing special that would merit escaping with a backslash.
create record with {name:theQuery, content:(cd as string), location:(moj_loc), type:rtf})
Now, the documentation of create record with states:

create record with v : Create a new record.
create record with [record]: The properties of the record (‘name’, ‘type’, ‘comment’, ‘aliases’, ‘path’, ‘URL’, ‘creation date’, ‘modification date’, ‘date’, ‘plain text’, ‘rich text’, ‘source’, ‘data’, ‘content’, ‘columns’, ‘cells’, ‘thumbnail’ and ‘tags’ are possible values).
in [record]: The destination group for the new record. Uses incoming group or group selector if not specified.

location, as in your code, is not mentioned. I guess what you want to achieve is to store your record in the group /00 Test/10 Inbox (the choice of Inbox for a group is a bit unfortunate, as Inbox is a default group in every database). Then you should use the in parameter for that. Something along these lines, perhaps (I’m not an AppleScript person, so the code might not quite work as it is):

set moj_loc to create location "/00 Test/10 Inbox"
set moj_record to create record with {name:theQuery, content:(cd as string), type:rtf} in moj_loc

I also removed the unneeded parenthesis.

If you feel uncomfortable with AppleScript, you could achieve the same result with JavaScript, e.g.

const app = Application("DEVONthink 3");
…
const moj_loc = app.createLocation("/00 Test/10 Inbox");
const moj_rec = app.createRecordWith({name: theQuery, content: cd, type: "rtf"}, {in: moj_loc});

(not too many differences there, though).

Hi @chrillek ,

first thank you, thank you, thank you ! :smiley_cat:

Second my apology for lack of hygiene on code block, and yes Inbox is a bit unfortunate, but this was just a playground. My point was with one click to create + open rtf document is specific location which is not “system” inbox.

Third this is the functioning code thanks to Bluefrog and Chrillek , I run it via Alfred:

on run argv
  set theQuery to item 1 of argv

	set cd to (current date)

	tell application id "DNtp"
	tell database "Some_DB"
		
		set moj_loc to (create location "/00 Test/10 Inbox")
		set moj_record to (create record with {name:theQuery, content:(cd as string), type:rtf} in moj_loc)	
		open window for record moj_record with force
		
	end tell
	end tell
  return theQuery
end run

p.s. it’s pleasure to be part of this community

Nice and glad to have you here :slight_smile:

Why do you wrap create location and create record in a tell database … ? Neither these nor open window for are related to a database.

The database class doesn’t provide any commands at all.

tbh I don’t know … I took this piece of code from this forum in the past and now I modified a bit to fit my needs.

However, I tried few things till I made it work.
Don’t take my word for it, but I think if set moj_loc to (create location "/00 Test/10 Inbox") is outside of tell database then it was failing … I think, not 100% certain.

There’s nothing wrong with tell database as you need to create the location somewhere. You could tell the database or use e.g., create location "/Inbox/Test Group" in database "My Data"..

1 Like

Interesting. Until now, I thought that commands are associated with certain classes. And “create location” is, according to the scripting dictionary, associated with “Application”. OTOH, the class database doesn’t provide any commands.

Now, it seems that tell database "bar" to create location "foo" is equivalent to tell application "DEVONthink 3" to create location "foo" in "bar". Is that documented somewhere, or is it a kind of syntactic sugar provided behind the scenes in DT?

If so, would the same apply for, for example
tell record to addCustomMetaData "foo" for "bar"
being equivalent to
tell Application "DEVONthink 3" to addCustomMetaData "foo" for "bar" to record

Aside: Application.databases["foo"].createLocation("/INBOX") (the JXA equivalent of tell database … throws an error in Script Editor -2700 “Named parameters must be passed as an object”.

This works only if the desired database happens to be the current database as the current database is used if the in parameter is not specified. Databases don’t support any commands on their own.

1 Like

While it may not be the most common approach to talk to the database, the it reference can be used…

tell application id "DNtp"
	tell database "HR Data"
		create location "/From a Script" in it
	end tell
end tell

That looks very much like a longer version of

set db to database "foo"
create location "/bar" in db

In my opinion, using tell database obfuscates the fact that database doesn’t provide any commands.

As I said, it’s not the most common approach but it’s still a possible option. And it shows you can talk to other items people may not think of.