Korm-
Thanks so much for persisting and clarifying this, for those of us who want to incorporate Hazel into our workflow.
Excellent work!
I wanted to add my experience and results. I wanted to download bills, and have them automatically renamed, based on the service provider and date, both of which are used to rename the file.
I had a few issues getting this to work.
First, if I renamed using a date match taken from the bill, I had Hazel include the extension. Extensions are hidden in my Finder, but if you don’t have Hazel add the extension, the file becomes readable by TextEdit, and nothing shows up upon QuickView. So I added some language to the script to hide the file extension:
-- Hide file extension in Finder
tell application "Finder"
set extension hidden of theFile to true
end tell
At first, this didn’t work, because I had it in the wrong place. After some experimentation, I found it had to occur first in the script.
Second, I wanted the file imported into a group in DTPO without an extension. Even though DTPO preferences were set to import without a file extension, I suppose the string for the filename included the extension. So I had to come up with a way to remove the extension:
-- Strip extension for path and file name
tell application "Finder"
-- set theSelection to first item of (get selection)
set {theName, theExtension} to {name, name extension} of theFile
-- theSelection
end tell
if theExtension is not "" then
set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
set theExtension to "." & theExtension
end if
This was set up to work with the rest of Korm’s script.
Third, I wanted to delete the file from Finder after importing it. This is easy with Hazel.
So, Hazel is set up to search the doc for a keyword or two, and then a date. After this, Hazel renames the file based on these two data. Next, Hazel runs the following script, modified slightly from Korm:
-- Hide file extension in Finder
tell application "Finder"
set extension hidden of theFile to true
end tell
-- Strip extension for path and file name
tell application "Finder"
-- set theSelection to first item of (get selection)
set {theName, theExtension} to {name, name extension} of theFile
-- theSelection
end tell
if theExtension is not "" then
set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
set theExtension to "." & theExtension
end if
-- Get file metadata from Finder.
tell application "Finder"
set _path to (the POSIX path of theFile as string)
set _name to theName
-- (the name of theFile as string)
end tell
-- Lauch 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 "/Users/Scott/ Documents/Computer/DEVONthink Pro Office Data/Vanguard Specialty
Imaging.dtBase2"
set theGroup to create location "/Financial/VSI Vendors & Accounts/AT&T Uverse/" in theDatabase
set theImport to import _path name _name to theGroup
-- set theIndex to indicate _path to theGroup
end tell
Finally, Hazel moves the original file to the trash.
I’m quite sure Korm or another AS whiz will be able to clean up my patchwork script, but at least it works right now. Please let me know if there are any questions about the date match or renaming in Hazel.
I’m very pleased with the capabilities of Hazel for this purpose, and will definitely set up all my bills to download, auto-rename, and auto-file.
Cheers!
(Thanks again, Korm)