set theList to {"1", "", "", "apple", "acda", "", "3454", "ImportantStuff", "important1", "important2", "important3", "NotImportantStuff", "2", "", "efrg"}
set theImportant to items between "ImportantStuff" and "NonImportantStuff" of theList

How do I make it so AppleScript takes the items in between "ImportantStuff" and "NonImportantStuff" and assign a variable to those items. the variable "theList" is a substitute, the amount of items that are important (between the two specified items) is indefinite--along with the amount of items before and after the specified items.


Here's an alternative implementation that will be faster and is a lot more compact:

set L to {"1", "", "", "apple", "acda", "", "3454", "ImportantStuff", ¬
   "important1", "important2", "NotImportantStuff", "2", "", "efrg"}

set {i, j} to {1, -1}

tell L
    repeat until item i = "ImportantStuff"
        set i to i + 1
    end repeat
    repeat until item j = "NotImportantStuff"
        set j to j - 1
    end repeat
end tell

return items (i + 1) thru (j - 1) of L
-- OR: set myVar to items (i + 1) thru (j - 1) of L

It doesn't iterate through every item in the list, as that wastes time. Instead, we hone in from the left to find the index where the important stuff begins; then hone in from the right to find the index where the important stuff ends.

Then you can simply retrieve the items between those two indices.

Method 2: Text Manipulation

Less generally, if you know your list contains only simple text items, then you can use text item delimiters to coerce the list into a string and manipulate the string:

set my text item delimiters to linefeed

set L to {"1", "", "", "apple", "acda", "", "3454", "ImportantStuff", "important1", ¬
   "important2", "important3", "NotImportantStuff", "2", "", "efrg"}

set textList to L as text

set my text item delimiters to {"ImportantStuff", "NotImportantStuff"}

set importantStuffText to text item 2 of textList

The first set of text item delimiters transform the list into a string where each item in the list occupies a single line in the string. The second set of text item delimiters splits the string at the two places where the keywords are found. This allows us to retrieve the bits in between, which, as it stands, will return:


If there are multiple occurrences of the keywords, the important stuff will be all the even numbered text items.

To change the string back into a list again, you can do this:

set importantStuffList to paragraphs 2 thru -2 of importantStuffText
    --> {"important1", "important2", "important3"}
  • Amazing to see how the typically verbose AppleScript language can be compacted. – Graham Miln Jun 1 at 11:23
  • In this case, it's not that I've used briefer AppleScript syntax, but rather used a briefer algorithm in my approach. – CJK Jun 1 at 11:26

Manual Filter

One approach is to manually filter the list with a repeat loop:

set theList to {"1", "", "", "apple", "acda", "", "3454", "ImportantStuff", "important1", "important2", "important3", "NotImportantStuff", "2", "", "efrg"}

-- put the filtered items into subList
set subList to {}

-- note when iterating through important items
set withinImportant to false

-- examine every item in the list
repeat with i from 1 to length of theList
    set anItem to item i of theList

    -- are we within an important section?
    if withinImportant then

        -- have we reached the end of the important section?
        if anItem is "NotImportantStuff" then
            set withinImportant to false
            -- append to subList
            set subList to subList & anItem
        end if


        -- is this item the start of the important items
        if anItem is "ImportantStuff" then
            set withinImportant to true
        end if
    end if
end repeat

See Manipulating Lists of Items and AppleScript “list” examples for more examples of working with lists in AppleScript.

  • You can exit the repeat loop once you reach the end of the important section, which would save iterating through unnecessary items at the end. But, overall, it might be better (many fewer operations and more space efficient depending on final method) if, instead of building a working list of items iteratively, the index of the two pertinent list items are sought and then used to return items x thru y of theList. – CJK Jun 1 at 11:09
  • CJK's improvements are worth experimenting with. This answer's implementation will pick up multiple blocks of "important stuff" but CJK's is more compact. – Graham Miln Jun 1 at 11:24
  • I get error "Can’t set sublist of {} to some object." number -10006 from «class LST-» of {} when trying to run this script. sublist shows in bold blue like a command but I don't have anything in Plug-ins in Preferences of Script Editor. After some searching, I did find out I had Satimage installed. Which now explains some other anomalies I've run into lately. – user3439894 Jun 1 at 15:17
  • I can confirm this script works on plain macOS 10.14. Does changing subList to another word fix the problem? If not, maybe worth asking if anyone else can reproduce the behaviour. – Graham Miln Jun 1 at 18:33

You must log in to answer this question.