RunJS script, help with finish him

Writing a script

import * as obsidian from 'obsidian';
const runJS = app.plugins.plugins["runjs"];
const dataviewAPI = runJS.app.plugins.plugins["dataview"].api;
const list_pages_all = dataviewAPI.pagePaths().values;
let list_pages = filterBySubstring(list_pages_all, "books/details");
let books_list = await runJS.suggest('Select book', list_pages);
let current_book = dataviewAPI.page(books_list).file;
const list_operation = ['Start of reading', 'End of reading']
let choice = await runJS.suggest('Select action', list_operation, 'Start of reading');
operation = choice === "Start of reading" ? "start" : "end"

runJS.app.fileManager.processFrontMatter(current_book, (frontMatter) => {
	const startDate = frontMatter[operation];
	if (isEmptyVariable(startDate)) {
		let datenow = formatDate(new Date());
		frontMatter[operation] = datenow;		
	}
})

function isEmptyVariable(variable) {
    if (variable === undefined || variable === null) {
        return true;
    }

    if (typeof variable === 'string' && variable.trim() === '') {
        return true;
    }

    return false;
}

function formatDate(date) {
  let dayOfMonth = date.getDate();
  let month = date.getMonth() + 1;
  let year = date.getFullYear();

  year = year.toString();
  month = month < 10 ? '0' + month : month;
  dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;

  return `${year}-${month}-${dayOfMonth}`
}

function filterBySubstring(array, substring) {
    return array.filter(function(item) {
        return item.includes(substring);
    });
}

Stuck on a line

runJS.app.fileManager.processFrontMatter(current_book, (frontMatter) => {

because I must be passing the wrong type of data to the function. How can I fix it?
And the second question - how can I put some functions in a separate module, as they are repeated in different scripts?

I don’t use RunJS :innocent: … but from the Dev doc, app.fileManager.processFrontMatter() needs Obsidian’s TFile object … which I don’t know if this is what Dataview returns (I’d guess not… I mean, Dataview probably returns its own thing :woman_shrugging: :thinking: )

You could potentially get the needed TFile using .getFileByPath() … then you should be able to pass it to processFrontMatter… I guess :sweat_smile:

1 Like

That’s exactly what was needed. I just couldn’t find the function

My pleasure :smile: !