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
here would be repeatconvenient
with t in tabSet
set firstTab logto (getfirst nameitem of t)tabSet -- this works
, endsee repeatedit
set targetTab to first item of tabSet --whose thisname doesn'tis work"Untitled" as-- expectedthis fails
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,The result I try to get just one item withwas getting from first item of tabSet
(I'll be addingwas throwing me off because it looked like it was returning a whose ...
filter in the future), and that's whereform of the whole thing blows up. Here's what it gives meentire list:
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 goesThe actual problem occurs on and includes the whole list of tabs. I've tried every wayfilter line where I can think oftry to get this to workthe (everyfirst item of tabSet whose name is "X"
, item 1 of tabSet"Untitled"
, 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 writeReplace with a valid name of course) languages I've ever come across. The documentation is so incomplete I don't even know if I'm using the right terminology hereThen, so hopefullyI get this question actually makes sense.error:
error "Safari got an error: Can’t get tab 1 of window id 15561 whose name = \"Untitled\". Invalid index." number -1719