MacOS – Applescript : generate a new list based on existing list

applescriptmacos

my script is generating a list (random number of items) and for each item I like to add a colour.

I can do it manually :

> set myList to {{"demo", "#5cdf64"}, {"icloud.com", "#FFFF00"}, {"more
> random e.g", "#FF0000"}}

but how can I add a different colour automatically to the list and based on the number of item on the first list?

I know how to count the primary list and do an action for each items :

set listSize to count of myList

set theList to {"demo", "demo1", "demo2", "demo2"}
repeat with a from 1 to length of theList
    set theCurrentListItem to item a of theList
    -- Process the current list item

end repeat

I think I'm nearly there the only thing is I'm not adding but replacing the items :

set theList to {"Demo", "ok", "blabla", "demo2"}
set ColortheList to {"5cdf64", "FFFF00", "FF0080", "FF1000"}
set myNewList to ""

repeat with a from 1 to length of theList
    set theCurrentListItem to item a of theList
    set myNewList to {item a of theList, item a of ColortheList}

end repeat

also tried

copy {item a of theList, item a of ColortheList} to myNewList

Best Answer

If you're wanting myNewList to return a list of lists I think you're trying to do:

set theList to {"Demo", "ok", "blabla", "demo2"}
set ColortheList to {"5cdf64", "FFFF00", "FF0080", "FF1000"}
set myNewList to {}

repeat with a from 1 to length of theList
    copy ({item a of theList, item a of ColortheList}) to the end of the |myNewList|
end repeat

return myNewList

A little fun with random number. You can modify the color selection and then randomly add to a list:

set theList to {"Demo", "ok", "blabla", "demo2", "foobar"}

set colorList to {"5cdf64", "FFFF00"}
set colorLength to length of colorList

set myNewList to {}

repeat with a from 1 to length of theList
    set colorPick to random number from 1 to colorLength
    copy ({item a of theList, item colorPick of colorList}) to the end of the |myNewList|
end repeat

return myNewList

If you're wanting a string approach try:

set theList to {"Demo", "ok", "blabla", "demo2"}
set ColortheList to {"5cdf64", "FFFF00", "FF0080", "FF1000"}
set myNewList to ""

repeat with a from 1 to length of theList
    set result to ({item a of theList, item a of ColortheList})
    set myNewList to myNewList & (result & " ")
end repeat

The above can be modified in the result. Let me know if I'm understanding you or if you're needing something different.