Quitting and restarting DT with a script

I was reminded again of my poor understanding of DT-specific Applescripts. Here is the background to my question:

I am using DT/DTTG as my main tool to sync information between iOS and OS X devices. This works really well for me. Periodically I sync to DTTG. Before doing that, I make sure that all indexed folders are updated and that all “in-DB” files residing in an indexed folder are de-consolidated. In other words, the moment I start the DTTG sync, I want DT on the Mac absolutely up-to-date. I posted a script that does this in one swoop (based on Christian’s original script):

[url]Updating indexed groups including deconsolidation]

This worked very well, until I added an iPhone to the mix. Previously I only had an iPad, and DTTG could always see my Mac on the local network. Once I added the second iOS device, Bonjour frequently fails. For example, when I sync the iPad, the iPhone will afterwards no longer see the Mac on the network and vice versa (not always, but very often). Quitting and restarting DT on the Mac fixes this problem. So before I start a sync, I run my “update indexed folders and de-consolidate” script, then I quit DT and then restart it.

I figured that it would be easy enough to turn this into a single script that could run while I do something else (EDIT: in fact, no need to do anything else; without the restart in the script, I would run the index script, then pick up my iOS device, work my way into the sync panel, then notice that the Mac does not show up, put down the device, go back to DT and do the restart thing - quite frustrating; now I just run the script shown below, then pick up the iPad, and by the time I made my way to the sync panel, the restart is already accomplished). So I added to the above mentioned script the following:


tell application id "DNtp" to quit
delay 5.0
tell application id "DNtp" to activate

When I run this script from the DT script menu, it executes to the point of quitting DT, but never restarts it. If I run it standalone, it quits and re-starts, but never carries out the DT-internal index update. I guess a DT-script-menu-type Applescript terminates when DT quits, so it won’t initiate the restart. Is there any clever way around this?

I came up with the following kludge:

  1. Appelscript runs the index update etc as per above script
  2. Applescript launches an independent background shell script to restart DT after a set delay
  3. Applescript quits DT

Here is the code:


-- Update indexed items of all databases
-- Created by Christian Grunenberg on Tue Mar 25 2014.
-- Copyright (c) 2014. All rights reserved.

-- Added automatic deconsolidation of internal items that reside in indexed folders. Only use this if you want ALL items inside indexed folders to be indexed.
-- gg378 , 26-Sep-2014

-- Set to true if indexed items are not only located at the root of the database
property pAllRecords : true

tell application id "DNtp"
	try
		set theDatabases to databases
		show progress indicator "Updating Indexed Items" steps (count of theDatabases)
		repeat with theDatabase in theDatabases
			step progress indicator (name of theDatabase as string)
			set theRecords to records of theDatabase -- Children of root
			repeat with theRecord in theRecords
				if pAllRecords or theRecord is indexed then
					synchronize record theRecord -- Updates children of theRecord too
					deconsolidate record theRecord -- Export internal files in indexed folders
				end if
			end repeat
		end repeat
		hide progress indicator
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

-- index is updated and items de-consolidated
-- now we start a background shell script that will restart DT after a delay
-- then we quit DT

do shell script "(sleep 6; open -a 'Devonthink Pro') > /dev/null 2>&1 & "

-- less elegant way with calling an actual shell script
--do shell script "'/Users/gerald/GGWork/Programs/Devon - my scripts/restartDT.sh' > /dev/null 2>&1 &"


tell application id "DNtp" to quit


Any better ideas? Or even better: Is there a way to re-initialize the Bonjour mechanism without having to cycle DT?

Don’t tell DEVONthink to quit or activate itself. Use system events scripting to quit and activate an application. Check macscripters.net for several examples.

For the quitting I could do:


tell application "System Events" to tell process "DNtp" to keystroke "q" using {command down}

Is there any deeper reason not to ask the application directly to quit? I’ve done this in many scripts (DT and other applications) and never seemed to have issues.

For restarting the program, I’m even more curious: If one tells a program to activate and it is not open, the system opens it and I don’t see how this would be in any way different from, say, clicking on the icon in the dock. But admittedly I don’t have any deeper insights. In any case, even after using System Events to quit DT, the Applescript, if run via DT script menu, is done. So I still seem to need the weird detour via the delayed background shell script.

I’ve always found System Events more reliable for this.

Butting in here for a moment, (partially because of my disdain for UI Scripting on account of all the bruises and the permanently flat spot on my forehead :mrgreen: )


quit application id "DNtp"