Autofile from Hazel -> OCR (PDFPenPro) -> DevonThink

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

For completeness sake, my modified script:
-----------------------------------------------------
Matches="Cleaners|/Utilities/Cleaners
Phone|/Utilities/Phone
Bank|/Finance/Bank
Cable|/Utilities/Cable
Community Giving|/Charity/Community Giving
Paystub|/Employment/My Employer
mc_0170|/mc_0170
mc_7814|/mc_7814"

IFS=$'\n' read -rd '' -a MatchNames <<<"$Matches"

FullPath=`echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"`

echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

j=0
for i in "${MatchNames[@]}"
do
IFS=$'|' read -rd '' -a GroupNames <<<"${MatchNames[$j]}"

MatchString=${GroupNames[0]}
FolderName=$(echo ${GroupNames[1]}) #use this to remove trailing new line character

echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
echo "$1" | grep -q "$MatchString"
greprc=$?
if [[ $greprc -eq 0 ]] ; then
echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
Command='
tell application id "com.devon-technologies.thinkpro2"\n
launch\n
set theDatabase to open database "/Users/erwin/DT_bases/MyScans.dtBase2\n
set theGroup to create location "'$FolderName'" in theDatabase\n
import "'$FullPath'" to theGroup\n
end tell\n
'
echo $Command >> ~/HazelAutomation/log.txt
echo $Command | osascript
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit
fi
let "j++"
done
echo "No match found for $1" >> ~/HazelAutomation/log.txt
cp "$FullPath" "/Users/erwin/Library/Application Support/DEVONthink Pro 2/Inbox."
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit
panini
 
Posts: 23
Joined: Mon Dec 08, 2014 6:54 pm

Hello!
I followed your way but the script doesn't work. When I run the script simple nothing happens. The File is not moved in the specific folder from DevonThink. The problem seems to be the Apple-Script.
The Part of your bash-script which generates the Apple-Script looks like that:
Code: Select all
       Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/Matthias/Documents/DEVONthink/Privat.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
           import "'$FullPath'" to theGroup\n
         end tell\n'

When i enter the generated Apple-Script, which is stored in the log-file log.txt into the Terminal:
Code: Select all
Command='tell application id "com.devon-technologies.thinkpro2"\n launch\n set theDatabase to open database "/Users/Matthias/Documents/DEVONthink/Privat.dtBase2"\n set theGroup to create location "/Versicherungen/Haftpflicht" in theDatabase\n import "/Users/Matthias/Documents/HazelAutomation/3.HazelProcessed/2016_12_28_Haftpflichtversicherung.pdf" to theGroup\n end tell\n'

echo $Command | osascript

The Terminal gives out an error:
Unknown Token found (-2741)

Maybe there is an error with the "-Signs.
May someone help?
Polaria
 
Posts: 5
Joined: Thu Dec 29, 2016 12:37 pm

Polaria wrote:Hello!
I followed your way but the script doesn't work. When I run the script simple nothing happens. The File is not moved in the specific folder from DevonThink. The problem seems to be the Apple-Script.
The Part of your bash-script which generates the Apple-Script looks like that:
Code: Select all
       Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/Matthias/Documents/DEVONthink/Privat.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
           import "'$FullPath'" to theGroup\n
         end tell\n'

When i enter the generated Apple-Script, which is stored in the log-file log.txt into the Terminal:
Code: Select all
Command='tell application id "com.devon-technologies.thinkpro2"\n launch\n set theDatabase to open database "/Users/Matthias/Documents/DEVONthink/Privat.dtBase2"\n set theGroup to create location "/Versicherungen/Haftpflicht" in theDatabase\n import "/Users/Matthias/Documents/HazelAutomation/3.HazelProcessed/2016_12_28_Haftpflichtversicherung.pdf" to theGroup\n end tell\n'

echo $Command | osascript

The Terminal gives out an error:
Unknown Token found (-2741)

Maybe there is an error with the "-Signs.
May someone help?


