Can't get xattr to work inside Hazel shell script

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

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.
tamarax
 
Posts: 13
Joined: Thu Mar 01, 2012 11:46 pm

It could be your filetype, else the quotations around $1. Does the command strip quarantine in terminal?
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

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.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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.
tamarax
 
Posts: 13
Joined: Thu Mar 01, 2012 11:46 pm

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?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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
tamarax
 
Posts: 13
Joined: Thu Mar 01, 2012 11:46 pm

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.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

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!
tamarax
 
Posts: 13
Joined: Thu Mar 01, 2012 11:46 pm

I guess I'll chalk it up to some odd fluke. Let me know if it starts acting up again.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support