#!/bin/csh -f # xattr_delete # ------------------------------------------------------------------------- # C shell script to clear the specified Mac OS X attribute if it exists # on the specified files. # Returns status code: # 255 = None of the files had the attribute. Nothing was done. # 0 = At least one file had the attribute and it was successfully # deleted # other = Error code returned by xattr -d that caused this script to abort # ------------------------------------------------------------------------- # Revision History: # $Log$ # ------------------------------------------------------------------------- # Collect command line options set option_verbose = "false" set option_prefix_string = "" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Deletes the specified Mac OS X quarantine attribute." echo "Usage: $0:t [options] attribute filename..." echo "Options:" echo " -h = Show this help text" echo " --help = Show this help text" echo " -v = Verbose: show xattr command executed" echo " --verbose = Verbose: show xattr command executed" echo " -p prefix_string = Prefix each output line with prefix_string" echo " --prefix prefix_string = Prefix each output line with prefix_string" echo "Examples:" echo " $0:t com.apple.quarantine *.jpg" echo " $0:t -v com.dropbox.attrs *.pdf" echo " $0:t -v -p 'xattr command executed: ' com.apple.quarantine *.pdf" echo " $0:t -p 'Attr com.dropbox.attrs deleted from file abc.jpg' com.dropbox.attrs abc.jpg" exit 1 else if ("$1" == "-v" || "$1" == "--verbose") then shift set option_verbose = "true" else if ("$1" == "-p" || "$1" == "--prefix") then shift set option_prefix_string = "$1" shift else if ("-" == "`echo $1:q | cut -c 1`") then echo "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first argument break endif end # At least 1 argument required if ($#argv < 2) then echo "Error: Attribute and filename are required." $0 -h exit 1 endif set attribute = $1:q shift # Plan to return 255 (max positive allowable exit status) if nothing is # done, so the caller can detect that @ rc = 255 foreach file ($*:q) xattr_detect $attribute:q "$file:q" if ($status) then # Purposely do nothing and echo no output if attribute not found. # This is useful when called for a large number of files that # mostly don't have the attribute. else echo -n "$option_prefix_string" set option_verbose_string = "-v" if ("$option_verbose" == "false") then # Echo the newline that was omitted by "echo -n" above if # anything at all was echoed. if ("$option_prefix_string" != "") echo "" else # Note: Have to echo explicitly since -v option of xattr seems # to be a no-op. Generates no verbose output. echo "xattr -d $option_verbose_string $attribute:q $file:q" endif xattr -d $option_verbose_string $attribute:q $file:q # Plan to return the status code of xattr so caller can detect that. @ rc = $status # Abort early if xattr reports an error. if ($rc != 0) then echo "${0:t}: Error code $rc returned by xattr when deleting attribute $attribute from file $file:q. Aborting..." break endif endif end exit $rc