Open selected records with external applications

In some cases, the app I want to use is not the default app, so I can’t do that with ⌘⇧O. In finder, I can achieve “open selected files with specific application” with the following automator service:

But in DEVONthink, I simply don’t know how. I’ve tried AppleScript as well, but failed at the very first step:

tell application "DEVONthink 3" to log ( path of  selected record)

returns

execution error: Can’t get path of selected record. (-1728)

If all you want is to open records in a specific app then there’s no need to use AppleScript or Automator. You can assign shortcuts via system preferences or CustomShortcuts.app.

If you want to use AppleScript take a look at this example:

-- Open in Marked 2

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select a Markdown record"
		
		repeat with thisRecord in theRecords
			set thisType to (type of thisRecord) as string
			if thisType is in {"markdown", "«constant ****mkdn»"} then
				set thisPath to path of thisRecord
				
				tell application "Marked 2"
					try
						activate
						open thisPath
					on error error_message number error_number
						if the error_number is not -128 then display alert "Marked 2" message error_message as warning
						return
					end try
				end tell
				
			end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

(You can ignore the part that’s testing for the correct record type)


PS Not sure where you are developing your script, if it’s in Automator then that’s a bad place to write it, I think. If so use at least Script Editor.app instead (or download Script Debugger).

Use (selected records) or (selection as list). Both returns lists so you’ll want to reference items in a list.

Here’s a common and pretty trouble-free construct…

tell application "DEVONthink 3"
	repeat with theRecord in (selection as list)
		-- Do stuff with theRecord
	end repeat
end tell

My app isn’t even in the “open with” menu, so… It’s weird actually, since it does support that file type.

Yeah, debugging AppleScript is real nightmare (and the same for iOS Shortcuts), that’s why I never take the time to dig into it. Please don’t take this as an excuse :grin:. I always have a hard time reading app-specific docs in Script Editor. Anyway, I will definitely give Script Debugger a try. Thx for the suggestion.

As for the script, don’t really have the time to try it now. Wait for my good news later! And @BLUEFROG, thx for the fully (not “semi-”) useful addition :wink:.

You’re welcome.

What file type and what application are you trying to do the Open With?

The script you provided works exactly as expected, huge thanks :grin:. I’ve also assembled a script that works both in Finder and DEVONthink as follows:

set openWithApp to application "BetterZip"
set openWithAppName to "BetterZip"

tell application "System Events"
	set activeApp to name of first application process whose frontmost is true
end tell

if "Finder" is in activeApp then
	tell application "Finder"
		open (get selection) using path to openWithApp
	end tell
	
else if "DEVONthink 3" is in activeApp then
	tell application "DEVONthink 3"
		repeat with theRecord in (selection as list)
			set thePath to path of theRecord
			tell openWithApp
				try
					activate
					open thePath
				on error error_message number error_number
					if the error_number is not -128 then display alert openWithAppName message error_message as warning
					return
				end try
			end tell
		end repeat
	end tell
end if

The file type is .CBZ, Comicbook ZIP Archive, which is basically a .zip file renamed, and the application is BetterZip. I am sure this is not a DEVONthink-related issue, as it’s the same in Finder’s contextual menu.

After I set the default app used to open .cbz files as “YACReader”, BetterZip also disappeared from the Open With menu.

I also don’t use BetterZip as the default app for opening .zip files because BetterZip has some issue with filename encoding. (But it is useful when editing archive contents, so I still keep it for that use.) However, BetterZip is still in the Open With list of zip files.

BTW: I find DEVONthink 3’s smart rules, metadata as well as the long-standing smart group really handy in organizing my comic collection.

Just use the construct I gave you…

tell application "DEVONthink 3"
	repeat with theRecord in (selection as list)
		-- Do stuff with theRecord
	end repeat
end tell

Using this, there is no need to set a variable to selected records and check if the list is empty.
If the list is empty, the repeat with pass that along and nothing will (logically) happen.

1 Like

I used selcted record at first instead of selected records because that’s what I found in Script Editor’s Dictionary

I didn’t even know that selected records is available. Is there any other resources recommended other than Dictionary in Script Editor when one is developing a AppleScript of DEVONthink? With Shortcuts coming to macOS, how necessary is it now to learn AppleScript well? For now, I only imitate others’ scripts, but lack much fundamental knowledge of AppleScript. I think I would have to learn from the most basic things if I want to start writing my own AppleScripts.

It appears to just be a typo in the dictionary.

With Shortcuts coming to macOS, how necessary is it now to learn AppleScript well?

Too early to tell but I would always suggest knowing AppleScript if you’re inclined to do real fine-grained automation.

I think I would have to learn from the most basic things if I want to start writing my own AppleScripts.

That’s where we all started. :slight_smile:

I learn from looking at scripts posted in these forums; also the scripts delivered with Devonthink
Even if I don’t need the script, I work through each command to learn the use

any other resources recommended

Google search. I use this often when I need a solution to a specific problem

1 Like

Check out the Help > Documentation > Automation > Applescript section for some links.

Ah, good idea to get you started with AppleScript! But for truly knowing what you are doing, I think it’s necessary to learn from, e.g. a manual.

Try Introduction to AppleScript Language Guide

When nothing happens that’s a problem for me. While writing scripts it happened often that I didn’t immediately grasp why nothing … nothing … ??? … That happens especially when a script does not yet work the way I want. Unnecessary confusion.

One way to prevent this is to display a dialog. However I’ve found that while writing it’s better to use one (or some) fixed test records via get record with uuid. Caveat: one should definitively not forget to test with other records afterwards (happened more than once that I initially had choosen suboptimal test records :roll_eyes: … )

A dialog is also a good idea to give other users who might use a script some feedback that explains why the script didn’t do what they expected.

This is much faster:

tell application "System Events"
	set activeApp to displayed name of (path to frontmost application)
end tell

Script Debugger’s dictionary window has different views (see the tabs in the capture). You’ll find it in Script Debugger’s menu Window > Dictionary

Didn’t read it but maybe this thread is intersting Shortcuts + Applescript?

2 Likes

The Inherits view looks amazing!

PS: also don’t feel like reading such a long thread :stuck_out_tongue_winking_eye: as English is not my mother tongue. Well, let me just wait and see what Shortcuts is going to bring us~

1 Like