Help with Applescript - Finder Reveal

Hi

I was hoping you could help with a really simple Applescript. It finds an indexed file in the DevonThink database and figures out its corresponding remote file, then reveals that in the finder.

This works brilliantly when run from Script Debugger. The finder window opens and reveals the corresponding location of the currently selected DevonThink document.

However, if I run this in the DevonThink Toolbar, I get no errors on the error console, but no finder window opens. I’m wondering if it’s somehow a permissions issue? Normally I’d get a permissions request, though - asking permission for DT to control the other app:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property devonThinkPath : "/Volumes/DevonThink/OneDrive/"
property oneDrivePath : "/Volumes/Data2/OneDrive/Documents/"
tell application "DEVONthink 3"
	set theSelection to the selection
	set firstRecord to item 1 of theSelection
	set thePath to path of firstRecord
end tell

set oneDrivePath to findAndReplaceInText(thePath, devonThinkPath, oneDrivePath)
set oneDrivePosixPath to POSIX file oneDrivePath
tell application "Finder" to reveal oneDrivePosixPath

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

Are you putting your DEVONthink databases in a OneDrive folder? I don’t think so, but have to ask.

I am curious why you’re approaching the reveal with the find and replace handler.

Hey,

Good question… The OneDrive thing isn’t related to this (I probably should have provided a simpler proof of concept that doesn’t do the find and replace). But I’d been meaning to do a longer post re: OneDrive, so will mention here.

Basically - all my work happens on a shared OneDrive, with many gigabytes of data. Nobody else uses DevonThink (yet), and many people edit these files.

I need to stay in sync, but indexed folders are often seen as problematic (just based on other forum posts), and it’s easy to accidentally delete indexed data (e.g. confusion if you delete the root of a collection of indexed files and folders, versus if you delete a child folder… One asks you if you want to delete the files too, the other just deletes them).

So - my solution is to let OneDrive have a complete, synced folder on my disk. It’s not selective sync, so it keeps the whole lot in sync and everything works well.

I then use Chronosync, to two-directional sync to a different folder. This is the folder that DevonThink indexes.

In Chronosync, I’m able to add exceptions so that filetypes and folders can be selectively synced. There’s no easy way to do that in DevonThink, but Chronosync handles this.

I can also tell Chronosync to ignore certain tags. So I can tag my annotations in DevonThink (for example) and they are right there in the indexed folder but they never end up publicly on OneDrive.

So I’ve now got a really good, stable way of using indexed folders with cloud storage, whilst also making it very easy to remove individual subfolders folders from DevonThink, without accidentally trashing them on OneDrive.

… Which brings me back to the script. What I’m doing there is having a button to give me the real OneDrive URL; and I think convert that to a share link. I can send other people a link to the file I need them to check, and they can see and edit it on OneDrive then.

:flushed:
This sounds like an administration problem if people are accidentally deleting data. Yikes!

I’m not seeing any particular issue when running the script from DEVONthink’s Scripts menu.

Here’s a little error-trapping and cleanup…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property devonThinkPath : "/Volumes/DevonThink/OneDrive/"
property oneDrivePath : "/Volumes/Data2/OneDrive/Documents/"
tell application "DEVONthink 3"
	set theSelection to the selection
	if (theSelection ≠ {}) and ((count theSelection) = 1) then -- Only process one file; ignore empty selection
		set firstRecord to item 1 of theSelection
		set isIndexed to indexed of firstRecord -- Only process a file if it's indexed
		if isIndexed then
			set thePath to path of firstRecord
			my revealInOneDrive(path of firstRecord, devonThinkPath, oneDrivePath)
		else
			beep 2
		end if
	end if
end tell

on revealInOneDrive(thePath, devonThinkPath, oneDrivePath)
	set oneDrivePath to findAndReplaceInText(thePath, devonThinkPath, oneDrivePath)
	set oneDrivePosixPath to POSIX file oneDrivePath
	tell application "Finder"
		activate
		try -- Comment out the try…end try while debugging.
			reveal oneDrivePosixPath
		on error
			display alert "Related file can't be located"
		end try
	end tell
end revealInOneDrive

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText
2 Likes

Some further (untested) recommendations …

on run()
	tell application id "DNtp"
		set theSelection to the selection
		if (theSelection ≠ {}) and ((count theSelection) = 1) then -- Only process one file; ignore empty selection
			set firstRecord to item 1 of theSelection
			if (indexed of firstRecord is true) -- Only process a file if it's indexed
				my revealInOneDrive(path of firstRecord, devonThinkPath, oneDrivePath)
			else
				my do_alert(0)
			end if
		else
			my do_alert(1)
		end if
		...
	end tell
end run

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AStid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to AStid
	return theText
end findAndReplaceInText

on do_alert(alertnum)
	-- write an alert handler
end do_alert


JJW

1 Like