-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvert_tags.bash
executable file
·56 lines (45 loc) · 1.87 KB
/
convert_tags.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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>'