Remove obj and bin folders via OS X Finder
I frequently find myself wanting to delete the bin and obj folders from my projects while developing in Xamarin on OS X. I usually do this by just deleting them manually via Finder. But there's too many clicks involved and too much room for error.
So, why not use OS X Automator to create a Finder context menu item to do it for you?
-
Use Spotlight to open Automator:
-
Select "Services" document type:
-
Set "Services receives selected" dropdown to "folders":
-
Set "in" dropdown to "Finder":
-
Locate the "Utilities" group under "Library" in the Actions in the leftmost pane, and locate "Run Shell Script" in the next pane:
-
Drag "Run Shell Script" to the rightmost pane:
-
Set the "Pass input" dropdown to "as arguments" (IMPORTANT!), and enter the following script into the Run Shell Script action, like so:
The script contents:
#!/bin/bash
# Constrain the allowable path in which to execute the actions.
# Assumes that your projects reside in ~/Projects
constrainedPath="$HOME/Projects"
if [ -z $1 ] ; then
exit 1
elif [[ $1 != *$constrainedPath* ]]; then
echo "Invalid path. The allowable path is constrained to $constrainedPath (and subdirectories) for safety"
exit 1
else
foldersToRemove=$(find $1 -type d \( -name bin -o -name obj \))
if [[ $foldersToRemove != "" ]]; then
echo "Removing folders:"
echo "$foldersToRemove"
rm -rf `find $1 -type d \( -name bin -o -name obj \)`
else
echo "No bin or obj folders to remove"
fi
fi
The script assumes that your projects reside in ~/Projects
. You can change this by modifying the constrainedPath
variable to suit your needs. This is just to prevent accidental deletion on bin folders in locations other than your projects folder.
If you like, you may remove all of the echo
lines and comments. They only exist for explanation and troubleshooting in the terminal.
#!/bin/bash
constrainedPath="$HOME/Projects"
if [ -z $1 ] ; then
exit 1
elif [[ $1 != *$constrainedPath* ]]; then
exit 1
else
foldersToRemove=$(find $1 -type d \( -name bin -o -name obj \))
if [[ $foldersToRemove != "" ]]; then
rm -rf `find $1 -type d \( -name bin -o -name obj \)`
fi
fi
8. Then save with ⌘ + S and name it something like "Remove bin and obj folders".
9. Now, when you right-click on any folder in Finder, under the "Services" context menu, you'll see "Remove bin and obj folders". Clicking it will recursively remove all "bin" and "obj" folders from the selected folder:
![](/content/images/2016/02/FinderContext.png)
Be sure to only use this in code project folders! Don't use it in any folders in which you don't want to delete the child bin or obj folders.
Enjoy.