Returning a value to Keyboard Maestro

I am writing a Keyboard Maestro macro to copy the selected text in a PDF to a Markdown file, and I need access to two pieces of data from the PDF in DT3: the record name and the value of a custom field.

I would like to write two scripts, one that returns the value of the record name and a second script that returns the value of a custom field labeled citekey. My intent is to run the two AppleScripts from within KBM, in the macro chain:

I am not sure how to tell the script to return the value such that KBM picks it up and assigns it to the variable. I assume this is a rookie-level challenge, but I readily admit that I am not even at that level yet… Can anyone help?

May be this helps:
http://downloads.techbarrack.com/books/programming/AppleScript/website/handlers/return_statement.html
First hit when searching for “AppleScript handler return value”

Thanks for the link. This returns the name of the item, so that’s the first objective complete:

tell application id "DNtp"
	return name of content record
end tell

I’m still looking for the AppleScript to access the citekey custom field.

I’m looking at this post and trying to piece things together, but am not finding the right AppleScript to return the contents of the custom field citekey… Is this close?

tell application id "DNtp"
	return custom meta data from "citekey" of content record
end tell

Another approach is to gather the various values in a single KM Execute ... Script action, and then return them by binding values to one or more KM variables from inside the script:

Perhaps this kind of pattern:

tell application id "DNtp"
    set thinkWindows to think windows
    if 0 < (count of thinkWindows) then
        set contentRecord to content record
        if missing value is not content record then
            set recordName to name of contentRecord
            
            ------ ASSUMING META DATA WITH THE NAME "citeKey" -----
            set keyValue to get custom meta data for "citeKey" from contentRecord
            
            ------- NORMALISING TO A STRING VALUE IF MISSING ------
            if missing value is not keyValue then
                set citeKey to keyValue
            else
                set citeKey to ""
            end if
            
            ---------- SETTING KEYBOARD MAESTRO VARIABLES ---------
            tell application "Keyboard Maestro Engine"
                setvariable "recordName" to recordName
                setvariable "citeKey" to citeKey
            end tell
        end if
    end if
end tell

Or an alternate version…

set citekey to ""

tell application id "DNtp"
	if content record ≠ missing value then
		set recordName to (name of the content record)
		set citekey to (get custom meta data for "citekey" from content record)
		my KM(recordName, citekey)
	else
		display alert "You must be displaying the file you're interacting with."
	end if
end tell

on KM(recordName, citekey)
	tell application "Keyboard Maestro Engine"
		setvariable "recordName" to recordName
		setvariable "citeKey" to citekey
	end tell
end KM

an alternate version…

Tho that entails a partial function – it will error when the value of citeKey is missing value

tell application "Keyboard Maestro Engine"
    setvariable "citeKey" to missing value -->  🔥 
end tell

Obtaining the empty string that Keyboard Maestro needs in lieu of a missing value is simpler, of course, in JavaScript:

// citeKey :: String
citeKey = keyValue || ''
JS Source
(() => {
    'use strict';

    const
        think = Application('DEVONthink 3'),
        windows = think.thinkWindows;

    if (0 < windows.length) {
        const contentRecord = think.contentRecord();
        if (null !== contentRecord) {
            const
                recordName = contentRecord.name(),
                keyValue = think.getCustomMetaData({
                    for: 'citeKey',
                    from: contentRecord
                }),
                // citeKey :: String
                citeKey = keyValue || '';
            const
                kme = Application(
                    'Keyboard Maestro Engine'
                );
            return (
                kme.setvariable('recordName', {
                    to: recordName
                }),
                kme.setvariable('citeKey', {
                    to: citeKey
                }),
                [recordName, citeKey]
            );
        }
    };
})();

Or even more directly:

JS Source
(() => {
    'use strict';

    const
        think = Application('DEVONthink 3'),
        windows = think.thinkWindows;

    if (0 < windows.length) {
        const contentRecord = think.contentRecord;
        if (contentRecord.exists()) {
            const
                recordName = contentRecord.name(),
                // citeKey :: String
                citeKey = think.getCustomMetaData({
                    for: 'citeKey',
                    from: contentRecord
                }) || '';
            const
                kme = Application(
                    'Keyboard Maestro Engine'
                );
            return (
                kme.setvariable('recordName', {
                    to: recordName
                }),
                kme.setvariable('citeKey', {
                    to: citeKey
                }),
                [recordName, citeKey]
            );
        }
    };
})();

This is very helpful, thank you! I have integrated @houthakker 's suggestion and included a check for a valid citekey and this now works:

set citekey to ""

tell application id "DNtp"
	if content record ≠ missing value then
		set recordName to (name of the content record)
		set citekey to (get custom meta data for "citekey" from content record)
		if citekey = missing value then
			display alert "You must set the CiteKey first."
			error number -128
		end if
		my KM(recordName, citekey)
	else
		display alert "You must be displaying the file you're interacting with."
	end if
end tell

on KM(recordName, citekey)
	tell application "Keyboard Maestro Engine"
		setvariable "recordName" to recordName
		setvariable "citeKey" to citekey
	end tell
end KM

Is there an AppleScript that can retrieve the page link for the selected page? In this post you pointed out the Cmd-Ctrl-Option-Shift-C keystroke to retrieve the page link:

This seems to work sometimes, but KBM does not seem to trigger it consistently. Is there a way to retrieve the page link for the current PDF using AppleScript? If so, I would like to integrate into the AppleScript above and set a pageLink variable that way.

The script I posted yesterday copies the Reference URL. What do you want? A Markdown link?

I don’t how I missed that! It looks like it has everything I need. Thank you :+1:

1 Like

You’re welcome.
I had a crash in Script Editor and it didn’t save my last edits, so I accidentally pasted an older version of the script.