Oct 07, 2020 05:54 PM
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!
Oct 22, 2020 10:06 AM
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):
This is the same file downloaded with the script on:
Things i’ve tried:
More context (if helpful)
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
})
Oct 22, 2020 10:29 AM
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!
Oct 22, 2020 10:56 AM
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.
Oct 22, 2020 02:33 PM
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?
Oct 23, 2020 04:20 PM
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!!!
Jan 13, 2023 09:10 AM - edited Jan 13, 2023 09:12 AM
I'm currently trying to implement this into my airtable base but it's throwing an error at me.
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
Jan 13, 2023 09:51 AM
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.
Jan 13, 2023 04:13 PM
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.
Jan 26, 2023 12:12 PM
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
Jan 26, 2023 04:56 PM
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.