I want to write a script/automation outside of DEVONthink to complete my workflow. The reason is that I like to save useful web pages as HTML, but the HTML pages saved by DEVONthink often have some small problems. So, I prefer to use the SingleFile extension to save HTML.
I came up with a way to use Hazel to match and move the newly downloaded HTML file with the SingleFile extension to the Global Inbox folder in DEVONthink. At the same time, I run a script to get the URL of the currently active tab in the browser as a variable. After the file is moved, the script continues to match the HTML file in the Inbox folder based on the file name and assigns the URL variable to the URL of the file. I completed this code in Script Editor, and the result of running it was very smooth:
- Code: Select all
if (typeof exports === 'undefined') exports = {}
function timer (repeats, func, delay) {
var args = Array.prototype.slice.call(arguments, 2, -1)
args.unshift(this)
var boundFunc = func.bind.apply(func, args)
var operation = $.NSBlockOperation.blockOperationWithBlock(boundFunc)
var timer = $.NSTimer.timerWithTimeIntervalTargetSelectorUserInfoRepeats(
delay / 1000, operation, 'main', null, repeats
)
$.NSRunLoop.currentRunLoop.addTimerForMode(timer, "timer")
return timer
}
function invalidate(timeoutID) {
$(timeoutID.invalidate)
}
function run() {
$.NSRunLoop.currentRunLoop.runModeBeforeDate("timer", $.NSDate.distantFuture)
}
var setTimeout = timer.bind(undefined, false)
var setInterval = timer.bind(undefined, true)
var clearTimeout = invalidate
var clearInterval = invalidate
setTimeout.run = setInterval.run = run
exports.setTimeout = setTimeout
exports.setInterval = setInterval
exports.clearTimeout = clearTimeout
exports.clearInterval = clearInterval
exports.run = run
var fileName = "test";
var app = Application("DEVONthink 3");
app.includeStandardAdditions = true;
var edge = Application("Microsoft Edge");
var url = edge.windows[0].activeTab.url();
var database = app.databases.byId(1);
setTimeout(() => {
var record = app.search("name:" + fileName + " kind:HTML text" + " scope:inbox" + " {any: url==chrome-extension://efnbkdcfmcmnhlkaijjjmhjjgladedno/ url==chrome-extension://mpiodijhokgodhhofbcjdecpffjipkle/}")[0];
record.url = url;
}, 1000);
To ensure the accuracy of the matching, I have set the following matching conditions in Hazel for the ~/Documents/Inbox folder:
Extension is html
Subfolder depth is 0
Date added is after date last matched
Source URL is chrome-extension://efnbkdcfmcmnhlkaijjjmhjjgladedno / chrome-extension://mpiodijhokgodhhofbcjdecpffjipkle

When I tried to embed the script into Hazel, it didn’t work as expected. The URL was not correctly named. Could you please let me know where I went wrong?
- Code: Select all
function hazelProcessFile(theFile, inputAttributes) {
if (typeof exports === 'undefined') exports = {}
function timer (repeats, func, delay) {
var args = Array.prototype.slice.call(arguments, 2, -1)
args.unshift(this)
var boundFunc = func.bind.apply(func, args)
var operation = $.NSBlockOperation.blockOperationWithBlock(boundFunc)
var timer = $.NSTimer.timerWithTimeIntervalTargetSelectorUserInfoRepeats(
delay / 1000, operation, 'main', null, repeats
)
$.NSRunLoop.currentRunLoop.addTimerForMode(timer, "timer")
return timer
}
function invalidate(timeoutID) {
$(timeoutID.invalidate)
}
function run() {
$.NSRunLoop.currentRunLoop.runModeBeforeDate("timer", $.NSDate.distantFuture)
}
var setTimeout = timer.bind(undefined, false)
var setInterval = timer.bind(undefined, true)
var clearTimeout = invalidate
var clearInterval = invalidate
setTimeout.run = setInterval.run = run
exports.setTimeout = setTimeout
exports.setInterval = setInterval
exports.clearTimeout = clearTimeout
exports.clearInterval = clearInterval
exports.run = run
var app = Application("DEVONthink 3");
app.includeStandardAdditions = true;
var edge = Application("Microsoft Edge");
var url = edge.windows[0].activeTab.url();
var database = app.databases.byId(1);
setTimeout(() => {
// 这里放要延迟执行的代码
var record = app.search("name:" + fileName + " kind:HTML text" + " scope:inbox" + " {any: url==chrome-extension://efnbkdcfmcmnhlkaijjjmhjjgladedno/ url==chrome-extension://mpiodijhokgodhhofbcjdecpffjipkle/}")[0];
record.url = url;
}, 5000);
}