Aug 14, 2021 04:54 AM
Hello there,
I have a table [Main] with PNG files in {Attachments}. Unfortunately, the file name is “UnnamedAttachment” for most of them. The desired file name (including .png extension) can be found in the field {Filename}.
How can I manually trigger a batch name change of the files in {Attachments} to the the name in {Filename}?
Thank you!
Cheers
Christoph
Aug 14, 2021 10:16 AM
This can be done using a script that loops through all records and performs the renaming. This has been somewhat discussed previously, though from the angle of renaming files via automation as they’re attached. It would only require a slight change to batch process everything, though. Here’s a thread from last fall:
The script listed there is designed for an automation, so it’ll need to be modified to pull the new name from the {Filename}
field. If you need help with the script changes, just holler.
Aug 14, 2021 10:48 AM
Thanks @Justin_Barrett.
Thanks exactly what I’ve tried yesterday. I couldn’t get the automation part out of it. There were multiple issues that didn’t let me execute the script, for instance at let config = input.config()
, there was a message that config is not defined. And some more.
Any ideas how to modify this properly?
Thanks!
Aug 14, 2021 10:19 PM
@christoph input.config
operates completely differently in the Scripting app. Long story short, it’s designed to allow you to build a simple UI that allows the user to pick tables, fields, etc. for use inside the script. You won’t use it for this, but I still recommend reading up on it in the API docs. It could come in handy at some point.
For your current use case, try this script:
// Setup
const mainTable = base.getTable("Main")
const mainQuery = await mainTable.selectRecordsAsync()
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let attachments = record.getCellValue("Attachments")
if (attachments.length) {
updates.push({
id: record.id,
fields: {
"Attachments": [{
url: attachments[0].url,
filename: record.getCellValue("Filename")
}]
}
})
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}