Oct 29, 2022 10:31 PM
I have multiple images in a field. How can i change each attachment name using one click script?
The attachment name should be based on {name} field.
Thank you for the advice
Oct 30, 2022 01:07 AM
Hi @Kenny_Purnomo ,
Very interesting question. I have looked into it and according to my best understanding, while you cannot use the script to rename the attachment names, you can use it to “re-upload” files with different names.
End result is the same… :winking_face:
Here is a draft script of how it could look like:
const imageTable = base.getTable("Table 1");
const recordsFromImageTable = await imageTable.selectRecordsAsync({fields:["Thumbnail","Title"]})
console.log(recordsFromImageTable)
for (let record of recordsFromImageTable.records){
console.log(record.name)
let title = record.getCellValue("Title")
let imageField = record.getCellValue("Thumbnail")
let newImageField =[]
for ( let [imageNumber,image] of imageField.entries()){
console.log(imageNumber,image,)
let findExtension = image.filename.match(/\.\w+$/) || [""]
let extension = findExtension[0]
console.log(extension)
let fileSpecificName= imageNumber>0 ? "-" + (imageNumber+1) : ""
newImageField.push( {
url:image.url,
filename: title + fileSpecificName + extension
})
}
console.log("New image field array", newImageField)
await imageTable.updateRecordAsync(record.id,
{
Thumbnail: newImageField
})
}
I was testing this on this base:
I made some assumptions here:
So the use case above is that the script renames the images to match the title of video for which I am using them.
You can also check out the YouTube video here:
I hope this helps! :v:
Oct 30, 2022 09:00 PM
Thank you for the answer!
I Copy and tweak someone’s code in other thread. Please see below link
Can i just rename it? I think someone did this
Nov 01, 2022 06:11 PM
Hi @kennypurnomo ,
I think the other code piece goes into similar direction. What are you missing from the solution above?