Hello, is it possible to create a script that automates an application I already have? Essentially I have an application that will convert a URL to an attachment, but would like to have it run on a script instead of having to click the button multiple times a day to get it to run.
Code for the application currently is:
let settings = input.config({
title: "Convert URLs to attachments",
description: `This script will download all attachments from a URL into an attachment field. The
source field can be a URL field or any text field containing a URL. Each cell should only contain a single URL.`,
items: [
input.config.table("table", { label: "Table" }),
input.config.field("urlField", {
parentTable: "table",
label: "URL field",
}),
input.config.field("attachmentField", {
parentTable: "table",
label: "Attachment field",
description: "URLs will be downloaded into this field",
}),
],
});
async function convertURLsToAttachments() {
let { table, urlField, attachmentField } = settings;
if (attachmentField.type !== "multipleAttachments") {
output.text(
`${attachmentField.name} is not a attachment field.\nRun the script again when you have an attachment field.`
);
return;
}
let skipAlreadySet = await input.buttonsAsync(
"Skip attachment entries that already have files?",
[
{ label: "Yes", value: true },
{ label: "No", value: false },
]
);
let updates = [];
for (let record of (
await table.selectRecordsAsync({ fields: [urlField, attachmentField] })
).records) {
let existingAttachments = record.getCellValue(attachmentField) || [];
if (skipAlreadySet && existingAttachments.length) continue;
let urls = record.getCellValueAsString(urlField);
if (typeof urls !== "string") continue;
let attachmentsFromUrls = urls.split("\n").map((url) => ({ url }));
updates.push({
id: record.id,
fields: {
[attachmentField.id]: [
...existingAttachments,
...attachmentsFromUrls,
],
},
});
}
for (let i = 0; i < updates.length; i += 50) {
await table.updateRecordsAsync(updates.slice(i, i + 50));
}
}
await convertURLsToAttachments();