Populate dropdown list in SwitchScripter

Post Reply
joaodaffonsojr
Member
Posts: 24
Joined: Wed Sep 08, 2021 4:36 pm

Populate dropdown list in SwitchScripter

Post by joaodaffonsojr »

I´m using a node script that reads a directoty content and stores the values in a variable.
I want to make this result apear is a dropdown menu in the switch script so I can select one of the values.
I see that have this option on Switch Scripter but I have to enter manually the values.
Is it possible to populate this dropdown menu with the results from the variable ?
dropdown.png
dropdown.png (55.13 KiB) Viewed 8839 times
jan_suhr
Advanced member
Posts: 592
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Populate dropdown list in SwitchScripter

Post by jan_suhr »

You have the property "Select from library". Then you get a list of values that you can populate from different sources.

You get the values from this Entry point in the script
getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]>
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
joaodaffonsojr
Member
Posts: 24
Joined: Wed Sep 08, 2021 4:36 pm

Re: Populate dropdown list in SwitchScripter

Post by joaodaffonsojr »

Thank you jan !

I´ll give a try !
joaodaffonsojr
Member
Posts: 24
Joined: Wed Sep 08, 2021 4:36 pm

Re: Populate dropdown list in SwitchScripter

Post by joaodaffonsojr »

jan_suhr wrote: Fri Feb 10, 2023 7:41 pm You have the property "Select from library". Then you get a list of values that you can populate from different sources.

You get the values from this Entry point in the script
getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]>
Hi jan !
Sorry to bother you!
I'm trying to make getLibraryForProperty work,
but I really couldn't understand how to do it.
Below is the example I intend to use.
I have a directory that I need to be the "array" to populate the Library.
Can you give me a tip to solve this?
Thank you very much in advance!


Code: Select all

//requirements
const fs = require("fs");

// replace with the path to your directory
const directoryPath = 'D:\\node_js\\dirlist\\sampleDoc';

//switch
async function jobArrived(s, flowElement, job) {

    //log directoryPath
    await job.log(LogLevel.Warning, "directoryPath " + directoryPath);

    async function getLibraryForProperty(s, flowElement, tag) {   //directory list array

    fs.readdir(directoryPath, (err, tag) => {
        if (err) {
            console.error('Error reading directory:', err);
            return;
        }
        // Create a JSON array from the file names
        const jsonArray = JSON.stringify(tag);
       
    });
    //log array
    await job.log(LogLevel.Warning, "directoryPath " + jsonArray);

}

    //remove input job
    await job.sendToSingle();
}
mkayyyy
Member
Posts: 80
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Populate dropdown list in SwitchScripter

Post by mkayyyy »

This is an example of how you'd implement the getLibraryForProperty entry point:

Code: Select all

import * as fse from "fs-extra";

async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
    await job.sendToSingle();
}

async function getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]> {
    let props: string[] = [];

    if (tag === "directoryPath") {

        // replace with the path to your directory
        const directoryPath = 'D:\\node_js\\dirlist\\sampleDoc';
    
        //log directoryPath
        await flowElement.log(LogLevel.Warning, "directoryPath " + directoryPath);
    
        props = await fse.readdir(directoryPath);
    }

    return props;
}
tdeschampsBluewest
Member
Posts: 37
Joined: Tue Jun 01, 2021 11:57 am

Re: Populate dropdown list in SwitchScripter

Post by tdeschampsBluewest »

Code: Select all

async function getLibraryForProperty(s: Switch, flowElement: FlowElement, tag: string): Promise<string[]> {
    let library = [];
    switch (tag) {
        case "case1":
            library = []; //Some code here;
            break;
        default: 
            library = ["No Value found"];//optional

            break;
    }
    return library;
}
joaodaffonsojr
Member
Posts: 24
Joined: Wed Sep 08, 2021 4:36 pm

Re: Populate dropdown list in SwitchScripter

Post by joaodaffonsojr »

This is the code that made it work.

thanks Jan , mkayyyy and tdeschampsBluewest !

Code: Select all

async function getLibraryForProperty(s, flowElement, tag) {
    let props = [];
    if (tag === "Template") {
        // replace with the path to your directory
        const directoryPath = 'D:\\node_js\\dirlist\\sampleDoc\\';
        //creates de file list array
        props = fs.readdirSync(directoryPath);
    }
    return props;
}
Post Reply