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