Help with AppleScript Action ...

I am hoping for some help with an AppleScript action problem.
The (brief) scenario is that I am looking to move a file into a subfolder based on the value of its Finder tags. If the file has, for instance, a tag "DTPersonal" then it would be moved to a subfolder called "DTPersonal". The "DT" part I use as an identifier to grab the tag that is used for the subfolder selection. There are multiple tags starting with DT and corresponding subfolders. (I could of course accomplish this with multiple rules each testing for a single "DT" tag, but the idea is to handle all this with one rule so if I decide to add more tags of this format, the rule just works automatically.)
The tests to find files with a tag of the right format work fine.
I use as the first action the following AppleScript action:
set theTags to do shell script "mdls -raw -name kMDItemUserTags '" & theFile & "' | grep -m 1 '^ *DT' | sed -e 's/^ *//' -e 's/,$//'"
return {hazelStop:false, hazelOutputAttributes:{theTags}}
I think the intent is clear; use mdls to grab the tabs; grep for the first line that starts with <spaces>DT; use sed to strip off any leading spaces and trailing commas, and set theTags to the results.
My problem is that the output result is always blank; I never get the tag back. I have confirmed this with the logs in DEBUG mode; hazelOutputAttributes is getting a blank string.
If I modify the script:
set theTags to do shell script "mdls -raw -name kMDItemUserTags '" & theFile & "' | grep -m 1 '^ *DT' | sed -e 's/^ *//' -e 's/,$//'"
set theTags to "FOO"
return {hazelStop:false, hazelOutputAttributes:{theTags}}
I will in fact get "FOO" in the output parameter, so I know that I am doing that part correctly, The problem seems to be in the result of the first line, setting the output of the shell script into variable theTags.
If I run the same script in AppleScript Editor (first setting theFile to the correct path for the file I am using for test purposes), I get the following in the results window:
tell current application
do shell script "mdls -raw -name kMDItemUserTags '/Users/nl/Documents/DevonThink/DTInbox/test copy' | grep -m 1 '^ *DT' | sed -e 's/^ *//' -e 's/,$//'"
end tell
Result:
{hazelStop:false, hazelOutputAttributes:{"DTPersonal"}}
and so it looks to work fine in the AppleScript Editor.
I can only assume I am missing something, but I cannot figure out what.
Any help would be appreciated.
Thanks.
The (brief) scenario is that I am looking to move a file into a subfolder based on the value of its Finder tags. If the file has, for instance, a tag "DTPersonal" then it would be moved to a subfolder called "DTPersonal". The "DT" part I use as an identifier to grab the tag that is used for the subfolder selection. There are multiple tags starting with DT and corresponding subfolders. (I could of course accomplish this with multiple rules each testing for a single "DT" tag, but the idea is to handle all this with one rule so if I decide to add more tags of this format, the rule just works automatically.)
The tests to find files with a tag of the right format work fine.
I use as the first action the following AppleScript action:
set theTags to do shell script "mdls -raw -name kMDItemUserTags '" & theFile & "' | grep -m 1 '^ *DT' | sed -e 's/^ *//' -e 's/,$//'"
return {hazelStop:false, hazelOutputAttributes:{theTags}}
I think the intent is clear; use mdls to grab the tabs; grep for the first line that starts with <spaces>DT; use sed to strip off any leading spaces and trailing commas, and set theTags to the results.
My problem is that the output result is always blank; I never get the tag back. I have confirmed this with the logs in DEBUG mode; hazelOutputAttributes is getting a blank string.
If I modify the script:
set theTags to do shell script "mdls -raw -name kMDItemUserTags '" & theFile & "' | grep -m 1 '^ *DT' | sed -e 's/^ *//' -e 's/,$//'"
set theTags to "FOO"
return {hazelStop:false, hazelOutputAttributes:{theTags}}
I will in fact get "FOO" in the output parameter, so I know that I am doing that part correctly, The problem seems to be in the result of the first line, setting the output of the shell script into variable theTags.
If I run the same script in AppleScript Editor (first setting theFile to the correct path for the file I am using for test purposes), I get the following in the results window:
tell current application
do shell script "mdls -raw -name kMDItemUserTags '/Users/nl/Documents/DevonThink/DTInbox/test copy' | grep -m 1 '^ *DT' | sed -e 's/^ *//' -e 's/,$//'"
end tell
Result:
{hazelStop:false, hazelOutputAttributes:{"DTPersonal"}}
and so it looks to work fine in the AppleScript Editor.
I can only assume I am missing something, but I cannot figure out what.
Any help would be appreciated.
Thanks.