Applescript importer to DevonThink Pro

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Applescript importer to DevonThink Pro Mon Jan 07, 2008 8:01 pm • by caseykoons
I apologize if the answer to this question lies somewhere on the site.

I'm an avid user of DevonTHINK Pro as a method of tracking research. This involves downloading a lot of pdfs and importing them into a database.
I use a central download folder called "Inbox" in my home folder. I've already set Hazel to do the appropriate things to download archives, dmgs, music files, etc. I have an applescript that will import pdfs to a specific DevonTHINK Pro database behind the scenes.

The problem is that not every pdf that I download should go into this database. So I tried to make an applescript that prompts me when it's loaded, asking me whether or not the pdf should be added to DevontTHINK. The script works fine outside of a hazelprocessfile script, but throws an generic applescript error when used with Hazel. I've attached the code below.
Is this an issue with Hazel not wanting to wait for an applescript to complete? Thanks for your help.

Code: Select all
on hazelProcessfile(addedFile)
   --on run
   set theFile to addedFile as alias
   set addedInfo to info for theFile
   set addedStr to name of addedInfo as string
   set theQuestion to display dialog "Do you want to add " & addedStr & " to ∂Research?" buttons {"Yes", "No"} default button "No" giving up after 10 with icon 1
   set theAnswer to button returned of theQuestion
   if theAnswer is "No" then
      return
   else
      try
         tell application "DEVONthink Pro"
            launch
            open database "/Users/username/Documents/Research/∂Research.dtBase"
            tell application "System Events" to set visible of process "DEVONthink Pro" to false
         end tell
         try
            set thePath to addedFile as text
            if thePath does not end with ".download:" then
               tell application "DEVONthink Pro"
                  if exists incoming group then
                     import thePath to incoming group
                  else
                     import thePath
                  end if
               end tell
               --tell application "Finder" to move addedFile to trash
            end if
         end try
      end try
   end if
   --end run
end hazelProcessfile
Last edited by caseykoons on Mon Jan 07, 2008 10:40 pm, edited 1 time in total.
caseykoons
 
Posts: 4
Joined: Mon Jan 07, 2008 7:30 pm

Update: checked the logs Mon Jan 07, 2008 9:03 pm • by caseykoons
I checked the logs to find the following error:
Code: Select all
2008-01-07 19:55:18.580 hazelfolderwatch[588] 0.pdf: Rule PDF catcher matched.
2008-01-07 19:55:19.547 hazelfolderwatch[588] [Error] AppleScript failed: Error executing AppleScript /Users/kc/Library/Scripts/Hazel to ∂Research.scpt on file /Users/kc/Inbox/0.pdf.
2008-01-07 19:55:19.575 hazelfolderwatch[588] AppleScript error: {
    NSAppleScriptErrorBriefMessage = "No user interaction allowed.";
    NSAppleScriptErrorMessage = "No user interaction allowed.";
    NSAppleScriptErrorNumber = -1713;
    NSAppleScriptErrorRange = <00000000 00000000 >;

Does this mean that hazel will not allow applescripts that involve any user interaction? If so, I'd be quite disappointed, as that is often the best way to parse things. Thanks for your input.
caseykoons
 
Posts: 4
Joined: Mon Jan 07, 2008 7:30 pm

Tue Jan 08, 2008 1:10 pm • by Mr_Noodle
Unfortunately, this is not quite doable, but let's tackle one thing at a time:

The process that runs the rules is a background process with no GUI so it is unable to put up a dialog. But you can use "System Events" to do it on your behalf. In your script, just bracket the code with:

Code: Select all
tell application "System Events"
...
end tell


The main problem, though, is that you can't have a script change the flow of the rest of the actions. You can throw an error but that just makes the rule fail and it will be re-executed until it succeeds.

I've thought about making Hazel interpret the return value of Applescripts. So, Hazel could possibly do something like if the script returns a certain value, it indicates that Hazel should stop processing actions (but still consider the rule to have successfully run).

The issue there is that if I add it in, it may break people's scripts currently as they wrote them with the understanding that the return value will be ignored. I'd need to figure out a way to migrate those older scripts over so that there will be no side effects when people upgrade. It's being considered and is more a matter of working out the details.

One workaround is to have your script set a comment or color label depending on the user's answer to the dialog. Then, have a separate rule that checks for that comment or color and does the move. It's a two phase process and it is a tad clumsy but for now it should work. Let me know how it works out.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I mashed up something with Automator last night that relied heavily on Spotlight comments. It was a work-around, using two different hazel rules and a workflow to import, tag, and move the pdfs around.
The applescript alternative (where you can simply add a flag to the file) is elegant in comparison. I'm toying with the idea of making a universal "HazelIgnore" tag for use with other rules. It's easy, quick, and it works great. Thanks for your help. I've attached the final code below for anyone who is interested in this sort of thing. I'm a firm believer that the prompting of pseudo-intelligent questions is the future of computing.

Code: Select all

on hazelProcessfile(addedFile)
   tell application "System Events"
      set addedFile to addedFile as alias
      tell application "Finder" to set addedStr to (get displayed name of addedFile)
      set theQuestion to display dialog "Do you want to add " & addedStr & " to ∂Research?" buttons {"Yes", "No"} default button "No" giving up after 10 with icon 2
      set theAnswer to button returned of theQuestion
      if theAnswer is "No" then
         tell application "Finder"
            set flag to "DoNotDT"
            set delim to "
"
            set currentCom to (get comment of addedFile)
            if currentCom does not contain flag then
               if currentCom is "" then
                  set comment of addedFile to flag
               else
                  set comment of addedFile to (currentCom & delim & flag) as string
               end if
            end if
         end tell
      else
         try
            tell application "DEVONthink Pro"
               launch
               open database "/Users/username/Documents/Research/∂Research.dtBase"
               tell application "System Events" to set visible of process "DEVONthink Pro" to false
            end tell
            try
               set thePath to addedFile as text
               if thePath does not end with ".download:" then
                  tell application "DEVONthink Pro"
                     if exists incoming group then
                        import thePath to incoming group
                     else
                        import thePath
                     end if
                  end tell
                  tell application "Finder" to move addedFile to trash
               end if
            end try
         end try
      end if
   end tell
end hazelProcessfile
caseykoons
 
Posts: 4
Joined: Mon Jan 07, 2008 7:30 pm

Thu Jan 10, 2008 1:01 pm • by Mr_Noodle
Thanks for sharing this.

So, right now, I'm considering some return value (like a specific string) that the script returns that would tell Hazel that everything executed successfully but it should stop processing. If you have any thoughts on this, let me know.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support