Help w/AppleScript to Import Folder Contents Into DEVONthink

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

Moderator: Mr_Noodle

First, I'm new at AppleScript. Please be kind.

I feel like I've gone down a rabbit hole on this one. I can't figure out what the problem is. I have gone down several different paths with examining the contents of theFile.

The goal of this script is to identify if a new item in a Hazel folder is a file or a folder.

If theFile is a folder, the script should enumerate the files within the folder. For each file within the folder, it should import into DEVONthink Pro inside existing Groups based on the name of the folder. If the Group doesn't exist in DTP, it should create a new one.

If theFile is a file, it should just add the file to the existing Financial Records Group.

I believe the code works correctly to identify whether theFile is a file or a folder. The problem is that when it is a folder, I have been unable to enumerate the contents of the folder so that I can add each one to DTP.

Any help is appreciated!

Jon

Code: Select all
-- Grab the containing folder
set theFolder to item 3 of inputAttributes

-- Check to see if theFile is a file or folder
tell application "Finder"
   tell application "System Events"
      if theFile is package folder or kind of theFile is "Folder" or kind of theFile is "Volume" then
         set isFolder to true
      else
         set isFolder to false
      end if
   end tell
end tell

-- Launch DEVONthink if it isn't already open.
tell application "System Events"
   if not (exists process "DEVONthink Pro Office") then
      tell application id "com.devon-technologies.thinkpro2" to activate
   end if
end tell

-- Import the file
tell application id "com.devon-technologies.thinkpro2"
   set theDatabase to open database "~/DEVONthink/Finances.dtBase2"
   
   set thePath to POSIX path of ("Financial Statements:" & theFolder)
   
   set theGroup to get record at thePath in theDatabase
   if not (exists (theGroup)) then
      set theGroup to create location thePath in theDatabase
   end if
   
   if isFolder then
      set output to {} -- this will be a list of the output items
      tell application "Finder"
         if theFiles's kind is "Alias" then
            set the end of output to POSIX path of (original item of theFile as alias)
         else
            set the end of output to POSIX path of theFile
         end if
      end tell
      
      tell application "System Events"
         set importFiles to the contents of output
      end tell
      
      set item_count to (get count of items in importFiles)
      
      repeat with i from 1 to item_count
         set {theFileName, theFileExtension} to {name, name extension} of anItem
         
         -- Hide extension in Finder
         set extension hidden of anItem to true
         
         -- Remove extension for DEVONthink displays
         set theFileName to texts 1 thru -((count theFileExtension) + 2) of theFileName
         
         set theImport to import anItem name anItem to theGroup
      end repeat
      
   else
      
      set {theFileName, theFileExtension} to {name, name extension} of theFile
      
      -- Hide extension in Finder
      set extension hidden of theFile to true
      
      -- Remove extension for DEVONthink displays
      set theFileName to texts 1 thru -((count theFileExtension) + 2) of theFileName
      
      set theImport to import theFile name theFileName to theGroup
   end if
end tell

return true
JonnieCache
 
Posts: 2
Joined: Sun Jun 23, 2019 10:47 am

This seems to be more of a general AppleScript question. Doing a quick search shows this as one way to go through files: https://stackoverflow.com/questions/144 ... one-by-one
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:This seems to be more of a general AppleScript question. Doing a quick search shows this as one way to go through files: https://stackoverflow.com/questions/144 ... one-by-one


Thank you, good sir! I agree that it is perhaps more of an AppleScript question than Hazel. I was thinking that because "theFile" is provided by Hazel that someone here could help better than StackOverflow or the like.

That said, thank you so much for the link! I'll give that a shot.

Cheers,

Jonathan
JonnieCache
 
Posts: 2
Joined: Sun Jun 23, 2019 10:47 am


Return to Support