Import record to path in specific database

first off, I’m using DTPO pb8 and i’m not very good at applescript.

that said, I’ve been mucking around with the bundled template “Project” to create a simple skeleton project for a todo database (i’m replacing Things with DTPO).

anyhoo, i want this script to place the imported structure into a specific database, and into a static path inside this DB. specifically, the DB is called “To-Do List” (filename “To-Do_List.dtBase2”). the imported structure should always be imported to the group “Active”, which is nested inside a group “Projects”, which is at the DB root ("/Projects/Active"). i’ve been trying to modify the relevant lines in the template script to do this, but can’t seem to get it to work. here’s the official code:

set theRecord to import theTemplateFiles placeholders thePlaceholders to current group
		set the name of theRecord to theProjectName

i’ve tried replacing the “current group” keyword with every variation of the static path i can think of, but none seem to work. can anyone help?

1 Like

The “to” parameter expects a reference to a group, not a path. Therefore you could use something like this:

set theDatabase to open database "<insert POSIX path to your database here>"
set theGroup to create location "/Projects/Active" in theDatabase
set theRecord to import theTemplateFiles placeholders thePlaceholders to theGroup

But adding the group to the Favorites of the sidebar to easily select the group and import the template afterwards is a more generic and useful approach too.

Christian, you’re the man. Works like a charm!!

Hi, I need the exact same feature, i tried to adjust according to my data base info but I still can’t figure out where to put your code in the actual default import script (that comes with the application). Here is my code, could you please provide me with the complete code, not just the part to add ? Thanks already !!

– Action Import.applescript
– Created by Christian Grunenberg on Tue Dec 03 2002.
– Copyright © 2002-2009. All rights reserved.

on adding folder items to this_folder after receiving added_items
tell application id “com.devon-technologies.thinkpro2” to launch
repeat with theItem in added_items
set thePath to theItem as text
if thePath does not end with “.download:” then
tell application id “com.devon-technologies.thinkpro2” to import thePath to incoming group
end if
end try
end repeat
end try
end adding folder items to

since i’m not great at applescript, and i’m not 100% sure of your environment, i think the easiest thing to do is to post what i have so you can see what i did.

first off, i started with the bundled template “Project”. It has a file/folder structure that it uses to populate a new project in DTPO. i made a copy, and modified the structure to include only 2 files in the provided structure: the ‘about this project’ RTF file, and an empty task RTF (that i modified). i wanted to fire this template off and have a pre-populated structure get dropped into a dedicated group, and replace various placeholders to set text within the 2 RTF files.

the template script, main.scpt, was what i modified to be this:

-- Smart template adding a localized project template to the current group
-- Written by Eric Böhnisch-Volkmann, modified by Christian Grunenberg
-- © 2009 DEVONtechnologies, LLC

-- Default non-localized project name, also used to identify the resources
property pTemplateName : "New Project"
property pInsertNameHere : "First Last"
property pInsertEmailHere : ""
property pInsertDeptHere : "My Department"
property pInsertPhoneHere : "(800) 555-2368"
property pAboutDocument : "About this project"
property pBlackColor : {0, 0, 0} -- RTF text color

-- Import helper library
tell application "Finder" to set pathToAdditions to ((path to application id "com.devon-technologies.thinkpro2" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions

set startDate to do shell script "date '+%F'"

	-- We're later working in DEVONthink, we need to cache localized strings while still in our realm
	set theProjectName to localized string pTemplateName
	set theUserName to my helperLibrary's localizedString(pInsertNameHere)
	set theUserEmail to my helperLibrary's localizedString(pInsertEmailHere)
	set theUserDept to my helperLibrary's localizedString(pInsertDeptHere)
	set theUserPhone to my helperLibrary's localizedString(pInsertPhoneHere)
	tell application id "com.devon-technologies.thinkpro2"
		-- Let the user change the project name
		set theProjectName to display name editor my helperLibrary's localizedString(pTemplateName) default answer theProjectName info (my helperLibrary's localizedString("Please enter a name for this project") & ":") as string
		set thePlaceholders to {|%email%|:{|URL|:"mailto:" & theUserEmail, |name|:theUserEmail}, |%dept%|:theUserDept, |%phone%|:theUserPhone, |%project%|:theProjectName, |%start%|:startDate}
		-- Import the predefined structure
		set theDatabase to open database "/Users/user/Library/Application Support/DEVONthink Pro 2/Databases/To-Do_List.dtBase2"
		set theGroup to create location "/Projects/Active" in theDatabase
		set theTemplateFiles to helperLibrary's pathToLocalizedResources() & my helperLibrary's localizedString(pTemplateName)
		set theRecord to import theTemplateFiles placeholders thePlaceholders to theGroup
		set the name of theRecord to theProjectName -- Rename to user-defined project name
	end tell
on error errMsg number errNum
	if errNum ≠ -128 then display alert (localized string "An error occured when creating the new project structure") message errMsg as warning
end try

the relevant bits are the shell script to get today’s date in a usable format (which is tough using applescript), and the ‘tell’ block within the ‘try’ block. this sets the necessary database to the DB i want to always use (“To-Do List.dtBase2”) by setting the filesystem path. it then populates a variable to contain the path within the DB to the required group tree ("/Projects/Active"). then it imports the structure, replacing placeholders with the localized variable set at the beginning, and renames the new project (which is a group) to the project name i provide in the prompt.

the parts that got me were setting the DB path (which works whether the DB is open or not), and the group path, which doesn’t create a new path, but uses what is there already (if it exists).