Page 1 of 1

Can't get xattr to work inside Hazel shell script

PostPosted: Fri Mar 23, 2012 12:19 pm
by tamarax
I'm trying to strip the quarantine flag from certain files using Hazel. I tried

Code: Select all
xattr -d com.apple.quarantine "$1"


in the shell script action and I kept getting "Error processing shell script" with no additional information. All the log says is "2012-03-23 11:56:48.700 hazelworker[26761] [Error] Shell script failed: Error processing shell script on file /Users/Shared/Documents/Finance/Data/Bank Downloads/20120323114957-onlineeast3.bankofamerica.com.qfx."

So I found another thread on here where someone was using xattr, and you gave them this script:

Code: Select all
attr="com.apple.quarantine"
output=`xattr "$1" | grep "$attr"`

if [ -n "$output" ]; then
    xattr -d "$attr" "$1"
fi


Using that, the error is gone and the script finishes, but the flag is still not removed. I don't understand why, because if I run the "xattr -d com.apple.quarantine" command with the file name in Terminal, it works fine.

Any help you could give would be appreciated. I'm running Hazel 3.0.3 on OS X 10.7.3, Hazel is only installed as my user.

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Fri Mar 23, 2012 1:23 pm
by a_freyer
It could be your filetype, else the quotations around $1. Does the command strip quarantine in terminal?

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Fri Mar 23, 2012 2:18 pm
by Mr_Noodle
I believe xattr will produce an error if you try and remove an xattr that doesn't exist. The second form is correct because it checks first that it exists before trying to remove it.

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Fri Mar 23, 2012 3:35 pm
by tamarax
I don't think it's the file type or the quotes. It works in the command line in Terminal, just not in Hazel.

Code: Select all
inara:Bank Downloads tamara$ xattr "/Users/Shared/Documents/Finance/Data/Bank Downloads/20120323114957-onlineeast3.bankofamerica.com.qfx"
com.apple.FinderInfo
com.apple.metadata:kMDItemDownloadedDate
com.apple.metadata:kMDItemWhereFroms
com.apple.metadata:kMDLabel_veyixt7jjffe3g7nfailqcbxny
com.apple.quarantine

inara:Bank Downloads tamara$ xattr -d com.apple.quarantine "/Users/Shared/Documents/Finance/Data/Bank Downloads/20120323114957-onlineeast3.bankofamerica.com.qfx"

inara:Bank Downloads tamara$ xattr "/Users/Shared/Documents/Finance/Data/Bank Downloads/20120323114957-onlineeast3.bankofamerica.com.qfx" com.apple.FinderInfo
com.apple.metadata:kMDItemDownloadedDate
com.apple.metadata:kMDItemWhereFroms
com.apple.metadata:kMDLabel_veyixt7jjffe3g7nfailqcbxny


It's there, then I run the command, then I check again and it's gone. That's all in Terminal.

But if Hazel processes it, then when I check, the attribute is still there, and if I open the file, it will give me the message about it being downloaded.

If I put just the "xattr -d com.apple.quarantine $1" command in the Hazel rule, I get an error that the file can't be processed. If I use the longer snippet, then the script doesn't error, but the attribute is not removed.

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Mon Mar 26, 2012 1:51 pm
by Mr_Noodle
As I mentioned before, do not bother with the first form of the script. It is clouding the issue. It will return an error if the xattr is not there.

What happens if you run the 2nd form of the script from terminal?

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Mon Mar 26, 2012 3:53 pm
by tamarax
I put the code into a file and ran it as a shell script from Terminal, passing the file name through the command line. It did successfully remove the quarantine flag.

Code used in the shell script was exactly:

Code: Select all
#!/bin/sh

attr="com.apple.quarantine"
output=`xattr "$1" | grep "$attr"`

if [ -n "$output" ]; then
    xattr -d "$attr" "$1"
fi

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Mon Mar 26, 2012 4:32 pm
by Mr_Noodle
Try the following:

Code: Select all
#!/bin/sh

attr="com.apple.quarantine"
output=`xattr "$1" | grep "$attr"`

if [ -n "$output" ]; then
    xattr -d "$attr" "$1"
else
    echo "*****************COULD NOT FIND XATTR $attr FOR FILE $1*****************"
fi


If Hazel executes the script without error, then check the logs, both the Hazel ones as well as the regular console (system.log) for the "COULD NOT FIND" error.

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Mon Mar 26, 2012 5:04 pm
by a_freyer
Since we know that the original single-line script works fine, you could just do this:

Code: Select all
xattr -d com.apple.quarantine "$1" &


or

Code: Select all
xattr -d com.apple.quarantine "$1" >/dev/null 2>&1


The first will fork the process and not return the error to Hazel.
The second will spit errors to /dev/null

You should only do these in circumstances when you want to ignore the error, regardless of what it is.

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Tue Mar 27, 2012 6:55 pm
by tamarax
Sheesh. Before I tried your most recent suggestions, I just tried again with the same script as yesterday, absolutely no changes made in Hazel, and it worked fine this time. The only thing I've done differently was restart the computer today. Very odd, I don't know what would have caused Hazel to not be able to carry out the xattr -d command, but it seems to be working now.

Thanks for the help!

Re: Can't get xattr to work inside Hazel shell script

PostPosted: Wed Mar 28, 2012 2:53 pm
by Mr_Noodle
I guess I'll chalk it up to some odd fluke. Let me know if it starts acting up again.