thespacedoctor
11/20/2015 - 1:46 PM

[OpenMeta 2 MacOS tags] Script that reads OpenMeta tags and writes them to OS X Mavericks tags #tag #openmeta #convert

[OpenMeta 2 MacOS tags] Script that reads OpenMeta tags and writes them to OS X Mavericks tags #tag #openmeta #convert

http://mosx.tumblr.com/post/54049528297/convert-openmeta-to-os-x-mavericks-tags-with-this

This requires a OpenMeta binary to be installed. Change its path, if you prefer it to be somewhere else.

For the files to be processed properly don't include a trailing slash in $files.

Please run: chmod +x OpenMeta2OSXTags.sh and call the script using: ./OpenMeta2OSXTags.sh

# =============================================================
# =                  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