# AppleScript for new document

**URL:** <https://discourse.devontechnologies.com/t/applescript-for-new-document/61284>\
**Category:** DEVONthink\
**Created:** [January 27, 2021, 8:30pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284 "2021-01-27T20:30:43Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![cid](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@cid](https://discourse.devontechnologies.com/u/cid)\
**Post date:** [January 27, 2021, 8:30pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/1 "2021-01-27T20:30:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [January 28, 2021, 5:02am UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/2 "2021-01-28T05:02:47Z")

</div>

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** …

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/4/7/473cdf6ab0b3a8e34c0ca948f94b7e200ed5baf5.jpeg)

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.

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/b/6/b6a2dc948826ac4bc47a1308fbbb6e3e2513da7c.jpeg)  
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.

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/4/4/44fed31f69cd97d3a3f9e7890547a02ae6f236b4.jpeg)

Here is a simple _teaching edition_ script as an example…

```auto
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…

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/b/5/b5fca2e7269828678b138798341259a2b4de8235.jpeg)

---

<div class="post-metadata">

**Author:** ![cid](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@cid](https://discourse.devontechnologies.com/u/cid)\
**Post date:** [January 28, 2021, 3:08pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/3 "2021-01-28T15:08:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [January 28, 2021, 3:13pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/4 "2021-01-28T15:13:13Z")

</div>

You’re very welcome 🙂

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Abdt](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@Abdt](https://discourse.devontechnologies.com/u/Abdt)\
**Post date:** [February 3, 2024, 8:05pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/6 "2024-02-03T20:05:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [February 3, 2024, 11:20pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/7 "2024-02-03T23:20:14Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Abdt](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@Abdt](https://discourse.devontechnologies.com/u/Abdt)\
**Post date:** [February 4, 2024, 12:46am UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/8 "2024-02-04T00:46:57Z")

</div>

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

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

```auto
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 ☹

 ![image](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/f/f/ff0a7ab78e3e9128c997770764a9f17bebde3794.png)

What am I doing wrong ? 🙄

Thanks  
Cheers

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 4, 2024, 9:08am UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/9 "2024-02-04T09:08:04Z")

</div>

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):

```auto
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.

```javascript
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).

---

<div class="post-metadata">

**Author:** ![Abdt](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@Abdt](https://discourse.devontechnologies.com/u/Abdt)\
**Post date:** [February 4, 2024, 4:06pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/10 "2024-02-04T16:06:33Z")

</div>

Hi @chrillek ,

first thank you, thank you, thank you ! 😺

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [February 4, 2024, 4:08pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/11 "2024-02-04T16:08:30Z")

</div>

Nice and glad to have you here 🙂

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 4, 2024, 4:17pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/12 "2024-02-04T16:17:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Abdt](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@Abdt](https://discourse.devontechnologies.com/u/Abdt)\
**Post date:** [February 4, 2024, 8:04pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/13 "2024-02-04T20:04:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [February 4, 2024, 9:37pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/14 "2024-02-04T21:37:57Z")

</div>

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".`.

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 5, 2024, 3:07pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/15 "2024-02-05T15:07:43Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![cgrunenberg](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/cgrunenberg/32/7172_2.png) [@cgrunenberg](https://discourse.devontechnologies.com/u/cgrunenberg)\
**Post date:** [February 6, 2024, 8:26am UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/16 "2024-02-06T08:26:11Z")

</div>

> [@BLUEFROG](#):
>
> 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".`.

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.

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [February 6, 2024, 2:45pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/17 "2024-02-06T14:45:48Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [February 6, 2024, 2:53pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/18 "2024-02-06T14:53:05Z")

</div>

That looks very much like a longer version of

```auto
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.

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [February 6, 2024, 3:02pm UTC](https://discourse.devontechnologies.com/t/applescript-for-new-document/61284/19 "2024-02-06T15:02:39Z")

</div>

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.
