Skip to content

Instantly share code, notes, and snippets.

@wouterpotters
Created October 7, 2022 12:24
Show Gist options
  • Save wouterpotters/15299cb0d0475da0ab295b717d9cb2d8 to your computer and use it in GitHub Desktop.
Save wouterpotters/15299cb0d0475da0ab295b717d9cb2d8 to your computer and use it in GitHub Desktop.
Bash function to copy MS Office title metadata to the Finder Comments field
# For use on MacOS
# MacOS cannot show the title metadata of MS Office files. This script shows the
# metadata in the Comment field, which can be made visible in the finder app.
# Script to extract the MS Office word title metadata and excel title metadata
# and copy that text to Comment field, which can be visible in the Finder.app
# This enables you to show the comment in Finder.app
#
#
# Installation
#
# In the Terminal on MacOS:
# 1) Create a new file: touch move_word_title_to_comments.sh
# 2) Add execute rights: chmod +x move_word_title_to_comments.sh
# 3) Copy in this entire script
#
#
# Usage:
# move_word_title_to_comments /path/to/input/directory
# # this will update all files in that folder(!)
# # existing comments will be overwritten!
# # no title available? Nothing happens to the comments.
# check if
if [ ! -d "${1}" ]
then
echo "Directory '$1' DOES NOT exist."
echo "Usage: move_word_title_to_comments /path/to/input/directory"
exit
fi
echo "Adding MS Office titles to Finder comments in folder ${1}..."
# step 1: collect files and loop over files
# save and change IFS to avoid issues with spaces in folder names...
OLDIFS=$IFS
IFS=$'\n'
FILES="$1/*"
for f in $FILES
do
echo "Processing $f"
# Find meta data title in each file
meta=$(mdls $f | grep kMDItemTitle | grep -o '".*"' | tr -d '"')
if [ ${#meta} != "0" ]
then
# If the meta data title is present: change it using Applescript
# We are using Applescript because that also updates the database of Finder.app
# Other commands to update the metadata do not update the database and result in
# the finder app not showing that the metadata of the files were changed
osascript -e 'set p to "'"$f"'"
set p2 to POSIX file p as alias
tell application "Finder" to set comment of p2 to "'"$meta"'"'
echo "-- Comment added"
fi
echo "------"
done
#restore IFS again to avoid issues with other bash scripts...
IFS=$OLDIFS
echo "... finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment