Can anyone help with modifying a script bellow so it forks with automations when a new record is created?
Currently, the script works when a button is pressed. The script extension is activated and attachments are resized.
let tinyPngApiKey = 'tinyPngApiKey;
let airtableAttachmentSource = 'Source';
let airtableAttachmentDestination = 'Destination;
let airtableColumnToLog = 'Compress';
let table = base.getTable('baseName');
let record = await input.recordAsync('Select a record to compress attachments:', table);
let recordAttachmentUrls = record.getCellValue(airtableAttachmentSource);
console.log('Compressing attachments for the selected record');
let compressedAttachmentUrls = [];
if (Array.isArray(recordAttachmentUrls)) {
for (let i = 0; i < recordAttachmentUrls.length; i++) {
let recordAttachmentUrl = recordAttachmentUrls[i]['url'];
body: JSON.stringify({ 'source': { 'url': recordAttachmentUrl } }),
headers: {
Authorization: 'Basic ' + btoa('api:' + tinyPngApiKey),
'Content-Type': 'application/json'
},
method: 'POST'
});
const json = await request.json();
// Checks that the API didn't fail.
if (request.status == 201) {
let percentReduced = Math.round((1 - json.output.ratio) * 100);
let kbReduced = (json.input.size - json.output.size) / 1024;
console.log('Panda just saved you ' + percentReduced + '% (' + Math.round(kbReduced) + 'KB).');
compressedAttachmentUrls.push({ url: json['output']['url'] });
}
}
}
await table.updateRecordAsync(record.id, {
[airtableAttachmentDestination]: compressedAttachmentUrls,
});