Scripts by ashkanhosseini

URL actions (inspired by Alfred)

Hey, I put together a script for URL actions very similar to the one from Alfred. It also works with {parameters}. Hope you find it useful.

// Menu: URL Actions "App"
// Description: Add your urls and include parameters
// Shortcut: cmd shift u
// Author: Ashkan Hosseini
let { getUrl, getUrls, addUrl, removeUrl } = await lib('urls');
let urlsChoices = () =>
getUrls().map(({ name, url, id }) => ({
name: `${name} (${url})`,
value: id,
}));
let useUrl = async () => {
let id = await arg('Use url:', urlsChoices());
let { url } = await getUrl(id);
const matches = url.match(/\{(.*?)\}/g);
if (matches && matches.length) {
const params = {};
for (let key of matches) {
key.replace('{', '').replace('}', '');
params[key] = await arg(`Enter value for param ${key}`);
}
url = Object.entries(params).reduce((acc, [key, value]) => {
return acc.replace(new RegExp(`${key}`, 'g'), value);
}, url);
}
exec(`open ${url}`);
};
let add = async () => {
const name = await arg('Enter a URL name:');
const url = await arg('Enter a URL address:');
addUrl(name, url);
await add();
};
let remove = async () => {
let id = await arg('Remove url:', urlsChoices());
removeUrl(id);
await remove();
};
onTab('Use', useUrl);
onTab('Add', add);
onTab('Remove', remove);

also the urls module:

let urlsDb = db('urls', { urls: [] });
export let urls = urlsDb.get('urls');
export let getUrls = () => urls.value();
export let addUrl = (name, url) => {
urls.insert({ name, url }).write();
};
export let removeUrl = (id) => {
urls.remove({ id }).write();
};
export let getUrl = async (id) => {
const urls = await getUrls();
return urls.find((obj) => obj.id == id);
};
Discuss Post