Skip to content

Instantly share code, notes, and snippets.

@Zettt
Last active May 5, 2021 15:58
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Zettt/5880737 to your computer and use it in GitHub Desktop.
Save Zettt/5880737 to your computer and use it in GitHub Desktop.
Script that reads OpenMeta tags and writes them to OS X Mavericks tags. 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…
# =============================================================
# = 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
@nickbloom
Copy link

I get this error when I try to run the script:

./OpenMeta2OSXTags.sh: line 41: syntax error near unexpected token<' ./OpenMeta2OSXTags.sh: line 41: done < <(find "$files" -print0)'

@lcmen
Copy link

lcmen commented Jan 24, 2016

@nickbloom replace 39-41 lines with:

while read -r file; do
  fileArray[i++]="$file"
done <<< "`find $files`"

@lcmen
Copy link

lcmen commented Jan 24, 2016

This script doesn't work correctly - if you update tags inside nvAlt app after your tags has already been converted, os x tags won't be updated.

I've updated the script here. It also doesn't require openmeta binary any more.

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