Skip to content

Instantly share code, notes, and snippets.

@fcrespo82
Created October 16, 2020 13:09
Show Gist options
  • Save fcrespo82/b1f11b98ffb2171b861d97cce341d08f to your computer and use it in GitHub Desktop.
Save fcrespo82/b1f11b98ffb2171b861d97cce341d08f to your computer and use it in GitHub Desktop.
Scriptable Helper Scripts
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: check-double;
let alertUtils = importModule("AlertUtils")
// Ask for input with validation
let input = await alertUtils.askInput({
title: "Enter phone number",
validate: (value) => {
if (isNaN(parseInt(value))) {
return false
}
return true
},
validationMessage: "Please, input only numbers."
})
// Show the response from the previous Alert
await alertUtils.showAlert({ title: "The phone number you entered was", message: input })
// Ask a Yes/No question
let question = await alertUtils.showAlert({ title: "Choose Yes or No", question: true })
// Show the response from the previous Alert
await alertUtils.showAlert({ title: `You answered ${question}` })
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: wrench;
//@ts-check
/**
* Show an Alert asking for input with support for response validation
* @param {{placeholder?: string, text?: string, title?: string, message?: string, okButtonTitle?: string, cancelButtonTitle?: string, validate?: (value) => Boolean, validationMessage?: string}} config Alert input configuration
*/
async function askInput(config) {
let alert = new Alert();
alert.addTextField(config.placeholder ?? "", config.text ?? "");
alert.title = config.title ?? "Default title";
alert.message = config.message ?? "";
alert.addAction(config.okButtonTitle ?? "OK");
alert.addCancelAction(config.cancelButtonTitle ?? "Cancel")
let cancel = await alert.present() === -1;
if (cancel) {
return
}
if (config.validate && config.validate(alert.textFieldValue(0))) {
return alert.textFieldValue(0);
} else {
await showAlert({ title: "Validation error", message: config.validationMessage })
return await askInput(config)
}
}
/**
* Show an Alert asking a question
* @param {{title: string, message?: string, question?: Boolean, okButtonTitle?: string, cancelButtonTitle?: string}} config
*/
async function showAlert(config) {
let alert = new Alert();
alert.title = config.title ?? "Default title";
alert.message = config.message ?? "";
alert.addAction(config.question ? (config.cancelButtonTitle ?? "Yes") : (config.cancelButtonTitle ?? "OK"));
if (config.question) {
alert.addCancelAction(config.question ? (config.cancelButtonTitle ?? "No") : (config.cancelButtonTitle ?? "Cancel"))
}
let cancel = await alert.present() === -1;
return !cancel
}
module.exports = {
showAlert,
askInput
}
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: check-double;
const tableUtils = importModule("UITableUtils")
const alertUtils = importModule("AlertUtils")
const dummyData = [
{ name: "Simon Støvring" },
{ name: "Scriptable", message: "Awesome App" }
]
/**
* Show a table configured with rows before and after data rows, and a custom row design
*/
await tableUtils.showTable({
data: dummyData,
beforeDataRows: (table) => {
let row = new UITableRow()
row.addText("Tap on a row")
table.addRow(row)
},
rowForItem: (table, item) => {
let row = new UITableRow()
row.addText(item.name, item.message ?? "")
row.onSelect = async () => {
await alertUtils.showAlert({ title: item.name })
}
return row
},
afterDataRows: (table) => {
let row = new UITableRow()
row.addText("After")
table.addRow(row)
}
});
/**
* Show a table configured with a custom row design
*/
await tableUtils.showTable({
data: dummyData,
rowForItem: (table, item) => {
let row = new UITableRow()
row.addText(item.name, item.message ?? "")
row.addButton("Tap me").onTap = async () => {
await alertUtils.showAlert({ title: item.name })
}
return row
}
});
/**
* Show a table configured with a default row design
* By default it converts any Object to string using JSON.stringify() function
*/
await tableUtils.showTable({
data: dummyData
})
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: wrench;
//@ts-check
/**
* Show a table configured by config
* @param {{data: any[], showSeparators?: Boolean, fullscreen?: Boolean, rowForItem?: (table: UITable, item) => UITableRow, beforeDataRows?: (table: UITable) => void, afterDataRows?: (table: UITable) => void}} config UITable configuration
*/
async function showTable(config) {
let table = new UITable();
table.showSeparators = config.showSeparators ?? true;
if (config.beforeDataRows) {
config.beforeDataRows(table)
}
for (let index in config.data) {
if (config.rowForItem) {
table.addRow(config.rowForItem(table, config.data[index]));
} else {
let row = new UITableRow()
row.addText(JSON.stringify(config.data[index]))
table.addRow(row)
}
}
if (config.afterDataRows) {
config.afterDataRows(table)
}
return table.present(config.fullscreen ?? true);
}
module.exports = {
showTable
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment