I do not think Safari can be made to return the exact list you want “all in one go”. You could build it yourself by looping across the windows and collecting the list of matching tabs, but that is bound to be slower since you have to send a separate query for each window. So I think you will have to do some “post processing”.
In some circles, the function you want is called “flatten” (make nested lists into a single “flat” list).
tell application "Safari"
tell (windows whose visible is true) to set matching_tabs to (tabs whose name begins with "MacScripter")
end tell
to flatten(|list|)
-- There are other ways to write "flatten". This is one of the shorter ones (though it may not be the fastest).
if |list| is missing value then return {} -- a "true" (functional programming) flatten would likely leave this kind of filtering to another function
if |list| is {} then return {}
if class of |list| is not list then return {|list|}
return flatten(first item of |list|) & flatten(rest of |list|)
end flatten
set orig to matching_tabs
set matching_tabs to flatten(matching_tabs)
return {orig:orig, flat:matching_tabs} --> {orig:{{tab 1 of window id 10763 of application "Safari", tab 2 of window id 10763 of application "Safari"}, missing value, missing value, missing value}, flat:{tab 1 of window id 10763 of application "Safari", tab 2 of window id 10763 of application "Safari"}}
(*
* The code below just makes sure flatten works like we expect.
* If you make changes to flatten, you should use and extend the tests to make sure there are no regressions in functionality.
*)
to assert(|result|, expected_result)
-- This may not work for general AppleScript references since it may not be possible to coerce all classes to strings. But it works OK for the testing done below (though the message can still be misleading since the text version of the list does not show the actual list structure).
if |result| is not expected_result then error "Expected " & expected_result & "; got " & |result| & "."
end assert
assert(flatten({}), {})
assert(flatten(1), {1})
assert(flatten({1}), {1})
assert(flatten({1, 2}), {1, 2})
assert(flatten({1, 2, 3}), {1, 2, 3})
assert(flatten({{}, 1, 2, 3}), {1, 2, 3})
assert(flatten({1, {}, 2, 3}), {1, 2, 3})
assert(flatten({1, 2, {}, 3}), {1, 2, 3})
assert(flatten({1, 2, 3, {}}), {1, 2, 3})
assert(flatten({missing value, 1, 2, 3}), {1, 2, 3})
assert(flatten({1, missing value, 2, 3}), {1, 2, 3})
assert(flatten({1, 2, missing value, 3}), {1, 2, 3})
assert(flatten({1, 2, 3, missing value}), {1, 2, 3})
assert(flatten({{1}, 2, 3}), {1, 2, 3})
assert(flatten({1, {2}, 3}), {1, 2, 3})
assert(flatten({1, 2, {3}}), {1, 2, 3})
assert(flatten({{{1}}, 2, 3}), {1, 2, 3})
assert(flatten({1, {{2}}, 3}), {1, 2, 3})
assert(flatten({1, 2, {{3}}}), {1, 2, 3})
assert(flatten({{{1}, 2}, 3}), {1, 2, 3})
assert(flatten({{1, {2}}, 3}), {1, 2, 3})
assert(flatten({{1, 2}, {3}}), {1, 2, 3})
assert(flatten({{1}, {2, 3}}), {1, 2, 3})
assert(flatten({1, {{2}, 3}}), {1, 2, 3})
assert(flatten({1, {2, {3}}}), {1, 2, 3})
assert(flatten({{1, {}}, {}, {2, {}, 3}}), {1, 2, 3})
assert(flatten({1, {}, {{2, {}}, 3, {}}}), {1, 2, 3})
assert(flatten({1, {}, {2, {}, {3, {}}}}), {1, 2, 3})
assert(flatten({{1, missing value}, missing value, {2, missing value, 3}}), {1, 2, 3})
assert(flatten({1, missing value, {{2, missing value}, 3, missing value}}), {1, 2, 3})
assert(flatten({1, missing value, {2, missing value, {3, missing value}}}), {1, 2, 3})
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 4 Public Beta (4528.16)
Operating System: Mac OS X (10.4)