Skip to content

Instantly share code, notes, and snippets.

@kergoth
Forked from Zettt/OpenMeta2OSXTags.sh
Last active December 26, 2015 06:39
Show Gist options
  • Save kergoth/7109938 to your computer and use it in GitHub Desktop.
Save kergoth/7109938 to your computer and use it in GitHub Desktop.
#!/bin/bash
# =============================================================
# = OpenMeta to OS X Tags =
# =============================================================
# Script to convert OpenMeta tags to OS X Mavericks tags.
#
# Created by Zettt (Andreas Zeitler) on 2013-06-28
# Source www.macosxscreencasts.com, mosx.tumblr.com
#
# OpenMeta to OS X Tags by Andreas Zeitler is licensed under a
# Creative Commons Attribution-NonCommercial-ShareAlike
# 3.0 Unported License.
# Based on a work at http://www.macosxscreencasts.com.
# Permissions beyond the scope of this license may be
# available at http://www.macosxscreencasts.com.
# =============================================================
# with lots of help from various sources such as stack overflow
# https://groups.google.com/forum/#!topic/openmeta/DK4Of2QGkpM
# http://hints.macworld.com/article.php?story=20110102222441243
# http://bit.ly/12oHSSj
# https://github.com/jakepetroules/wherefrom/blob/master/wherefrom
# Thanks!
# =============================================================
PATH=/usr/local/bin:$PATH
unset a i
while IFS= read -r -d $'\0' file; do
fileArray[i++]="$file"
done < <(mdfind -0 "kMDItemOMUserTags == '*'")
set a i
# this is the plist part that will go before and end of tags
plistFront='<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array>'
plistEnd='</array></plist>'
for currentFile in "${fileArray[@]}"; do
# extract openmeta tags to string
currentTags="$(openmeta -t -p "$currentFile")"
# remove trailing -p path from openmeta
currentTags="${currentTags%% $currentFile}"
# remove trailing whitespace (if any)
currentTags="${currentTags%%\w}"
eval tagArray=($currentTags)
plistTagString=""
for i in "${tagArray[@]}"; do
plistTagString="$plistTagString<string>$i</string>"
done
printf >&2 "Setting tags for '%s' to '%s'\n" "$currentFile" "$currentTags"
xattr -w com.apple.metadata:_kMDItemUserTags "$plistFront$plistTagString$plistEnd" "$currentFile"
done
# Reading OpenMeta tags:
# xattr -p com.apple.metadata:kMDItemOMUserTags OpenMeta2OSXTags.sh | xxd -r -p | plutil -convert xml1 -o - - | xmllint --xpath "/plist/array/string/text()" -
# Writing Mavericks tags
# xattr -w com.apple.metadata:_kMDItemUserTags '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array><string>tag1</string><string>tag2</string></array></plist>' $currentFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment