Importing via Applescript to Database

Hi,

I am currently trying to use Hazel to run an AppleScript to import files I tag with a # to a specific database. i.e. #stats goes to the inbox of the stats database. However, no matter how I try, it just doesn’t seem to import. Can anyone help me out here? This is my current script. Thank you.

set theFile to quoted form of (POSIX path of (choose file without invisibles)) as string

--GET THE FILE
set the_tags to paragraphs of (do shell script "mdls -raw -name kMDItemUserTags " & theFile & " | sed 's/^[()]$//g' | ruby -ne 'each = $_.strip.end_with?(\",\") ? $_.strip[0...-1] : $_.strip; puts each if each != \"\"'")

repeat with w in every item of the_tags
	if w contains "#" then
		set dbname to first word of w
		tell application id "com.devon-technologies.thinkpro2"
			set theDatabase to open database "/Users/xxxxxxx/Documents/Devonthink/" & dbname & ".dtBase2"
			set theGroup to create location "Inbox" in theDatabase
			import theFile to the current group
		end tell
	end if
end repeat

R

Subscribed. This would be very useful, as far as I can tell.

**That said, I’m fairly certain that over the years I have been pointed to/stumbled across a virtually perfect import system, using one of the many built-in options, that I’ve since gone and forgotten about. Regardless, yours would be a very nice one to add.

Hope you come right - good luck!

What error are you getting?

Remove “quoted form of” from first line, i.e. “set theFile to POSIX …”
the import line will work

Actually, no it won’t work unless there are no spaces to be escaped in the path. My test file on my Desktop has spaces in the filename and fails, as expected.

I believe this is the problem code


set theGroup to create location "Inbox" in theDatabase
import theFile to the current group

Setting “theGroup” to “Inbox” does not change current group to “Inbox”.

You might try


set theGroup to create location "Inbox" in theDatabase
import theFile to theGroup

The rest of the code checks out, as far as I can test it without your environment. (It is clever.)

Regarding spaces in file name, a quick and dirty is changing first line to

set theName to (POSIX path of (…
set theFile to quoted form of theName

and then change the “import” line to

import theName …

I think the first line after the “if-statement” should read
set dbname to first item of the_tags – not of the w

This ended up working when I select the file through finder. However, still can’t do it through hazel when I changed it to theFile instead of (choose file without invisibles). It is very close to working though! This is the current - the file does not get imported for some reason, although it is processed and the database is opened

on hazelProcessFile(theFile, inputAttributes)
	
	set tf to quoted form of (POSIX path of theFile) as string
	
	--GET THE FILE
	set the_tags to paragraphs of (do shell script "mdls -raw -name kMDItemUserTags " & tf & " | sed 's/^[()]$//g' | ruby -ne 'each = $_.strip.end_with?(\",\") ? $_.strip[0...-1] : $_.strip; puts each if each != \"\"'")
	
	repeat with w in every item of the_tags
		if w contains "#" then
			set dbname to first word of w
			tell application id "com.devon-technologies.thinkpro2"
				set theDatabase to open database "/Users/ryanng/Documents/Devonthink/" & dbname & ".dtBase2"
				set theGroup to create location "Inbox" in theDatabase
				import tf to theGroup
			end tell
		end if
	end repeat
	
end hazelProcessFile