Can´t agree... Works fine for me....

But - only an idea - if you run the script without giving a filling to '$FolderName' the Script can´t be run successful cause the Foldername is missing? Can´t see a declaration for that?
Happy wife .... Happy life... :D
HaubenTaucher
 
Posts: 15
Joined: Sun Oct 23, 2016 8:23 am
Location: Germany / OWL

Hello baandab,
my good friend Rolf (aka HaubenTaucher) shared the link to your article in the forum.
Great work, thanks for the share.

Unfortunately it doesn´t work to upload screenshots :(

So far, my structure on the NAS and in Devonthink is the following (The documents are currently only indexed):

Ablage
2015
[*]Auto
[*]Bank
    Commerzbank
    Deutsche Bank
[*]Diverse
    A
    B
and so on
[*]Telefon
    Vodafone
    1&1

2016
[*]Auto
[*]Bank
    Commerzbank
    Deutsche Bank
[*]Diverse
    A
    B
and so on
[*]Telefon
    Vodafone
    1&1

The file name is: Amazon Rechnung 2016-12-15 Microsoft Office

My Hazel rule looks like for example:
Name starts with A
Name matches ...Jahr...

Move to NAS
Sort into Subfolder: Ablage
Sort into Subfolder: Jahr (from matches)
Sort into Subfolder: Diverse
Sort into Subfolder: A

This will sort the document in the right year on the NAS.

Do you have an idea whether and how I can install the annual exam accordingly in the script? This would also allow sorting into the right year in Devonthink.

Thanks for your help
Markus
AbwehrchefVC
 
Posts: 8
Joined: Sun Jan 08, 2017 9:46 am

Good morning from Germany,

meanwhile I have come a little further. First I have solved my requirement with the year so that I have created a Hazel rule with the year (name contains 2016).
Then I changed the script accordingly

Matches="CCL|/2016/Diverse/C
Cleverbridge|/2016/Diverse/C
CSL|/2016/Diverse/C
Cyberport|/2016/Diverse/C
"


It is still not perfect, but for the beginning already quite well. Automation would naturally be the highlight.

My biggest problem, however, is that the files are not moved. They are simply copied.
And that leads to a continuous loop. The files are copied permanently, so I have these 4-5 times in Devonthink before I can pause Hazel. :oops: :shock:

The code for the script I just copied and made my changes for the paths.
Or is there a mistake?

Please, I need some help to solve this problem. Here is my script

Code: Select all
#!/bin/sh

# This script moves files of to DevonThink folders automatically
# based on the $Matches variable.
#
# It looks for a match in the PDF File Name, and moves the file to
# the corresponding DevonTHINK Folder Name.
#
# If it does not find a match, it copies the file to the Inbox.
#
# In either case, it moves the file to the Backup folder
#
# use this pattern to match files:  PDFFileName|DevonTHINKFolderName

Matches="CCL|/2016/Diverse/C
Cleverbridge|/2016/Diverse/C
CSL|/2016/Diverse/C
Cyberport|/2016/Diverse/C
"

IFS=$'\n' read -rd '' -a MatchNames <<<"$Matches"

FullPath=`echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"`
NewName=`echo "$(basename "$1")" | sed "s/.[pP][dD][fF]//"`

echo $FullPath
echo $NewName

echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

j=0
for i in "${MatchNames[@]}"
do
      IFS=$'|' read -rd '' -a GroupNames <<<"${MatchNames[$j]}"

      MatchString=${GroupNames[0]}
      FolderName=$(echo ${GroupNames[1]})  #use this to remove trailing new line character

      echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
#      echo $j "["$MatchString"]"
      echo "$1" | grep -q "$MatchString"
      greprc=$?
      if [[ $greprc -eq 0 ]] ; then
         echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
#         echo "Found:[$MatchString] in [$1] moving to [$FolderName]"
         Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/MC/DevonThink/Test.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
            set theRecord to import "'$FullPath'" to theGroup\n
            set the name of theRecord to "'$NewName'"\n
         end tell\n
         '
         echo $Command  >> ~/HazelAutomation/log.txt
         echo $Command | osascript
         mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
         exit
      fi
      let "j++"
done
echo "No match found for $1"   >> ~/HazelAutomation/log.txt
cp "$FullPath" "/Users/MC/Library/Application Support/DEVONthink Pro 2/Inbox.dtBase2"
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit


Thanks, Markus
AbwehrchefVC
 
Posts: 8
Joined: Sun Jan 08, 2017 9:46 am

And this is the logfile from Hazel "View Log". A logfile in the path
echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

is not created

2017-01-10 08:08:28.941 hazelworker[64434] Shellscript exited with non-successful status code: 1
2017-01-10 08:08:28.942 hazelworker[64434] CCL Computers Ltd. StarTech Rechnung 2016-04-20 Thunderbold 2 Dual-Video Display Port.pdf: Rule Diverse C 2016 matched.
2017-01-10 08:08:29.092 hazelworker[64434] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/DevonThink-Script-C-2016-neu.sh on file /Users/MC/Documents/HazelAutomation/2.HazelSort/CCL Computers Ltd. StarTech Rechnung 2016-04-20 Thunderbold 2 Dual-Video Display Port.pdf.
2017-01-10 08:08:29.093 hazelworker[64434] Shellscript exited with non-successful status code: 1
2017-01-10 08:08:29.098 hazelworker[64434] Cleverbridge Rechnung 2016-05-08 Paragon NTFS Mac 14.pdf: Rule Diverse C 2016 matched.
2017-01-10 08:08:29.246 hazelworker[64434] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/DevonThink-Script-C-2016-neu.sh on file /Users/MC/Documents/HazelAutomation/2.HazelSort/Cleverbridge Rechnung 2016-05-08 Paragon NTFS Mac 14.pdf.
2017-01-10 08:08:29.246 hazelworker[64434] Shellscript exited with non-successful status code: 1
2017-01-10 08:08:29.247 hazelworker[64434] Cleverbridge Rechnung 2016-11-22 Parallels Desktop 12.pdf: Rule Diverse C 2016 matched.
2017-01-10 08:08:29.405 hazelworker[64434] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/DevonThink-Script-C-2016-neu.sh on file /Users/MC/Documents/HazelAutomation/2.HazelSort/Cleverbridge Rechnung 2016-11-22 Parallels Desktop 12.pdf.
AbwehrchefVC
 
Posts: 8
Joined: Sun Jan 08, 2017 9:46 am

Hej Markus,

try this modified Code:
Code: Select all
#!/bin/sh

# This script moves files of to DevonThink folders automatically
# based on the $Matches variable.
#
# It looks for a match in the PDF File Name, and moves the file to
# the corresponding DevonTHINK Folder Name.
#
# If it does not find a match, it copies the file to the Inbox.
#
# In either case, it moves the file to the Backup folder
#
# use this pattern to match files:  PDFFileName|DevonTHINKFolderName


Matches="CCL|/2016/Diverse/C
Cleverbridge|/2016/Diverse/C
CSL|/2016/Diverse/C
Cyberport|/2016/Diverse/C
EEEE|/Finance/EEEE
"

IFS=$'\n' read -rd '' -a MatchNames <<<"$Matches"

FullPath=`echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"`
NewName=`echo "$(basename "$1")" | sed "s/.[pP][dD][fF]//"`

echo $FullPath
echo $NewName

echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

j=0
for i in "${MatchNames[@]}"
do
      IFS=$'|' read -rd '' -a GroupNames <<<"${MatchNames[$j]}"

      MatchString=${GroupNames[0]}
      FolderName=$(echo ${GroupNames[1]})  #use this to remove trailing new line character

      echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
#      echo $j "["$MatchString"]"
      echo "$1" | grep -q "$MatchString"
      greprc=$?
      if [[ $greprc -eq 0 ]] ; then
         echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
#         echo "Found:[$MatchString] in [$1] moving to [$FolderName]"
         Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/MC/DevonThink/Test.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
            set theRecord to import "'$FullPath'" to theGroup\n
            set the name of theRecord to "'$NewName'"\n
         end tell\n
         '
         echo $Command  >> ~/HazelAutomation/log.txt
         echo $Command | osascript
         mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
         exit
      fi
      let "j++"
done
echo "No match found for $1"   >> ~/HazelAutomation/log.txt
cp "$FullPath" "/Users/MC/Library/Application Support/DEVONthink Pro 2/Inbox"
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit




Feedback is welcome....

Best
Rolf
Happy wife .... Happy life... :D
HaubenTaucher
 
Posts: 15
Joined: Sun Oct 23, 2016 8:23 am
Location: Germany / OWL

no way - unfortunately still only copy and this causes the continuous loop
AbwehrchefVC
 
Posts: 8
Joined: Sun Jan 08, 2017 9:46 am

did you observed that:

Line 51: "/Users/me/DevonTHINK/MyScans.dtBase2” to the unix path of your DevonTHINK database that you want to store. In DevonTHINK, right click on the Database you want to use, click on “Show in Finder”. Copy the path of that file onto line 51 replacing the text I have.

Is the path really right?
Happy wife .... Happy life... :D
HaubenTaucher
 
Posts: 15
Joined: Sun Oct 23, 2016 8:23 am
Location: Germany / OWL

Hi Rolf, yes I did:

set theDatabase to open database "/Users/MC/DevonThink/Test.dtBase2"\n
set theGroup to create location "'$FolderName'" in theDatabase\n
set theRecord to import "'$FullPath'" to theGroup\n
set the name of theRecord to "'$NewName'"\n

I have tested a little bit. After I have adapted the filenames and matches, the documents end up exactly in the folder in DT, in which they should land.

But still the documents are only copied and not moved. In addition, Hazel reports an error message by the script. Here is the log:

2017-01-11 20:22:21.662 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:21.663 hazelworker[20411] Commerzbank Silvi - Auszug Nr. 2016-12.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:21.829 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/Commerzbank Silvi - Auszug Nr. 2016-12.pdf.
2017-01-11 20:22:21.830 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:21.831 hazelworker[20411] DB 00 - Auszug Nr. 2016-11.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:21.984 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/DB 00 - Auszug Nr. 2016-11.pdf.
2017-01-11 20:22:21.985 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:21.986 hazelworker[20411] DB 00 - Auszug Nr. 2016-12.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:22.153 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/DB 00 - Auszug Nr. 2016-12.pdf.
2017-01-11 20:22:22.153 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:22.154 hazelworker[20411] DB 01 - Auszug Nr. 2016-12-1.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:22.387 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/DB 01 - Auszug Nr. 2016-12-1.pdf.
2017-01-11 20:22:22.387 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:22.388 hazelworker[20411] DB 01 - Auszug Nr. 2016-12.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:22.575 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/DB 01 - Auszug Nr. 2016-12.pdf.
2017-01-11 20:22:22.575 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:22.577 hazelworker[20411] Finanzamt Umsatzsteuer 2016-11.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:22.720 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/Finanzamt Umsatzsteuer 2016-11.pdf.
2017-01-11 20:22:22.720 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:22.721 hazelworker[20411] Foto Rechnung Amazon 2017-01-03 Fujifilm X-T2 Gehäuse.pdf: Rule Import DevonTHINK matched.
2017-01-11 20:22:22.869 hazelworker[20411] [Error] Shell script failed: Error processing shell script /Users/MC/Documents/Scripte DevonTHINK/test-neu.sh on file /Users/MC/Documents/HazelAutomation/3.HazelProcessed/Foto Rechnung Amazon 2017-01-03 Fujifilm X-T2 Gehäuse.pdf.
2017-01-11 20:22:22.869 hazelworker[20411] Shellscript exited with non-successful status code: 1
2017-01-11 20:22:22.877 hazelworker[20411] Received abort event.
2017-01-11 20:22:22.878 hazelworker[20411] Done processing folder 3.HazelProcessed


the document is moved with the part of the code, or? Can someone explain to why the documents are only copied?

Code: Select all
 if [[ $greprc -eq 0 ]] ; then
         echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
#         echo "Found:[$MatchString] in [$1] moving to [$FolderName]"
         Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/MC/DevonThink/Test.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
            set theRecord to import "'$FullPath'" to theGroup\n
            set the name of theRecord to "'$NewName'"\n
         end tell\n


As well, the file is not created ~/HazelAutomation/log.txt. No files are moved in this folder ~ / HazelAutomation/4.HazelBackup/.

many greetings
Markus
AbwehrchefVC
 
Posts: 8
Joined: Sun Jan 08, 2017 9:46 am

Hi, i'm not very experienced with scripting and I'm having issues with the script which is to move the file to DevonThink.

I've reviewed my code against the ones on here and didn't see any obvious errors. Thanks in advance.
Here is my code:
Matches="Citibank|Bills/Citibank"

IFS=$'\n' read -rd '' -a MatchNames <<<"$Matches"

FullPath=`echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"`

echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

j=0
for i in "${MatchNames[@]}"
do
IFS=$'|' read -rd '' -a GroupNames <<<"${MatchNames[$j]}"

MatchString=${GroupNames[0]}
FolderName=$(echo ${GroupNames[1]}) #use this to remove trailing new line character

echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
echo "$1" | grep -q "$MatchString"
greprc=$?
if [[ $greprc -eq 0 ]] ; then
echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
Command='
tell application id "com.devon-technologies.thinkpro2"\n
launch\n
set theDatabase to open database "/Users/Michael/Documents/DevonThink/test.dtBase2"\n
set theGroup to create location "'$FolderName'" in theDatabase\n
import "'$FullPath'" to theGroup\n
end tell\n
'
echo $Command >> ~/HazelAutomation/log.txt
echo $Command | osascript
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit
fi
let "j++"
done
echo "No match found for $1" >> ~/HazelAutomation/log.txt
cp "$FullPath" "/Users/Michael/Library/Application Support/DEVONthink Pro 2/Inbox/."
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit

The following is the log file created:
Looking in [/Users/Michael/HazelAutomation/3.HazelProcessed/2017_01_16_10_38_01-Citibank.pdf] for:
0 [Citibank]
Found:[Citibank] in [/Users/Michael/HazelAutomation/3.HazelProcessed/2017_01_16_10_38_01-Citibank.pdf] moving to [Bills/Citibank]
tell application id "com.devon-technologies.thinkpro2"\n launch\n set theDatabase to open database "/Users/Michael/Documents/DevonThink/test.dtBase2"\n set theGroup to create location "Bills/Citibank" in theDatabase\n import "/Users/Michael/HazelAutomation/3.HazelProcessed/2017_01_16_10_38_01-Citibank.pdf" to theGroup\n end tell\n
trivan
 
Posts: 1
Joined: Thu Feb 09, 2017 1:09 pm

Hi Rolf,

Thanks for this code! I hope you might have some advice for me. My problem is that the script runs without error, and if it is simply importing into the global inbox, that works fine. But it seems I am having trouble with the part that tells DEVONthink to import the file into a specific folder. As I said, there are no errors, and the log file is updated, but the file doesn't show up in the specified database anywhere.

I am moderately proficient with shell scripts and Hazel rules, but less so with AppleScript, which I am guessing that part of the script is. Here is the log output:

Code: Select all
Looking in [/Users/tamara/Documents/Finance/Processing/Import/Receipt/Amazon Tam 2017-02-03 2.pdf] for:
0 [Receipt]
Found:[Receipt] in [/Users/tamara/Documents/Finance/Processing/Import/Receipt/Amazon Tam 2017-02-03 2.pdf] moving to [/Receipts]
tell application id "com.devon-technologies.thinkpro2"\n launch\n set theDatabase to open database "/Users/tamara/Documents/Finance/Finance.dtBase2"\n set theGroup to create location "/Receipts" in theDatabase\n import "/Users/tamara/Documents/Finance/Processing/Import/Receipt/Amazon Tam 2017-02-03 2.pdf" to theGroup\n end tell\n


And here is the modified version of the shell script I have in Hazel:

Code: Select all
#!/bin/sh

# This script moves files of to DevonThink folders automatically
# based on the $Matches variable.
#
# It looks for a match in the PDF File Name, and moves the file to
# the corresponding DevonTHINK Folder Name.
#
# If it does not find a match, it copies the file to the Inbox.
#
# In either case, it moves the file to the Backup folder
#
# use this pattern to match files:  PDFFileName|DevonTHINKFolderName

Matches="Receipt|/Receipts"

IFS=$'\n' read -rd '' -a MatchNames <<<"$Matches"

FullPath=`echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"`

echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

j=0
for i in "${MatchNames[@]}"
do
      IFS=$'|' read -rd '' -a GroupNames <<<"${MatchNames[$j]}"

      MatchString=${GroupNames[0]}
      FolderName=$(echo ${GroupNames[1]})  #use this to remove trailing new line character

      echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
      echo "$1" | grep -q "$MatchString"
      greprc=$?
      if [[ $greprc -eq 0 ]] ; then
         echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
         Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/tamara/Documents/Finance/Finance.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
            import "'$FullPath'" to theGroup\n
         end tell\n
         '
         echo $Command  >> ~/HazelAutomation/log.txt
         echo $Command | osascript
         mv "$FullPath" ~/Documents/Finance/Processing/Complete/.
         exit
      fi
      let "j++"
done
echo "No match found for $1"   >> ~/HazelAutomation/log.txt
cp "$FullPath" "/Users/tamara/Library/Application Support/DEVONthink Pro 2/Inbox/."
mv "$FullPath" ~/Documents/Finance/Processing/Complete/.
exit


I made the necessary changes for my folder and database locations. I left the log file location as is, since it was referenced in multiple locations in the script, but I created the HazelAutomation folder and the log file was written there.

I found I could use subfolders in my "Import" folder to give the file location because I didn't want to put that part in the file name. So for instance, I have a folder ~/Documents/Finance/Import/Receipt and the Hazel rule monitoring "Import" is set to process subfolders. When the shell script looks at the whole file name, it matches the directory and sends it to the right group in the database (or at least so the log file claims).

Thanks for any help you can offer,

Tamara
tamarax
 
Posts: 9
Joined: Thu Mar 01, 2012 11:46 pm

Hi,

I had the same problem too. Finally I solved this, by changing in Hazel -> edit script -> Shell: from "/bin/bash" (how it is on one of the screenshots in first post) to "/bin/sh"
Michal
 
Posts: 1
Joined: Sun Apr 16, 2017 9:18 pm

Hi,

first of all: thanks for this great tutorial. It has allowed me to start digging into both Hazel and AppleScript, which has been fun and informative. (I was even able to get the OCR process to work with a different OCR program that I scripted myself.)

However, I am running into the same problem as "Abwehrchef": my file is copied into the right folder in DevonThink but it is then not moved to the folder "4.HazelBackup" and thus keeps getting copied into DevonThink over and over again.

Have any of you found the reason for this strange behavior? -- It'd be great if you could help me out.

Thanks a lot.
Tim

AbwehrchefVC wrote:Good morning from Germany,

meanwhile I have come a little further. First I have solved my requirement with the year so that I have created a Hazel rule with the year (name contains 2016).
Then I changed the script accordingly

Matches="CCL|/2016/Diverse/C
Cleverbridge|/2016/Diverse/C
CSL|/2016/Diverse/C
Cyberport|/2016/Diverse/C
"


It is still not perfect, but for the beginning already quite well. Automation would naturally be the highlight.

My biggest problem, however, is that the files are not moved. They are simply copied.
And that leads to a continuous loop. The files are copied permanently, so I have these 4-5 times in Devonthink before I can pause Hazel. :oops: :shock:

The code for the script I just copied and made my changes for the paths.
Or is there a mistake?

Please, I need some help to solve this problem. Here is my script

Code: Select all
#!/bin/sh

# This script moves files of to DevonThink folders automatically
# based on the $Matches variable.
#
# It looks for a match in the PDF File Name, and moves the file to
# the corresponding DevonTHINK Folder Name.
#
# If it does not find a match, it copies the file to the Inbox.
#
# In either case, it moves the file to the Backup folder
#
# use this pattern to match files:  PDFFileName|DevonTHINKFolderName

Matches="CCL|/2016/Diverse/C
Cleverbridge|/2016/Diverse/C
CSL|/2016/Diverse/C
Cyberport|/2016/Diverse/C
"

IFS=$'\n' read -rd '' -a MatchNames <<<"$Matches"

FullPath=`echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"`
NewName=`echo "$(basename "$1")" | sed "s/.[pP][dD][fF]//"`

echo $FullPath
echo $NewName

echo `date` > ~/HazelAutomation/log.txt
echo "Looking in [$1] for:" > ~/HazelAutomation/log.txt

j=0
for i in "${MatchNames[@]}"
do
      IFS=$'|' read -rd '' -a GroupNames <<<"${MatchNames[$j]}"

      MatchString=${GroupNames[0]}
      FolderName=$(echo ${GroupNames[1]})  #use this to remove trailing new line character

      echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
#      echo $j "["$MatchString"]"
      echo "$1" | grep -q "$MatchString"
      greprc=$?
      if [[ $greprc -eq 0 ]] ; then
         echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
#         echo "Found:[$MatchString] in [$1] moving to [$FolderName]"
         Command='
         tell application id "com.devon-technologies.thinkpro2"\n
            launch\n
            set theDatabase to open database "/Users/MC/DevonThink/Test.dtBase2"\n
            set theGroup to create location "'$FolderName'" in theDatabase\n
            set theRecord to import "'$FullPath'" to theGroup\n
            set the name of theRecord to "'$NewName'"\n
         end tell\n
         '
         echo $Command  >> ~/HazelAutomation/log.txt
         echo $Command | osascript
         mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
         exit
      fi
      let "j++"
done
echo "No match found for $1"   >> ~/HazelAutomation/log.txt
cp "$FullPath" "/Users/MC/Library/Application Support/DEVONthink Pro 2/Inbox.dtBase2"
mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
exit


Thanks, Markus
HazelBeginner
 
Posts: 2
Joined: Mon Apr 24, 2017 10:37 pm

Hi all,

just wanted to clear up that I figured out where I had gone wrong. I didn't include the "/PDFs" part in my path in the folder that Hazel should move the files to in the end. This meant that Hazel tried to move to a path that didn't exist and therefore got an error. That also meant that it would add the files to DevonThink over and over again.

Now that is fixed and I am a very happy camper.

Cheers!
Tim

HazelBeginner wrote:Hi,

first of all: thanks for this great tutorial. It has allowed me to start digging into both Hazel and AppleScript, which has been fun and informative. (I was even able to get the OCR process to work with a different OCR program that I scripted myself.)

However, I am running into the same problem as "Abwehrchef": my file is copied into the right folder in DevonThink but it is then not moved to the folder "4.HazelBackup" and thus keeps getting copied into DevonThink over and over again.

Have any of you found the reason for this strange behavior? -- It'd be great if you could help me out.

Thanks a lot.
Tim
HazelBeginner
 
Posts: 2
Joined: Mon Apr 24, 2017 10:37 pm

PreviousNext

Return to Tips & Tricks - DO NOT POST QUESTIONS

cron