Skip to main content
1 of 3

AppleScript List - can't get individual elements

I've got a bit of AppleScript that is frustrating the heck out of me. I need to get a list of all Safari tabs, then filter them down. I was expecting this to be very simple. It's not.

tell application "Safari"
    set tabSet to {}
    repeat with w in (get every window)
        repeat with t in (get every tab of w)
            set the end of tabSet to t
        end repeat
    end repeat
    --set tabSet to every tab of every window -- this doesn't work, although it would be convenient if there was a one-liner that did
    repeat with t in tabSet
        log (get name of t) -- this works
    end repeat
    set targetTab to first item of tabSet -- this doesn't work as expected
end tell

First of all, every tab of every window returns a nested list of lists of tabs (grouped by window) instead of just a flat list. I just ended up going with repeats. (If there is a way to make this a one-liner, it would be nice). But so far, so good - I can step through the list and print out all the tab names just to verify it's working. Great.

Finally, I try to get just one item with first item of tabSet (I'll be adding a whose ... filter in the future), and that's where the whole thing blows up. Here's what it gives me:

Result:
item 1 of {tab 1 of window id 15557 of application "Safari", tab 2 of window id 15557 of application "Safari", tab 3 of window id 15557 of application "Safari", tab 4 of...

It goes on and includes the whole list of tabs. I've tried every way I can think of to get this to work (every item of tabSet whose name is "X", item 1 of tabSet, etc.) but nothing has worked.

Any ideas? I come from a background of traditional programming languages and AppleScript is both one of the nicest (to read) as well as the most frustrating (to write) languages I've ever come across. The documentation is so incomplete I don't even know if I'm using the right terminology here, so hopefully this question actually makes sense.