Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save christopheranderton/0cd24b29ee20483f748b54ec79b0958b to your computer and use it in GitHub Desktop.
Save christopheranderton/0cd24b29ee20483f748b54ec79b0958b to your computer and use it in GitHub Desktop.
Add a ”Strip Metdata” Service to your context menu (Right-Click Menu) in macOS/Mac OS X. This Service uses ExifTool and Tag (both via Homebrew) or xattr.

1. What you need

  • macOS/Mac OS X (tested only on El Capitan, but should work on Sierra and older versions)
  • Xcode Command Line Tools / Xcode
  • Homebrew (http://brew.sh/)
  • Tag (via Homebrew)
  • ExifTool (via Homebrew)

2. Installing Homebrew, Tag and ExifTool

  1. For installing Xcode and Command Line Tools. Please see: http://railsapps.github.io/xcode-command-line-tools.html
  2. Open up your Terminal.app (or iTerm.app)
  3. Follow the instructions at http://brew.sh to install Homebrew in a snap.
  4. Install ExifTool by copy paste: brew install exiftool.
  5. Install Tag by copy paste: brew install tag

Short version:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  
    
    brew install exiftool     
    
    brew install tag

4. Creating the Automator.app Service

  1. Open up Automator.app located in your Applications folder.
  2. Choose ”Service” in the dialog sheet when creating a new document.
  3. In the Services receives selected: menu at the top of the ”Canvas” on the right, change to files and folders and ”in” to Finder.app
  4. In the sidebar to the left, search for Set Spotlight Comments for Finder Items
  5. Drag the Set Spotlight Comments for Finder Item action to the ”canvas” on the right side.
  6. In the text field for the Set Spotlight Comments for Finder Item action, add one single space (for some reason, if left empty, it will not work for what we are doing).
  7. In the sidebar to the left, search for Run Shell Script
  8. Drag the Run Shell Script action to the ”canvas” on the right side.
  9. At the right in the Run Shell Script action, change the Pass input from to stdin to as arguments
  10. Copy Paste this into the action text box (remove existing code in the text box, and change PATH to /usr/local/bin/exiftool if you want):
PATH=/usr/local/bin:/bin export PATH
for f in "$@"
do	
exiftool -overwrite_original -all= "$f"; tag -r "*" "$f"
done

Save the Service into /Users/yourusername/Library/Services. Test it by selcting a file with metadata (Finder Tags, Finder Labels, Camera Metadata, GPS Data, Spotlight Comments and so on) in the Finder and Right-Click and go to end at the Context menu labeled ”Services” and look for the name you gave your Service. If everything works alright, all Metadata stripped.

Breakdown

Line 1: PATH=/usr/local/bin:/bin export PATH
Tells where to look for the exiftool and tag tools.
This may not be necessary depending to your enviroment settings.

Line 4: exiftool -overwrite_original -all= "$f";
Removes all Metadata for Exif and so on and overwrites the original file (be careful!).
The $f is telling the command what file(s) it's working with.
The ; at the end indicates when the command is finished, go ahead and jump to the next command.

Line 4: tag -r "*" "$f"
The tag -r removes all Finder Tags and Labels. The "*" "$f" means all file types and the file(s) choosen.

Alternative way using ExifTool and xattr

Here we use xattr instead of the ”Tag” tool.

  1. Open up Automator.app located in your Applications folder.
  2. Choose ”Service” in the dialog sheet when creating a new document.
  3. In the Services receives selected: menu at the top of the ”Canvas” on the right, change to files and folders and ”in” to Finder.app
  4. In the sidebar to the left, search for Set Spotlight Comments for Finder Items
  5. Drag the Set Spotlight Comments for Finder Item action to the ”canvas” on the right side.
  6. In the text field for the Set Spotlight Comments for Finder Item action, add one single space (for some reason, if left empty, it will not work for what we are doing).
  7. In the sidebar to the left, search for Run Shell Script
  8. Drag the Run Shell Script action to the ”canvas” on the right side.
  9. At the right in the Run Shell Script action, change the Pass input from to stdin to as arguments
  10. Copy Paste this into the action text box (remove existing code in the text box, and change PATH if necessary):
for f in "$@"
do	
/usr/local/bin/exiftool -overwrite_original -all= "$f";
xattr -d com.apple.metadata:_kMDItemUserTags "$f" 2> /dev/null;
xattr -d com.apple.metadata:kMDItemWhereFroms "$f" 2> /dev/null;
xattr -d com.apple.metadata:kMDItemDownloadedDate "$f" 2> /dev/null;
xattr -d com.apple.FinderInfo "$f" 2> /dev/null
done

Breakdown

Line 3: /usr/local/bin/exiftool -overwrite_original -all= "$f"
As in the first script it tells where to look for the exiftool tool. Difference here is we are using the path to the binary instead of using a general path. This may not be necessary depending to your enviroment settings.

Line 4-6: 2> /dev/null when an extended attribute like com.apple.metadata:kMDItemWhereFroms is not present, it will stop the Workflow and outputs a warning message. In the Terminal it will simply output ”No such xattr: com.apple.metadata:kMDItemWhereFroms. But in a Automator Service, it stops the Workflow, so we direct the error message into a black hole which is: 2> /dev/null so the service can continue regardless.

Notes

I have been looking for a way to manipulate Spotlight Comments via the CLI, but the ones i found have failed. Using xattr -d com.apple.metadata:kMDItemFinderComment seems to removes the comment from Spotlight, but not the Finder Info Comment window unless you remove .DS_store, which also removes the metadata for all files in that directory if i understand it correctly. Please let me know if you know a way.

If you find errors or better ways to do things, let me know if there is better practices i could use and so on!

Getting creative with naming the Service. You could use a Emoji or Unicode symbol in the beginning of the file name. While it's not best practice in the * -nix or Windows world (or anywhere else), this is Mac only and for your personal use, also it will not give you any kind of problems (if you send it to someone, do zip the file first!). Example: EMOJI - Vector | SVG 2 EPS . Makes it easier to locate in the sometimes crowded ”Services” Contextual menu.

Example (from my SVG2PNG Service):

screenshot


@am1no
Copy link

am1no commented Jun 22, 2017

To manipulate comments, I use a service written on an Applescript. Here is the code:

tell application "Finder"
	activate
	set fileList to selection
	if (count result) is 0 then
		try
			get (target of front Finder window) as alias
		on error
			choose folder with prompt "Set comments of files in this folder:"
		end try
		
		try
			set theFolder to result
			set fileList to every file of folder (result) as alias list
		end try
	end if
	
	repeat with filelist1 in fileList
		set mycomment to (get comment of filelist1)
	end repeat
	
	display dialog "Comments: \n" & mycomment & " " default answer "" buttons {"Overwrite", "Cancel", "Append"} default button 3 with title "Set Spotlight Comments"
	
	set userInput to the result
	
	set newComment to text returned of userInput
	
	if (button returned of userInput) is "Overwrite" then
		if (class of first item of fileList) is alias then
			set comment of every file of folder theFolder to newComment
		else
			repeat with thisFile in fileList
				set comment of thisFile to newComment
			end repeat
		end if
	else
		repeat with thisFile in fileList
			tell thisFile
				if length of (comment as text) is not 0 then
					get ", " & newComment
				else
					get newComment
				end if
				set comment to (comment & result)
			end tell
		end repeat
	end if
end tell

@christopheranderton
Copy link
Author

@am1no Great! I didn't see your comment until now (i guess i need to check my notification settings!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment