How to change which database is "active" in a script

I am using an Applescript posted on a previous message board (Scripts for a Tickler File) related to using GTD in DevonThink. I have attached a copy of the script at the bottom of this message (as I have made some small changes for my setup).

I have a quick question that I am sure has an easy answer. However, I am not very familiar with Applescripts, so I cannot figure out the appropriate change.

The script moves the contents from one folder (designated by the date in a nested structure) to another (called “/* Next Actions”).

I typically have two databases open (one called GTD and the other called Research). I want the script to first “activate” the GTD database before executing the script, in case the database I used last is Research. If I have Research as the “active” database, then it executes the script for that database, and I don’t get the desired result. So can you tell me the command that “activates” the GTD database before executing the set of commands that move contents?

Thanks so much.

property FOLDERS_MONTHS : {"01 January", "02 February", "03 March", "04 April", "05 May", "06 June", "07 July", "08 August", "09 September", "10 October", "11 November", "12 December"}
property FOLDERS_DAYS : {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}

tell application "DEVONthink Pro"
activate
set todayFolder to create location "/* Next Actions"
set ticklerFolder to "/** Tickler"
set currentMonth to item (month of (current date)) of FOLDERS_MONTHS
set currentDay to item (day of (current date)) of FOLDERS_DAYS
set currentFolder to create location (ticklerFolder & "/" & currentMonth & "/" & currentDay)
set theChildren to the children of currentFolder
repeat with theRecord in theChildren
move record theRecord to todayFolder
end repeat
end tell

You could enclose your actions in a tell block


tell database "GTD"
[do a bunch of stuff]
end tell

Or by reference


set todayFolder to create location "/* Next Actions" in database "GTD"

I suspect the tell block will be the best approach. In either case, to be sure the database is open use


set myDB to open database "~/Documents/My Databases/GTD"

Change the path to whatever is appropriate for your configuration.

Korm,
Thanks for your reply. I apologize but I am having some problems with the suggestion you made. First, when I add the tell database line, I get an error when my GTD database is active:
error “DEVONthink Pro got an error: Can’t get parent id 986 of database id 2.” number -1728 from parent id 986 of database id 2

Second, when my other database is active, the same behavior occurs as before: the folders are created in that database instead and GTD is never ‘activated’ (so the tell database “GTD” line does not seem to be working for me).

And third, I loved the idea of adding the code to make sure the database is open:


set myDB to open database "~/Documents/DevontThink/GTD"

But when I add this code, I get an error: Expected end of line, etc. but found “"”.

I hope you’re able to provide some feedback as to what I could be doing wrong. Thanks very much, again, for your help.

Every sample command I suggested works over here (adjusted for my own names), so without seeing your entire script (the one that fails) it’s hard to diagnose. Though, I see that the code sample you posted was misspelled. Is your database actually named “GTD”? Do you know the file system path to that database?

Korm, I misspelled it in my post, good catch. I corrected it, but still it won’t compile. The database is called GTD and the path is ~/Documents/DevonThink/GTD.

Below is the code I am trying to run:

property FOLDERS_MONTHS : {"01 January", "02 February", "03 March", "04 April", "05 May", "06 June", "07 July", "08 August", "09 September", "10 October", "11 November", "12 December"}
property FOLDERS_DAYS : {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}

set myDB to open database "~/Documents/DevonThink/GTD"

tell application "DEVONthink Pro"
	activate
	tell database "GTD"
		activate
		set todayFolder to create location "/* Next Actions"
		set ticklerFolder to "/** Tickler"
		set currentMonth to item (month of (current date)) of FOLDERS_MONTHS
		set currentDay to item (day of (current date)) of FOLDERS_DAYS
		set currentFolder to create location (ticklerFolder & "/" & currentMonth & "/" & currentDay)
		set theChildren to the children of currentFolder
		repeat with theRecord in theChildren
			move record theRecord to todayFolder
		end repeat
	end tell
end tell

So, first I figured out that the script runs when there are no files in the appropriate tickler folder (/06 June/07). But when there are files, the error comes on the “move record theRecord to todayFolder”.

And, second, I figure out that “set myDB to open database” compiles, but once I add anything after that, it fails to compile.

set myDB to open database "~/Documents/DevonThink/GTD"
tell application "DEVONthink Pro"

should be

tell application "DEVONthink Pro"
set myDB to open database "~/Documents/DevonThink/GTD"

Why? Because “open database” is a command in DEVONthink’s dictionary, and if you’re not inside the “tell DEVONthink” structure then the AppleScript parser has no idea what you mean.

Once you’ve moved that command, then change


tell database "GTD"

to


tell database myDB