Help

Upcoming database upgrades. to improve our reliability at 03:30 UTC on Feb. 25 / 7:30pm PT on Feb. 24. Some users may briefly experience slow load times or error messages. Learn more here

How to change image name with script

Topic Labels: Automations
238 3
cancel
Showing results for 
Search instead for 
Did you mean: 

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

3 Replies 3

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:

  • Used a title field for new attachments names
  • In case of multiple attachments I have added -2 -3 -4 to the name
  • maintained the same extension for the files (if it was .jpg it stays as .jpg)

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:

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

Hi @kennypurnomo ,

I think the other code piece goes into similar direction. What are you missing from the solution above?