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:
- Appelscript runs the index update etc as per above script
- Applescript launches an independent background shell script to restart DT after a set delay
- 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?