The script is written to operate in Hazel. The value of the variable theFile in the second line of the script is provided by Hazel when Hazel executes the rule where the script is stored. It’s not a standalone script to be run inside DEVONthink or from ScriptEditor, etc. So, if you getting a path error, it’s possibly a Hazel issue, or because you tried to run the script outside of Hazel. (It would be unusual for Hazel to get a file path wrong.)
The script will not work with DEVONthink 3 without changing two lines:
tell application id "com.devon-technologies.thinkpro2" to activate
should be
tell application id "DNtp" to activate
and
tell application id "com.devon-technologies.thinkpro2"
And, of course, you need the UUID of the targeted group for the hardcoded destination that makes this script work.
set theGroup to get record with uuid "<group uuid>"
Generally, hardcoding destinations is “a bad idea” but in this case the fixed destination is the point of the script as it operates in Hazel, which itself frequently deals with fixed destinations.
I did originally run this from within Hazel and was having issues which then lead me to run it directly from script editor to try and troubleshoot. But as you’ve suggested I’ve reverted back to running within Hazel and I am getting the following errors appear:
The following is the exact script I have embedded into my hazel rule:
tell application "Finder"
set _path to (the POSIX path of theFile as string)
set {_name, _extension} to {name, name extension} of theFile
end tell
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
tell application id "com.devon-technologies.thinkpro2"
set theGroup to get record with uuid "D95C7EBA-FB02-43AA-9115-163425413560"
set theImport to import _path name _name to theGroup
end tell
I am still using DevonThink Office Pro 2.
And for completeness, below is the Hazel rule I have in place:
You might also want to add to your Hazel rule. You can make one of the conditions that the item is not labelled “Blue” (for example) and then make the last action of the rule that it applies the label “Blue” to the item. That prevents a loop, because anything that has been processed once is marked in such a way that it does not match the opening criteria. When you have things working you can make the final action the Trashing of the item.
I use the same sort of workflow successfully in Hazel and DTPO2. I checked my script, and it appears almost the same as yours. However, there are a few small differences, and I don’t know if there is a subtle difference that might help. Good luck!
tell application "Finder"
set _path to (the POSIX path of theFile as string)
set {_name, _extension} to {name, name extension} of theFile
-- optional: hide extension in Finder
set extension hidden of theFile to true
-- optional: remove extension for DEVONthink displays
set _name to text 1 thru -((count _extension) + 2) of _name
set _extension to "." & _extension
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 theGroup to get record with uuid "7A72BA27-C302-4362-8B25-01ACEC1C1F9A"
-- choose one option, Index or Import, comment out the other
set theImport to import _path name _name to theGroup
-- set theIndex to indicate _path to theGroup
end tell
Also, this code block is unnecessary though not necessarily incorrect)…
-- 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
Sending a command to DEVONthink will launch it.
Lastly, note the activate command will bring DEVONthink to the front.
You can use launch to run a command without bringing the app to the front, though that’s actually not necessary here. launch is typically used to open an app without switching from your current one, but would be unneeded here. However, some apps may require it, as needed.
I was working with this script in a combination of hazel and DT3 for a while but at the moment the script is not moving the files into the desired groups anymore. Instead it moves the files into the inbox of the specific databases. To change the uuids doesn’t bring a difference since the script will just move the files into the inboxes.
I am wondering what I am doing wrong since the script was working well in DT3. Is it due to a recent update?
thank you very much for your help. I am not sure what it was but the last files today were again sorted correctly into the specific group in hazel. I will come back if the flaw is occurring again and am very grateful for the support that you offer so well.
I assume the handling of the file will be slightly different as an external script, but I wasn’t bothered enough to update the script to do so, choosing to save and sync it inside of my Hazel rules instead.