Last active
January 28, 2024 02:58
-
-
Save alexmarkley/bc9f0d30dd3f6005ed16ee59b4a78a96 to your computer and use it in GitHub Desktop.
A little script for renaming font files if the filename has been obfuscated for some reason.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### renameFontFile.sh - A little script for renaming font files if the filename has been obfuscated for some reason. | |
### | |
### Reasons you might want this tool: | |
### - If you notice that you have a lot of fonts on hand, say that you have legally downloaded via a subscription to some service like Adobe Cloud. | |
### - And you find those OTF files in a location on your system (for example): "~/Library/Application Support/Adobe/CoreSync/plugins/livetype/.r/" | |
### - But you are dismayed because all of the font filenames have been obfuscated with useless names like: ".12345678.otf" | |
### - You could just copy them somewhere else and run this tool on each OTF file to de-obfuscate the names. (Or not. It's a free country.) | |
function _log() { | |
echo renameFontFile: "${@}" 1>&2 | |
} | |
function _die() { | |
_log FATAL: "${@}" | |
exit 1 | |
} | |
SOURCE="${1}" | |
DIRNAME=$(dirname -- "${SOURCE}") | |
BASENAME=$(basename -- "${SOURCE}") | |
EXTENSION="${BASENAME##*.}" | |
FILENAME="${BASENAME%.*}" | |
FONTNAME=$(fc-scan "${SOURCE}" | grep -E '^[[:space:]]postscriptname: "[-_a-zA-Z0-9]+"\(s\)$' | sed -E -e 's/^[[:space:]]postscriptname: "([-_a-zA-Z0-9]+)"\(s\)$/\1/') | |
if [ -z "${FONTNAME}" ]; then | |
_die "${SOURCE}: Couldn't find a font name inside this file." | |
fi | |
if [ "${FILENAME}" = "${FONTNAME}" ]; then | |
_log "${SOURCE}: This filename already matches the font name. Nothing to do." | |
exit 0 | |
fi | |
mv -v "${SOURCE}" "${DIRNAME}/${FONTNAME}.${EXTENSION}" || _die "${SOURCE}: Could not rename file." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment