Skip to content

Instantly share code, notes, and snippets.

@paulelms
Forked from Zettt/OpenMeta2OSXTags.sh
Created June 18, 2014 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulelms/3db2d03487dc0309731f to your computer and use it in GitHub Desktop.
Save paulelms/3db2d03487dc0309731f to your computer and use it in GitHub Desktop.
# =============================================================
# = 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!
# =============================================================
# Please run:
#
# chmod +x OpenMeta2OSXTags.sh
#
# Call the script using:
#
# ./OpenMeta2OSXTags.sh
#!/bin/sh
# this is the absolute path of the directory you want to process
# please no trailing slash!
# think twice if it's really necessary to process your entire $HOME
files="/Users/you/Documents"
unset a i
while IFS= read -r -d $'\0' file; do
fileArray[i++]="$file"
done < <(find "$files" -print0)
# echo ${fileArray[@]}
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
echo "Processing: $currentFile"
# extract openmeta tags to string
currentTags=$(/usr/local/bin/openmeta -t -p "$currentFile")
# remove trailing -p path from openmeta
currentTags=$(echo ${currentTags%%$currentFile})
# remove trailing whitespace (if any)
currentTags=$(echo ${currentTags%%\w})
# only process if there are tags
if [[ -z $currentTags ]]; then
echo "File has no tags."
else
echo "Number of tags: ${#tagArray[@]}"
echo "Tags: $currentTags"
# create array of all tags
eval tagArray=($currentTags)
# assemble plist string of tags
plistTagString=""
for i in "${tagArray[@]}"; do
# echo "Tag $i"
plistTagString="$plistTagString<string>$i</string>"
done
# write tags to file
xattr -w com.apple.metadata:_kMDItemUserTags "$plistFront$plistTagString$plistEnd" "$currentFile"
fi
echo
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