3

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
  • I'm somewhat confused on your color selection. Are you wanting the next color in the list or are you wanting to randomly select a color? Are you also wanting it to randomly add one or more colors? – ʀ2ᴅ2 Dec 14 '18 at 14:01
  • Sorry, I have a list of keywords and I want to add a different colours for each of them. but I can create the list of colours already and add them for each keyword of my first list – Kevin Dec 14 '18 at 14:06
  • Give me a sec and let me finish this answer and tell me if I understand you correctly. – ʀ2ᴅ2 Dec 14 '18 at 14:06
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .