Key bind/menu/AppleScript to jump to a specific sub group in devonthink

Hi all

I want to be able to use keyboard maestro to create a macro to copy a name and then create a specific markdown note from a devonthink template in a specific subgroup. I have everything working apart from how to switch to a specific subnote in devonthink :slight_smile:

I couldn’t find a menu way to this so I assume I could use apple script. I want to one able to switch devonthink to a subgroup ‘food review’ (DB>Home>food review)

is there a way to do so?

best regards and thx in advance

Z

Also I now realized that this could probably all be done with apple srcipt but way over my head…:slight_smile:
if anyone has an example of such a workflow in AppleScript which would be something like:

  1. copy a text anywhere on Mac
  2. in devonthink switch to a defined group (this may not be needed if apple script can auto file to that group)
  3. launch my pre defined template and rename the markdown note with the clip name as well
  4. label as todo
    thx so much again :slight_smile:

Hi, don’t try to copy text via AppleScript, I’ve tried it over and over again but it never worked reliably.

Try this instead (from the Keyboard Maestro forum).

I’ve never used DEVONthink templates (so I can’t help with that), but here’s how to create a markdown note in a specific group

-- Create markdown record in specific group

tell application id "DNtp"
	try
		
		set theGroup to (get record with uuid "35472373-565D-47C6-9E79-45F77640F746") -- this is an UUID. Copy the Reference URL via the menu and delete everything before (and including) "://"
		
		set theRecord to create record with {name:"Test", type:markdown, source:"Text"} in theGroup -- create a new markdown record
		
		#set label of theRecord to 1 -- not sure which label is "todo" 
		
		#open window for record theRecord -- uncomment if you want to open the new record
		#activate
		
		#open window for record theGroup -- uncomment if you want to open the group
		#activate
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

If you weren’t using a template, you may be able to use Services > Create Markdown Document, depending on the current application’s selection.

PS:

  1. in devonthink switch to a defined group (this may not be needed if apple script can auto file to that group)

You should not think of automation as a machine that pushes buttons, like switching to groups, except for very specific instances. The software doesn’t need to switch groups or windows in order to receive content. That’s a mechanical process we do but not required for interactions like this.

Thx @BLUEFROG appreciate the answer
is there documentation for beginners on how to do that or examples you can refer to?im not super technical but may try and pick that up.
if I understand correctly from you (and maybe im wrong) your saying use scripts to achieve that instead of the GUI?

best

Z

GUI scripting is a fragile and fiddly thing. And proper automation isn’t about pushing buttons, etc. It’s about automating the underlying processes. Here’s a simple example with some pseudocode…

tell application "SomeApp"
keystroke "H"
keystroke "e"
keystroke "l"
keystroke "l"
keystroke "o"
keystroke space
keystroke "W"
keystroke "o"
keystroke "r"
keystroke "l"
keystroke "d"
end tell

This is possible but not a good way to approach automating (and yes, people do this kind of thing - I’ve seen it). You aren’t making the computer type for you - and this is the conceptual mistake people make.

This is the better approach (bearing in mind the application must support AppleScript and a command such as this)

tell app "SomeApp"
set contents of document 1 to "Hello World"
end tell

I would suggest checking out the Automation chapter in the built-in Help, which has references to good resources on the web. Also, these forums have many good examples too.

@pete31
thx so much for the examples
the AppleScript does almost exactly what I wanted! Im still confused on the KM part and if its needed at all
what I meant by copying with AppleScript is I just want to paste from the clipboard. Is that still messy?
to clarify I would manually copy to the Mac clipboard a string. Then I want the AppleScript part that currently is

set theRecord to create record with {name:"Test", type:markdown, source:"#"} in theGroup

to paste the clip contents that is the contents of a variable $CLIP in the name and source parts

set theRecord to create record with {name:"$CLIP", type:markdown, source:"$CLIP"} in theGroup

Hope im not doing a terrible job at explaining this :slight_smile:

thx a lot for your kind help

Z

No, I thought that you tried to copy text - if you copy manually it’s in the clipboard.

Get the clipboard:

set theClipboard to the clipboard

Use it:

set theRecord to create record with {name:theClipboard, type:markdown, source:theClipboard} in theGroup

Thx @pete31 this is so cool

for future reference if anyone is interested here is the current state of the code

-- Create markdown record in specific group

tell application id "DNtp"
	try
		
		set theGroup to (get record with uuid "71A39E6F-DA44-4FBE-91C7-5E198D25122E") -- this is an UUID. Copy the Reference URL via the menu and delete everything before (and including) "://"
		
		set theClipboard to the clipboard
		
		set theRecord to create record with {name:theClipboard, type:markdown, source:theClipboard} in theGroup
		-- set theRecord to create record with {name:"Test", type:markdown, source:"#"} in theGroup
		
		set label of theRecord to 1 -- not sure which label is "todo" 
		
		-- create a new markdown record
		
		
		#open window for record theRecord -- uncomment if you want to open the new record
		#activate
		
		#open window for record theGroup -- uncomment if you want to open the group
		#activate
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

curious as a last thing since it seems that including a template rather than a blank md file is much more complex, is there anyway to make the script add a ‘#’ (md header) before the theClipboard variable?

I tried using escape and “” with no success, ie

set theRecord to create record with {name:theClipboard, type: markdown , source:#theClipboard} in theGroup

thx again for everything, its been a fantastic help!

Z

If you want to make a markdown heading try this

set X to "# " & (the clipboard)

Amazing!
thx so much for all the help @pete31, you rock :slight_smile:

for reference if anyone is interested here is the final script I use

-- Create markdown record in specific group

tell application id "DNtp"
	try
		
		set theGroup to (get record with uuid "71A39E6F-DA44-4FBE-91C7-5E198D25122E") -- this is an UUID. Copy the Reference URL via the menu and delete everything before (and including) "://"
		
		set theClipboard to the clipboard
		
		set theRecord to create record with {name:theClipboard, type:markdown, source:"# " & (the clipboard)} in theGroup
		-- set theRecord to create record with {name:"Test", type:markdown, source:"#"} in theGroup
		
		set label of theRecord to 1 -- not sure which label is "todo" 
		
		-- create a new markdown record
		
		
		#open window for record theRecord -- uncomment if you want to open the new record
		#activate
		
		#open window for record theGroup -- uncomment if you want to open the group
		#activate
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

thx Again

Z