MacOS – Can an AppleScript create new variables on its own

applescriptautomationmacos

I have an AppleScript .app file that creates a list variable based on a website's HTML data. The number of items in this list varies based on the unique HTML data that is downloaded on every iteration, and can range from 1 to 50.

I ultimately want to display the contents of each line as its own drop-down menu in one dialog. I am using the external script library, Dialog Toolkit, to accomplish this, which requires the declaration of many additional variables.

Dialog Toolkit works differently than choose from list, for example. The choose from list dialog will automatically adjust its size to add a new line for each list item. But, for a Dialog Toolkit dialog, you instead must separately define (i.e., as its own unique variable) every drop-down menu that is needed. If you don't, zero drop-down menus will exist in the dialog.

Hence, 50 if statements are needed to ensure that the dialog contains all drop-down menus, like so:

repeat with i from 1 to (count of theList)
        if (i is 1) then
            set {dropdown1} to create labeled dropdown (item i of theList)
        else if (i is 2) then
            set {dropdown2} to create labeled dropdown (item i of theList)
        i if (i is 3) then
            set {dropdown3} to create labeled dropdown (item i of theList)
        else if (i is 4) then

        ...

        else if (i is 50) then
            set {dropdown50} to create labeled dropdown (item i of theList)
        end if
end repeat

-- Note: The above code snippet is a very stripped down version of my code.

Is it possible for an AppleScript to create, on its own, new variables that have formulaic titles?

Here is the type of thing that I desire:

repeat with i from 1 to (count of theList)
    set {(("dropdown" & i) as variable)} to create labeled dropdown (item i of theList)
end repeat

OS X El Capitan, version 10.11.6.


Best Answer

This is an example of using a list to achieve something similar to what you are trying to do, I think:

    set varList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

    tell application "Google Chrome"
        count every tab of window 1
        set tabCount to result
        set counterOne to 1
        repeat tabCount times
            get URL of tab counterOne of window 1
            set item counterOne of varList to result
            set counterOne to counterOne + 1
        end repeat
    end tell

    tell application "Google Chrome"
        make new window
        set counterOne to 1
        repeat tabCount times
            set tabVari to item counterOne of varList
            open location tabVari
            set counterOne to counterOne + 1
        end repeat
    end tell