shabegom / buttons

Buttons in Obsidian
The Unlicense
455 stars 47 forks source link

Using buttons to modify frontmatter #55

Closed pdm-pcb closed 1 year ago

pdm-pcb commented 3 years ago

Hey there! I'm cross-posting this with the Obsidian forum, because after giving it a day, I realized I just may not be using Buttons correctly. =) If this isn't suitable for an issue, feel free to close. Here's what I've got!

Hey all,

In most of my tasks, I've got front matter looking akin to:

---
created: 2021-06-06
assigned: 2021-06-06
dueDate: 2021-06-09
todoDate: 2021-06-07
inprogressDate: ""
doneDate: ""
status: 2
statusList:
  - ❔ Created
  - 📆 Scheduled
  - ⚙️ In Progress
  - ✅ Complete
  - ❎ Will Not Do
class: MAT220🧮
task: "Data Visualization"
---

I then use dataview to render tables of tasks with their various traits. It's quite nice!

I'd like to streamline things, though by being able to graphically update the status number. As it stands, I wind up needing to count to be sure in most cases, which is lame and slow. =)

So I wrote this little python script:

#!/usr/bin/env python3

import sys
import frontmatter
import os

if len(sys.argv) != 2:
    quit()

current_filename = sys.argv[1]
desired_status   = os.environ["desired_status"].lower()

if len(desired_status) == 0:
    quit()

with open(current_filename) as current_file:
    post = frontmatter.load(current_file)

    desired_index = -1

    for index, status in enumerate(post["statusList"]):
        if desired_status in status.lower():
            desired_index = index

    if desired_index != -1:
        post["status"] = desired_index
        print(frontmatter.dumps(post))
    else:
        print(f"Could not find status '{desired_status}'.")

And put it into action with this here Buttons button:

```button
name Mark Complete
type line(1) text
action <% tp.user.update_status({desired_status: "complete"}) %>
replace [1,0]
templater true
color blue
```

It doesn't work quite, as it winds up appending everything. I tried using replace [1,1000], replace [1,-1], and replace [1,0] but it seems like Buttons wants exact line numbers.

So... thoughts? Is there a simpler way to graphically run a Templater command? Or maybe there's some way to run Templater commands via hotkeys?

I'm open to whatever solution. =) Thanks in advance!

shabegom commented 3 years ago

A couple of thoughts come to mind for this.

  1. Check out the Hotkeys for Templates plugin. That allows execution of a template based on a hotkey or from the command palette. https://github.com/Vinzent03/obsidian-hotkeys-for-templates
  2. Also look at the MetaEdit plugin which allows quick access to edit frontmatter values. https://github.com/chhoumann/MetaEdit

a replace[1, 1000] should in theory replace all content from the first line up to the end of the file (if the file has 1000 or fewer lines).

The other idea I have is using a TemplaterJS script to modify the document directly and then firing it with a button. You can directly modify the files contents with this method...

check out this post on writing TemplaterJS scripts: https://publish.obsidian.md/shabegom/Publish/How+To+Use+Templater++JS+Scripts