Help

Re: Automate changing the file name of an attachment

11390 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Cameron_Goldber
6 - Interface Innovator
6 - Interface Innovator

Hey Airtable Community.

I need help with automating the repetitive task of manually changing the name of a file after it’s been uploaded into my base. As of right now, we have a user filling out a form, and after it’s submitted, I want that to trigger the name changing. I have a field that is automatically created with a formula based on information captured in the form that I want the name changed too.

I’ve been looking at using the Airtable automation feature, but after trying a bunch of things, I can’t get it to work (perhaps I’m just too new at this) I also feel like maybe using a script might be the answer, but I don’t know how to do that, and if that’s the path to get this to work, I’ll need your help too :slightly_smiling_face: . Ultimately, I’m not sure and was hoping you (the community) might know.

Thanks in advance everyone!

29 Replies 29

@Justin_Barrett

Just ran into a problem today with the script… thought i’d reach out and get your advice.

So, on the surface, the script is a game changer for us. It’s changing file names perfectly, it’s amazing!!!

In our use case, we’re re-naming files so that when a user downloads them, it’ll be in the correct format. The problem is occurring when we download the file. For some reason, if the script is active, it turns it into a “TextEdit Document” when it’s downloaded onto my computer (we’ve also tested out multiple computers). It is critical that the script doesn’t change the type of file, as it makes it unusable… :frowning:

IE:
This is an example when the script isn’t on and how it still is a real .MOV file (same happens with .mp4): image

This is the same file downloaded with the script on: image

Things i’ve tried:

  1. I’ve tried changing the file name on my computer to see if it was perhaps the naming convention I gave it, no effect.

More context (if helpful)

  • The files we’re directing towards this script are mp4, .mov files.
  • This is what the file name looks like, it still contains .MOV so i’m confused: image

I edited the script a bit since our last message to get it working with the #1 and #2 convention. So, here is what the live version i’m using looks like:

let config = input.config()
let table = base.getTable("Self Tapes Submissions")
let query = await table.selectRecordsAsync()
let record = query.getRecord(config.recordID)

// Collect record data
let newPrefix = record.getCellValue("newName1")
let files = record.getCellValue("Self Tape #1")
// Get the extension and add it to the new name
let newFiles = []
for (let file of files) {
    let parts = file.filename.split(".")
    let ext = parts[parts.length - 1]

    let newName = `${newPrefix}. ${ext}`
    newFiles.push({url: file.url, filename: newName})
}

// Reattach the item with the new filename
await table.updateRecordAsync(record, {
    "Self Tape #1": newFiles
})

I think I know the problem…

The script is adding a space between the “.” and the file extension…

so it’s coming out “. mp4” when it should come out “.mp4”.

When I out the old number thing, I must have left a space where it would have been. Do you know what I’d have to change to remove the space. I’m going to keep trying, but if it’s an easy thing for you to see that’d be super helpful!

Changed to

let newName = `${newPrefix}.${ext}`

It’s working now.

also, now that I see the problem, it was such an easy one staring me in the face. I’ll chalk this up to a learning curve to scripts.

Glad that you were able to solve the problem. I don’t know how that space got into the new name creation, but it’s not part of my original script above. Did you copy my script, or retype it from scratch?

Yeah, as I look at your original, it was def me.

I was testing things out a bit, and trying to make it work, so I might have mistakenly added the " ".

Glad it’s solved, great script!!!

matt-pwi
5 - Automation Enthusiast
5 - Automation Enthusiast

@Justin_Barrett 

I'm currently trying to implement this into my airtable base but it's throwing an error at me.

mattpwi_0-1673629108723.png

This is what the image file looks like after I run the automation. It changes the file name, but the file is unreadable. This was from me trying the first script you wrote out.

I tried the second script, but then it was telling me that 

"let query = await table.selectRecordsAsync()"
is deprecated.
 
mattpwi_1-1673629432582.pngmattpwi_2-1673629457029.png
 
So now I'm at a loss on how to make it work. I also don't know if this script will fix the issue of not being able to read the file after reuploading. Here is everything that I'm currently working with:
 
mattpwi_3-1673629491746.png
 
mattpwi_4-1673629534267.png
 
mattpwi_6-1673629607532.png
 
mattpwi_5-1673629581069.png
 
Any help you can provide on this matter would be greatly appreciated!
matt-pwi
5 - Automation Enthusiast
5 - Automation Enthusiast

Okay, update on my issue. I noticed the name discrepancy between fileName-front and fileName.front. That was fixed. I also removed the image that got messed up from the first script and tried a fresh photo, everything works great now! 

 

I am still curious about the deprecated js line though. Will that be a problem in the future? I'm not well-versed in js yet.

Re: the "deprecated" warning, that's not about the command in its entirety, but the specific way that my original script is using it. Long story short, using that method without passing anything to it--meaning nothing inside the parentheses--used to be valid. However, it means that you're asking for *every* field from the table, which is an unnecessary resource drain if you only need a small number of fields.

The recommended (and, eventually, required) way is to pass an object that contains a list of the fields that you want to retrieve from the records. Here's a brief example; you can find more details in the documentation (click the "API" link at the bottom of the script editor):

const query = await table.selectRecordsAsync({
    fields: ["First Field", "Second Field"]
})

Unfortunately I'm not able to go back and edit my original post where the old method use exists, so I hope that this clarification will help others who find this thread as well.

Hello Justin,

I tried your first code, but I got the same error that matt-pwi. The script is renaming, however, the file seems to be broken. I reviewed the code but did not find where the issue is.

Nome ajustado > Field with the newName
Documento > Field with the attachment

Check video below to see all the details

Unfortunately I don't have time to dig into this very deeply and run any specific tests. The problem might stem from the fact that the filename that you're trying to use contains spaces. Then again, the example from @matt-pwi doesn't contain spaces and still apparently had errors, so that's a long shot.

My next hunch is that this could be fallout from the change to expiring URLs for attachments, but again, I don't have time to test this theory thoroughly.

I nearly said "Once my schedule lightens up" with a promise to come back to this, but the reality is that I have no idea when I could dig into this further. Sorry.