Shortcuts on Monterey

Here’s a quick sample of a Shortcut that

  1. Asks for a text input
  2. Runs a JS to rename the currently selected DT3 document
  3. Opens that DT3 document via the x-devonthink URL

Download the shortcut: Shortcuts

Most of the code is just for demonstration, to give you an idea about what’s possible with JS/AppleScript.

function run(input, parameters) {
	// The input parameter is a two-dimensional array...?
	var newName = input[0][0];

	// Get access to DEVONthink.	
	var app = Application('com.devon-technologies.think3');
	app.includeStandardAdditions = true;
	
	// Display a notification to confirm the input string.
	var message1 = `Rename document to "${newName}"?`;
	app.displayDialog(message1);
	
	// When clicking "Cancel" in the dialog, the script stops:
	// At this point we are certain that the user clicked "Okay".

	// Modify the currently selected document from DEVONthink.
	var docs = app.selection();
	var doc = docs[0];
	var oldName = doc.name();
	
	var message2 = `Renamed document "${oldName}" to "${newName}".`;
	
	// Update the DEVONthink document.
	doc.name = newName;
	
	// All done. SHow confirmation.
	app.displayAlert(message2);
	
	// Sample: Open the document via the x-devonthink URL
	return 'x-devonthink-item://' + doc.uuid()
}

To find more details on DT3 functions:

  1. Open the app “Script editor”
  2. Press ⇧+⌘+O (File > Open function library …)
  3. Find the “DEVONthink 3” app and open it
  4. I suggest writing your script inside the Script Editor because it’s easier to debug - in Script Editor you can use console.log(); that’s not possible in Shortcuts. Once your script is working, copy it to Shortcuts.

Excellent - much appreciated

How do I do the reverse, i.e. retrieve a document or metadata via Applescript/JXA and then pass that on to other apps via Shortcuts?

DT3 has a great API, but the difficult part is finding all the relevant bits and pieces. I suggest using Script Editor and the Function library to write/debug/test your script.

Some functions that might be helpful for you:

// Read custom meta data from a document:
var country = app.getCustomMetaData({for: 'country', from: doc});

// Get the name the a document:
var name = doc.name();

// DB name of the document:
var db = doc.database.name();

// Get full document contents:
var text = doc.plainText();

Once your script is working, simply use return myValue; to pass the value to the next shortcut action.

1 Like

Thanks I will try that

Well, there’s little point to automate an app you do not have. So I don’t see that as a disadvantage,. It’s the same as a script that requires the dictionary of a certain app: without all, no dictionary, so the script can’t run.

Why do you even need Shortcuts for that? The whole thing could be written in JXA.
In addition (again) some blatant PR: Introduction to JXA | JavaScript for Automation (JXA) has some pointers on how to use JXA with DT and other apps.

2 Likes

Because it’s a great teaching example.

First of all, check out the DT3 scripts that come with the app. There are a bunch of them. Also, have a look at the Take Control of DEVONthink book and the MacSparky Field Guide. Each has pretty extensive section on automating DEVONthink.

2 Likes

I know why I need (or rather, want) it, but I get the impression you’re not certain that a scripting solution will help you.

Personally, I pack the DT3/DTTG logic into utility-shortcuts which are called by other shortcuts. That way, I can replace DT3 with a different storage at any point without rewriting other existing shortcuts (but I do not plan to).

Here’s a sample condition that I use to run the correct utility, depending on whether the shortcut runs on the iPhone or Mac:

(get device-model / if device-model is “Mac” / Run shortcut “DT3 with JS” / Else / Run shortcut “DTTG on iPhone”)

That approach closes the compatibility gap for my use-case.
What specific scenario do you want to cover across your devices?

I do get the advantages of scripting. In fact, I advocate it over shortcuts (especially JXA). In my opinion, shortcuts is of very limited utility, and I tried to argue that point in this thread before.

After you explained your rationale, I see the point of your example. I’m not convinced that one could write an abstraction layer to isolate the underlying storage system (at least not if it’s something as complex as DT), but that’s neither here nor there). If that scratches your itch, who am I to argue. But since DT stores is documents as they are in the filesystem …

