I constantly need to make some simple calculations and that's one of a feature that I liked about Alfred.

With ScriptKit I think I made it slightly better:

  • live preview of your calculation
  • select to copy to clipboard
  • remembers last 10 calculations
  • auto fixing , to . - so you don't have to do it manually

Install calc

// Menu: Calculator
// Author: Jakub Olek
// Twitter: @JakubOlek
// Shortcut: opt =
const calcDb = await db("calc", { history: [] });
function createResult(calculationResult, input) {
return {
name: calculationResult,
description: input,
value: { calculationResult, input },
};
}
const { calculationResult, input, ...rest } = await arg("0", async (input) => {
const choices = [];
if (input) {
let { stdout } = exec(
`bc <<<"${input.replace(/\,/g, ".")}" -l`,
{ silent: true } // Avoids printing the errors in the terminal
);
if (stdout) {
choices.push(createResult(stdout.replace(/^\./, "0.").trim(), input));
return choices.concat(calcDb.history);
}
}
});
if (calculationResult) {
const history = calcDb.history;
history.unshift(createResult(calculationResult, input));
calcDb.history = history
.filter(({ description }, index) => index === 0 || description !== input)
.slice(0, 10);
await calcDb.write();
}
copy(calculationResult);