Help

Hard Code Airtable Provided Script

Topic Labels: Scripting extentions
1461 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Thomas_Winegard
5 - Automation Enthusiast
5 - Automation Enthusiast

I am wanting to hard code the table variables into the script airtable provides to convert URL’s into attachments, so that I can run it via an automation.

This is the script:

 let settings = input.config({

        title: "Convert URLs to attachments",

        description: `This script will download all attachments from a URL into an attachment field. The 

    source field can be a URL field or any text field containing a URL. Each cell should only contain a single URL.`,

        items: [

            input.config.table("table", { label: "Table" }),

            input.config.field("urlField", {

                parentTable: "table",

                label: "URL field",

            }),

            input.config.field("attachmentField", {

                parentTable: "table",

                label: "Attachment field",

                description: "URLs will be downloaded into this field",

            }),

        ],

    });

    async function convertURLsToAttachments() {

        let { table, urlField, attachmentField } = settings;

        if (attachmentField.type !== "multipleAttachments") {

            output.text(

                `${attachmentField.name} is not a attachment field.\nRun the script again when you have an attachment field.`

            );

            return;

        }

        let skipAlreadySet = await input.buttonsAsync(

            "Skip attachment entries that already have files?",

            [

                { label: "Yes", value: true },

                { label: "No", value: false },

            ]

        );

        let updates = [];

        for (let record of (

            await table.selectRecordsAsync({ fields: [urlField, attachmentField] })

        ).records) {

            let existingAttachments = record.getCellValue(attachmentField) || [];

            if (skipAlreadySet && existingAttachments.length) continue;

            let urls = record.getCellValueAsString(urlField);

            if (typeof urls !== "string") continue;

            let attachmentsFromUrls = urls.split("\n").map((url) => ({ url }));

            updates.push({

                id: record.id,

                fields: {

                    [attachmentField.id]: [

                        ...existingAttachments,

                        ...attachmentsFromUrls,

                    ],

                },

            });

        }

        for (let i = 0; i < updates.length; i += 50) {

            await table.updateRecordsAsync(updates.slice(i, i + 50));

        }

    }

    await convertURLsToAttachments();
2 Replies 2
Bill_Felix
6 - Interface Innovator
6 - Interface Innovator

I don’t quite know if this is what you mean…

const table = base.tables.filter( t => t.name === "<YOUR TABLE NAME>");

But this will allow you to ‘hard code’ from the available tables, so that your Automation will recognize where the attachments are. You can’t just remove the input.config.table portion though but this should get you started.

You can do something like this to replace the input.config section …

let settings = {}
settings.table = base.getTable("Table Name")
settings.urlField = settings.table.getField("url field name")
settings.attachmentField = settings.table.getField("attachment field name")