Help

Re: automatically change image attachment filenames

1388 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Edward1976
6 - Interface Innovator
6 - Interface Innovator

I have a table in which images are automatically uploaded from our server (see screenshot). These image files are then sent to s3 for further manipulation and need a more meaningful filename. I tried to follow this video https://www.youtube.com/watch?v=qUPQoXQQATc which I feel is almost working but I have some errors which I can't figure out. This is my code so far:

const imageAttachment = base.getTable("Pre Mock Prod (PB)");

const recordid = input.config()["recordid"];

const recordToUpdate = await imageAttachment.selectRecordAsync(recordid)

let title = recordToUpdate?.getCellValue("mockup_id")
let imageField = recordToUpdate?.getCellValue("mockUpFiles")

let newImageField = []

if (imageField){

for ( let [image, imageNumber] of imageField.entries()){
console.log(image, imageNumber);
 
let findExtension = image.filename.match(/\w+_\d+\.\w+$/)||[""]
let extension = findExtension[0]
console.log(extension);

let fileSpecifcName = imageNumber>0 ? "-" + (imageNumber) : ""

newImageField.push ( {
url:image.url,
filename: title + fileSpecifcName + extension
})
}
console.log("New Image Field Array", newImageField)

await imageAttachment.updateRecordsAsync(recordid,
{
mockUpFiles: newImageField
})
}

This is a copy or my table: https://airtable.com/shrS1aPnJ6YDPUsrN/tbltBWGWBNAKbDOIl

 

 

Link to demo base and script here: https://businessautomated.gumroad.com/l/update-multiple-attachment-names-in-airtable Using this Airtable script you can automatically rename the attachments / files uploaded to the attachment field in Airtable. You can rename the files in Airtable based on title
2 Replies 2
Edward1976
6 - Interface Innovator
6 - Interface Innovator

I think I am nearly there with this but it only updates 1 record:

const imageAttachment = base.getTable("Pre Mock Prod (PB)");

const recordid = input.config()["recordid"];

const recordToUpdate = await imageAttachment.selectRecordAsync(recordid);

let title = recordToUpdate?.getCellValue("mockup_id");
let imageField = recordToUpdate?.getCellValue("mockUpFiles");
let newImageField = [];

if (imageField) {
for (let [imageNumber, image] of imageField.entries()) {
console.log(image, imageNumber);
 
let findExtension = image.filename.match(/\w+_\d+\.\w+$/) || [""];
let extension = findExtension[0];
console.log(extension);

let fileSpecificName = imageNumber > 0 ? "-" + (imageNumber) : "";

newImageField.push({
url: image.url,
filename: title + fileSpecificName + extension
});
}
console.log("New Image Field Array", newImageField);

await imageAttachment.updateRecordsAsync([{
id: recordid,
fields: {
mockUpFiles: newImageField
}
}]);
}
Edward1976
6 - Interface Innovator
6 - Interface Innovator

This is as close as I can get. If someone could let me know how to edit the code so that the file name is renamed as it is created or updated that would be great. Currently, this only works if I update the record which isn't ideal:

const imageAttachment = base.getTable("Pre Mock Prod (PB)");

const recordid = input.config()["recordid"];

const recordToUpdate = await imageAttachment.selectRecordAsync(recordid);

let title = recordToUpdate?.getCellValue("mockup_id");
let imageField = recordToUpdate?.getCellValue("mockUpFiles");
let newImageField = [];

if (imageField) {
  for (let [imageNumber, image] of imageField.entries()) {
    console.log(image, imageNumber);
  
    let findExtension = image.filename.match(/\w+_\d+\.\w+$/) || [""];
    let extension = findExtension[0];
    console.log(extension);

    let fileSpecificName = imageNumber > 0 ? "-" + (imageNumber) : "";

    newImageField.push({
      url: image.url,
      filename: title + fileSpecificName + extension
    });
  }  
  console.log("New Image Field Array", newImageField);

  await imageAttachment.updateRecordsAsync([{
    id: recordid,
    fields: {
      mockUpFiles: newImageField
    }
  }]);
}