Skip to content

Instantly share code, notes, and snippets.

@jocap
Last active December 17, 2023 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jocap/4c357ef2b59e0ed66bcbfc7a5e8cbcaa to your computer and use it in GitHub Desktop.
Save jocap/4c357ef2b59e0ed66bcbfc7a5e8cbcaa to your computer and use it in GitHub Desktop.
DEVONthink 3 JXA for finding the selected citation in the reference list of the current document.
(function(){
let dt3 = Application("DEVONthink 3");
dt3.includeStandardAdditions = true;
var sel = dt3.windows()[0].selectedText()
if (sel) {
sel = sel.replace(/ +and +/, " ")
sel = sel.replace(/ +och +/, " ")
sel = sel.replace(/ +& +/, " ")
sel = sel.replace(/, +/, " ")
sel = sel.replace(/['’]s +/, " ")
sel = sel.replace(/\(/, "")
sel = sel.replace(/\)/, "")
sel = sel.replace(/ ([0-9]{4})/, " NEAR $1")
let clipboard = dt3.theClipboard()
dt3.setTheClipboardTo(sel)
let se = Application("System Events")
let p = se.processes.byName("DEVONthink 3")
dt3.activate()
var win = dt3.windows()[0]
if (win.class() == "viewerWindow") {
p.menuBars.at(0).menuBarItems.byName("Data").menus.byName("Data")
.menuItems.byName("Open").click()
while (dt3.windows()[0] == win)
delay(0.1)
win = dt3.windows()[0]
}
let isInfoClosed = win.class() != "viewerWindow"
&& p.windows[0].uiElements()[0].uiElements().length == 1
if (isInfoClosed) {
var bounds = win.bounds()
bounds.width += 280
win.bounds = bounds
}
p.menuBars.at(0).menuBarItems.byName("Edit").menus.byName("Edit")
.menuItems.byName("Find").menus.byName("Find").menuItems.byName("Find...").click()
p.menuBars.at(0).menuBarItems.byName("Edit").menus.byName("Edit")
.menuItems.byName("Paste").click()
dt3.setTheClipboardTo(clipboard)
ObjC.import("Cocoa")
while ($.NSEvent.modifierFlags != 0)
delay(0.01)
se.keyCode(36) // return
try {
while (p.windows[0].splitterGroups[0].uiElements().filter(x => x.class() == "busyIndicator").length)
delay(0.1)
} catch(err) {
}
delay(0.1)
p.windows[0].splitterGroups[0].scrollAreas[0].tables[0].rows[-1].select()
if (isInfoClosed) {
p.menuBars.at(0).menuBarItems.byName("Tools").menus.byName("Tools")
.menuItems.byName("Inspectors").menus.byName("Inspectors")
.menuItems.byName("Hide").click()
var bounds = win.bounds()
bounds.width += -280
win.bounds = bounds
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment