Move newly downloaded html files to DEVONthink

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Move newly downloaded html files to DEVONthink Sat Feb 18, 2023 10:28 am • by YuTang
The original post is here: https://discourse.devontechnologies.com ... base/74673
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

Image

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);

}
YuTang
 
Posts: 1
Joined: Mon Jan 24, 2022 7:14 am

I can't really debug your script for you but if you turn on debug logging as described here, you can see script output in the logs: https://www.noodlesoft.com/kb/hazel-debug-mode/
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Open Discussion