Fortunately, I do not have to cross the device gap. This thread started out with an argument about missing DT support for shortcuts, I tried to argue that shortcuts are not that great anyway and that I’d rather have good scripting support on all platforms.

1 Like

Related but somewhat off-topic…

Included in the documentation for my unofficial DT3 API and Zapier interface is an iOS shortcut which accepts an X-Devonthink UUID and downloads the document or Group to an app of your choice. I was pleasantly surprised to see that the shortcut works equally well now on macOS as on iOS

The Shortcut link is here (you need to download it with iOS; then it syncs to Monterey and works there too):

Shortcuts

Incidentally - the shortcut above is a good example of something you can do with Shortcuts that you cannot do with either Applescript or JXA. It lets you retrieve a document via an API and then send that document to any compatible application installed on either iOS or macOS.

I do not believe any way to do the same in Applescript or JXA without manually coding the scenario for each app you want to support on each platform.

More specifically- can you initiate a generic “Share” request via Applescript or JXA as you can in Shortcuts? I can’t find much about that online except this old post where it was hard to do

For sure this cannot be done on iOS without Shortcuts because you cannot use Applescript or JXA on iOS.

In Shortcuts this is pretty straightforward for either iOS or macOS

I see this “Shortcuts” as an “Automator” for advanced users, “overadvanced” for “Automator” but “underadvanced” for AppleScript (and the like). Almost everything Shortcuts can do one can do with AppleScript + Keyboard Maestro in a much more elegant way ))

As for why we don’t see a success in this direction, and to say more, will never see - is just a matter of a few simple figures: share of users who really want or rely on an inter-app automation is about 5%. 3-4 out of these 5 percents already know Apple/JavaScript, ObjC, JXA or the like. Tell me now will there be a real push for Apple (or would it be any other company) to really invest in all this just for this 1 but “overdedicated”) percent? Never. Except allowing a little playground for enthusiasts.

Oh, and DT3 is able to more than suit almost all these 5% with its own automation techniques (smart rules and folders and etc) and existing functions even if you don’t know all these AppleScripts… )

1 Like

I beg to differ. Automation is important not only for enthusiasts but also in a business environment. I worked in publishing, and a lot of the processes ran on Macs and used different kinds of automation tools from finder actions to AS running in Quarkexpress. Given that Microsoft’s office programs as well as Adobe’s software support macros respectively other scripting techniques, I believe that automation is important for a relevant clientele. Maybe not relevant for Apple, though.

Yes, you are just talking about these 4 percents
“Enthusiasts” - are those 5-4=1% :wink:
Don’t want to invest in learning automating languages - keep this “overhumanized-in-exchange-of-usefullness” tool and enjoy )

Are there any updates on that topic? Now even the Pages and keynote are supporting Shortcuts :slight_smile:

About the argument that programming with apple script or JXA is better - maybe in general yes, but not for me in this moment, after spending a day in work on manipulating Excel sheets in R or programming in Python I don’t want to spend another part of my evening or weekend on learning new language for simple piping, example workflow:

copy link to www page → DT shortcut save to indicated database → Shortcut get devonthink x-link → output Link name

I think there are many users who dont have time/energy to learn current scripting but would be happily using even rudimentary Apple Shortcut; Heh, there is even a quite substantial mac power users community who embrace shortcuts (counting by number of paid and unpaid portals posting about shortcuts, I didnt saw so many posts about users favorite Apple Scripts :slight_smile: )

There also nice tools for debugging shortcuts now
Logger Is the Missing Console for Shortcuts Power Users - MacStories

While that might be true, Shortcuts currently is a broken mess. And it also requires learning, but of an obscure under-documented product that has (as Applescript) no use outside of the Appleverse.

I agree that the idea of shortcuts is nice. Like the idea of Automator. But Apple (was “Adobe” because of auto-correction) apparently can’t be bothered to implement and maintain something really useful.

1 Like

Adobe?

Adobe was a victim of Apple’s perfect auto-correction on the iPhone’s keyboard.

2 Likes

Everything depends on the priorities of the developers :wink:
I hope the shortcuts will be soon good enough to invest developers time and support there.
I could use Applescript to run from shortcuts, so I am able to adjust my workflow if I really need it, but with default shortcuts support probably would be easier :slight_smile: