Hi all,
I have created a script that has 41 items in a list, and am attempting to repeat for the list. For each item, I am going onto safari and searching about 5 websites, getting info and then logging it on a spreadsheet. All the functions work swell, but what I would like to be able to do is create a function that would allow me to start at a specific point in the list. This way, if something happens while I am running the script, I do not have to start over. I can just cancel and pick up where I left off latter.
set my_list to {“item 1”, “item 2”, “Item 3”} (like mentioned earlier, I have 41 items in this list)
set myvar to ¬
choose from list my_list with title “title” with prompt “prompt” default items “item 1” without multiple selections allowed
repeat with i in my_list from myvar
display dialog i
end repeat
i just typed this up as a quick example. Any help on how to start in a specific point would be greatly appreciated,
Many thanks in advance!
Try this
set my_list to {"item 1", "item 2", "Item 3"}
set myvar to choose from list my_list with title "title" with prompt "prompt" default items "item 1"
if myvar = false then return -- user chose cancel so stop
set myvar to item 1 of myvar -- "choose from list" command returns a list, so get 1st item
repeat with i from 1 to count of my_list -- find offset of the chosen item in my_list
if item i of my_list = myvar then exit repeat -- when item is found, exit loop, i will now have offset
end repeat
repeat with j from i to count of my_list
display dialog (item j of my_list)
end repeat
Here’s another way
set my_list to {"item 1", "item 2", "Item 3"}
set myvar to choose from list my_list with title "title" with prompt "prompt" default items "item 1"
if myvar = false then return -- user chose cancel so stop
set myvar to item 1 of myvar -- "choose from list" command returns a list, so get 1st item
set text item delimiters to return
set list_Text to my_list as text
set list_Text to text (offset of myvar in list_Text) thru -1 of list_Text
set i to count paragraphs of list_Text
repeat with j from i to count of my_list
display dialog (item j of my_list)
end repeat
Here an working and improved version of Robert’s 2nd example. If the item selected is an substring of an item before this code still works because it will take surrounding delimiters into consideration as well which makes it work a little better as long as the items doesn’t contain an return character.
set my_list to {"item 1", "item 2", "Item", "Item 4"}
set myvar to choose from list my_list with title "title" with prompt "prompt" default items "item 1"
if myvar = false then return -- user chose cancel so stop
set myvar to item 1 of myvar -- "choose from list" command returns a list, so get 1st item
tell AppleScript
set oldTIDs to text item delimiters
set text item delimiters to return
set listAsText to my_list as string
if listAsText begins with myvar & return then
set start to 1
else if listAsText ends with return & myvar then
set start to count of my_list
else
set text item delimiters to return & myvar & return
set prefix to text item 1 of listAsText
set text item delimiters to return
set start to (count text items of prefix) + 1
end if
set text item delimiters to oldTIDs
end tell
repeat with i from start to count my_list
display dialog item i of my_list
end repeat
Thank you all for the help, and all apologies for taking so long to get back to you. I really appreciate it!
Ryan