Skip to main content
Bumped by Community user
Tweeted twitter.com/askdifferent/status/1005722949104603137
added 690 characters in body; edited title
Source Link

Safari AppleScript List - filtering by propertyCan't Filter Tabs of Inactive Windows

EDIT 2:

I added the following block:

repeat with t in tabSet
    set n to (get name of t)
    try
        set targetTab to (first item in tabSet whose name is n)
        set targetWindow to (first window whose tabs contains targetTab)
        log (get id of targetWindow) & (get name of t)
    on error
        log "ERROR: " & n
    end try
end repeat

This revealed that, once again, the problem was not as it seemed, but was actually something completely different:

Only tabs in the frontmost window can be filtered. It appears that the statement first item of tabSet whose name is "Untitled" fails if the tab "Untitled" is in any window other than the frontmost window.

Any ideas?

AppleScript List - filtering by property

Safari AppleScript - Can't Filter Tabs of Inactive Windows

EDIT 2:

I added the following block:

repeat with t in tabSet
    set n to (get name of t)
    try
        set targetTab to (first item in tabSet whose name is n)
        set targetWindow to (first window whose tabs contains targetTab)
        log (get id of targetWindow) & (get name of t)
    on error
        log "ERROR: " & n
    end try
end repeat

This revealed that, once again, the problem was not as it seemed, but was actually something completely different:

Only tabs in the frontmost window can be filtered. It appears that the statement first item of tabSet whose name is "Untitled" fails if the tab "Untitled" is in any window other than the frontmost window.

Any ideas?

Initial clarification revealed the problem was not as it initially seemed. Enough information has been left to show what the initial confusion was so it may help someone in the future.
Source Link

AppleScript List - can't get individual elementsfiltering by property

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

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.

AppleScript List - filtering by property

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 -- a one-liner here would be convenient
    
    set firstTab to first item of tabSet -- works, see edit
    set targetTab to first item of tabSet whose name is "Untitled" -- this 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.

The result I was getting from first item of tabSet was throwing me off because it looked like it was returning a form of the entire list:

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...

The actual problem occurs on the filter line where I try to get the first item of tabSet whose name is "Untitled". (Replace with a valid name of course). Then, I get this error:

error "Safari got an error: Can’t get tab 1 of window id 15561 whose name = \"Untitled\". Invalid index." number -1719
Source Link

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.