Page 1 of 1

Help with AppleScript Action ...

PostPosted: Tue Jan 16, 2018 10:05 pm
by nlippman
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.

Re: Help with AppleScript Action ...

PostPosted: Wed Jan 17, 2018 12:06 pm
by Mr_Noodle
Is there a reason you don't use a built-in condition instead of a script? I think you can do what you want using "Tags contains match", with a pattern to match "DT" plus whatever.

Re: Help with AppleScript Action ...

PostPosted: Wed Jan 17, 2018 8:33 pm
by nlippman
Thank you.

I changed my rule as you suggested, which does accomplish what I need.

I am still unclear as to why my script did not work, however....

Re: Help with AppleScript Action ...

PostPosted: Thu Jan 18, 2018 11:33 am
by Mr_Noodle
You'd have to do a bit more debugging though looking at it again, maybe it has to do with not using the full path to 'mdls'? Just a thought.