Skip to content

Instantly share code, notes, and snippets.

@julianpitt
Last active July 10, 2019 00:05
Show Gist options
  • Save julianpitt/2b2df29ec96b3b058b307effdee65ebd to your computer and use it in GitHub Desktop.
Save julianpitt/2b2df29ec96b3b058b307effdee65ebd to your computer and use it in GitHub Desktop.
[Take ownership quick action in mac] #automation
# Create a Take Ownership button in the mac right click menu in Finder under "Quick Actions"
# To create the automator script
# Open Automator by going to Finder -> Applications -> Automator.app
# Click "New Document"
# Select "Quick Action" then click "Choose"
# In the right panel make sure the following is selected:
# -> Workflow receives current "files or folders" in "Finder.app"
# In the left panel double click on Library -> Utilities -> Run Shell Script
# this should add a Run Shell Script block in the right window.
# Make sure the Pass input box is set to "as arguments"
# Paste the following into the command box
#================================================
## Get the current logged in username
USER_NAME=$(printf '%s' "${SUDO_USER:-$USER}");
## Join all the inputted files by a space and single quotes and save to a variable
##https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
FILES_AND_FOLDERS=$(join_by "' '" "${@}")
## Run the Chown command with admin privileges on the files and folders
osascript -e "do shell script \"chown -R $USER_NAME '$FILES_AND_FOLDERS'\" with administrator privileges"
#================================================
# Save the file as "Take Ownership" and celebrate
# test it out by opening the finder and right clicking on a directory, then go to Quick actions and select the Take Ownership action if that's what you named the workflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment