Skip to content

Instantly share code, notes, and snippets.

@brunerd brunerd/maclTackle.command
Last active Mar 29, 2020

Embed
What would you like to do?
A hacky example to clean the com.apple.macl attribute from a file using zip to sidestep SIP on Catalina
#!/bin/bash
#clean the com.apple.macl attribute from a file or folders using zip to sidestep SIP on Catalina
#WARNING: This will overwrite the original file/folders with the zipped version - DO NOT use on production data
#hold down command key at launch or touch /tmp/debug to enable xtrace command expansion
commandKeyDown=$(/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSCommandKeyMask > 1')
[ "$commandKeyDown" = "True" -o -f /tmp/debug ] && set -x && xtraceFlag=1
#hacky example to clean the com.apple.macl attribute from a file using zip to sidestep SIP
: <<-EOL
MIT License
Copyright (c) 2020 Joel Bruner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOL
#############
# VARIABLES #
#############
target="${1}"
OSVersionString=$(sw_vers -productVersion)
minorVersion=$(cut -d. -f2 <<< $OSVersionString)
#############
# FUNCTIONS #
#############
function maclZipClean
{
local target="${1}"
local randomeZipName="maclClean-${RANDOM}.zip"
#separate the filename from the folder
local fileName="$(basename "$target")"
local fileFolder="$(dirname "$target")"
#go into the folder - this method avoid the parent folders above
cd "$fileFolder"
#make a zip in the home folder (the only guaranteed folder we have access to) of the filename
zip -r ~/"$randomeZipName" "$fileName"
local exitCode=$?
#if something goes wrong bail
if [ "${exitCode}" -ne 0 ]; then
echo "Something went wrong zipping $fileName (${exitCode}), exiting."
exit
fi
#otherwise remove the target we zipped
rm -rf "$target"
#unzip the zip file with the cleaned file(s) inside, overwriting/updating
unzip -u ~/"$randomeZipName" -d "$fileFolder"
#remove the temp zip
rm ~/"$randomeZipName"
}
function maclCleanXattr
{
local target="${1}"
#perhaps you are running this script on 10.14 and under, why do it the hard way then?!
xattr -rd com.apple.macl "$target"
}
########
# MAIN #
########
#just in case we can do it the easy way
if [ "${minorVersion}" -le 14 ]; then
maclCleanXattr "${target}"
#otherwise we'll do our nifty zip way
else
maclZipClean "${target}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.