# Creating new file from template folder by AppleScript

**URL:** <https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279>\
**Category:** Automation\
**Created:** [July 15, 2020, 10:08am UTC](https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279 "2020-07-15T10:08:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngan](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@ngan](https://discourse.devontechnologies.com/u/ngan)\
**Post date:** [July 15, 2020, 10:08am UTC](https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279/1 "2020-07-15T10:08:12Z")

</div>

I am writing a handler as a part of my script to create a new file from a few frequently used templates (no template scripts within). I am not familiar with scripting Finder, but this handler does what it is expected for a one-level folder.

EDITED. This method of extraction can not handle a nested top folder nor to distinguish file vs folder. I wonder how to code for the extraction of the templates for a nested top folder?

Thanks

```auto
set thePath to "/Users/username/Library/Application Support/DEVONthink 3/Templates.noindex/Favorites"
tell application "System Events"
	set ll to {name, POSIX path} of every disk item of folder thePath
end tell

tell application id "DNtp"
	-- I am simplifying the preparation of the list here
	set theTemplate to choose from list ll's item 2
	set newDoc to import template (theTemplate as string) to incoming group of current database
end tell

```

---

<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:** [July 15, 2020, 3:40pm UTC](https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279/2 "2020-07-15T15:40:28Z")

</div>

You could write a recursive handler to process subfolders.

---

<div class="post-metadata">

**Author:** ![ngan](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@ngan](https://discourse.devontechnologies.com/u/ngan)\
**Post date:** [July 15, 2020, 4:20pm UTC](https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279/3 "2020-07-15T16:20:40Z")

</div>

Thanks. I’ll continue to search for more info on the web, too.

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [July 15, 2020, 4:35pm UTC](https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279/4 "2020-07-15T16:35:44Z")

</div>

> [@ngan](#):
>
> more info on the web

Take a look at this before you start [MacScripter / System Events and Alias](https://macscripter.net/viewtopic.php?pid=199787)

---

<div class="post-metadata">

**Author:** ![ngan](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@ngan](https://discourse.devontechnologies.com/u/ngan)\
**Post date:** [July 15, 2020, 4:47pm UTC](https://discourse.devontechnologies.com/t/creating-new-file-from-template-folder-by-applescript/57279/5 "2020-07-15T16:47:52Z")

</div>

Thanks!

This morning I was looking at [this MacScripter post](https://www.macscripter.net/viewtopic.php?id=46815) that is focused on using application “Finder” to perform the task. But it seems using “System Events” is more efficient for a simple task. Your suggested post is perhaps more relevant…
