Toggle obsidian.css via Alfred on MacOS

I wanted a way to toggle between monospace and sans serif fonts via keyboard shortcut. Thankfully Dean Jackson of the Alfred community got it up and running, it’s really nifty. To set it up you need to have 2 additional css files (reference files) next to your obsidian.css. These can be named whatever you like, you just need to make sure the script has the right file names. Now whenever the script is run, it will populate the contents of your obsidian.css with that of your reference files, toggling between them each time it is run. Hook that up to a keyboard shortcut and you’re good to go.

If you want to get this going yourself, just make a new Alfred workflow with a Run Script action using /bin/zsh like this:

And heres the code to paste in there:

#!/bin/zsh

set -e

# Script settings

# directory containing all files (only used to set other paths)
dir="/Users/nathan/Dropbox/Maintenance/Notes"

# the two source files that will be toggled
source1="${dir}/obsidianSans.css"
source2="${dir}/obsidianMono.css"

# the file one of the sources will be written to
target="${dir}/obsidian.css"

# where the current value is saved
statefile="${dir}/.current.txt"

#######################################################################
# End of settings
# Don't edit below unless you know what you're doing!
#######################################################################

current=
if [[ -f "$statefile" ]]; then
	current="$( cat "$statefile" )"
fi

source="$source1"
if [[ "$current" = "$source1" ]]; then
	source="$source2"
fi

command cp -f "$source" "$target"
echo -n "$source" > "$statefile"

echo "switched: $current  -->  $source" >&2
3 Likes