Jump to content

Toggle a class between 2 values in a local .css file


Recommended Posts

What I need help building:

 

Ok so I have a .css file on disk and I'd like to toggle the value of a specific class through an Alfred workflow. 

 

Basically I want Alfred to toggle between these 2 states: 

.markdown-source-view { font-family: 'Space Mono', monospace; }

 

.markdown-source-view { font-family: 'Inter', Sans-Serif; }

 

Hoping someone could help out, I have no idea how best to tackle such a thing? 

 

 

What I will use it for (if you're interested):

 

I recently started using Obsidian for knowledge management (it's amazing). It allows for theming through custom css. I like to switch on monospace editing sometimes and have been manually switching between obsidian.css files to achieve this. An Alfred workflow would save so much time here.

Link to comment

something like this should work - basically there are two versions of the file and Alfred calls a shell script to copy one or the other into a third active file.  I found that was easier than trying to parse file contents.

Edited by dfay
Link to comment

Hi dfay,

 

Thanks for volunteering some time to help me out with this, I really appreciate it.

 

The original workflow from the thread you linked is no longer available, so I wasn't 100% sure where to plug in your code. I've stripped out the unnecessary features from your code ( (hopefully correctly?) and plugged this into a Run NSAppleScript node like so:

on alfred_script(q)
  try
	do shell script "cd '/Users/nathan/Dropbox/Maintenance/Notes/'; obsidian1.css > obsidian.css"
on error
	do shell script "cd '/Users/nathan/Dropbox/Maintenance/Notes/'; obsidian2.css > obsidian.css"
end try
end alfred_script

 

Unfortunately this throws an error. Here's the debug console:

 

[14:47:51.984] Toggle Obsidian's .css[Hotkey] Processing complete
[14:47:51.986] Toggle Obsidian's .css[Hotkey] Passing output '' to Run NSAppleScript
[14:47:52.012] ERROR: Toggle Obsidian's .css[Run NSAppleScript] {
    NSAppleScriptErrorAppName = "Alfred 4";
    NSAppleScriptErrorBriefMessage = "sh: obsidian2.css: command not found";
    NSAppleScriptErrorMessage = "sh: obsidian2.css: command not found";
    NSAppleScriptErrorNumber = 127;
    NSAppleScriptErrorRange = "NSRange: {124, 101}";
}

 

Would you mind taking a look? 

Link to comment

Oh wait, I'm an idiot 🤦‍♂️ Seems I hacked away an important part of the script: cat

 

So now I've got this:

on alfred_script(q)
try
	do shell script "cd '/Users/nathan/Dropbox/Maintenance/Notes/'; cat obsidian1.css > obsidian.css"
on error
	do shell script "cd '/Users/nathan/Dropbox/Maintenance/Notes/'; cat obsidian2.css > obsidian.css"
end try
end alfred_script

 

So this is doing something now. But it's not toggling between the 2 states. 

It will successfully copy the contents of obsidian1.css into obsidian.css, but once it's in that state, the workflow won't change it to the contents of obsidian2.css

 

Link to comment
3 hours ago, Floating.Point said:

But it's not toggling between the 2 states.

 

No. It overwrites obsidian.css with obsidian1.css, and if that fails, it overwrites it with obsidian2.css.

 

If you want to toggle something, you first need to know what state it's currently in.

 

Personally, I'd make obsidian.css a symlink and change the file it points to, but applications don't always notice when symlinks change.

Link to comment

Hi deanishe,

 

Thanks for jumping in, I appreciate your help. I just tested and unfortunately Obsidian app doesn't work when the the obsidian.css file is symlinked :(

 

Quote

If you want to toggle something, you first need to know what state it's currently in.

 

Would you be willing to help work this out?

 

 

Link to comment

Here's a zsh script that should work. Run it in a Run Script with Language = /bin/zsh.

 

#!/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}/obsidian1.css"
source2="${dir}/obsidian2.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

 

Link to comment

Ok wow, thank you deanishe. It's no wonder you're carrying the badge "community hero"! This works perfectly. Thank you so much for taking the time to not only put this together, but to also make it so clear as to what needs to be edited to get it working. The inclusion of the warning after the settings is great :D  I hope you have a great day

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...