Skip navigation

close safari tabs doesn't work full screen

549 Views 5 Replies Latest reply: Apr 8, 2013 10:05 AM by Pierre L. RSS
davidsignal Level 3 Level 3 (525 points)
Currently Being Moderated
Apr 7, 2013 7:54 PM

I have a script that does various different things on some webpages and then finishes with a few uneeded extra tabs.  The script ends by closing these extra tabs using this:

 

  repeat with i from (count tabs) to 1 by -1

      set thisTab to tab i

      if ("a string to match" is in name of thisTab) and (thisTab is not visible) then close thisTab

  end repeat

My issue is that this doesn't seem to work when Safari is in full-screen mode.  I have tried various different versions of this segment and usually end up with "thisTab is undefined".  Some methods I have tried result in the Applescript editor showing that the conditions matched and that the close tab event was executed, but the tabs don't close.... they stay right where they are.

 

If I take Safari out of full-screen mode, this works fine.  Is there some different way that I need to do this?

  • pjdube Level 3 Level 3 (830 points)
    Currently Being Moderated
    Apr 7, 2013 9:48 PM (in response to davidsignal)

    It would help if you posted the full script.

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Apr 8, 2013 6:52 AM (in response to davidsignal)

    Maybe you might try GUI Scripting. The following script, for example, should close all tabs whose name does not contain "Apple Support Communities" in the first Safari window:

     

    tell application "Safari"

        activate

        set theWindow to window 1 whose id > 0

        repeat (count tabs of theWindow) times

            if not (name of theWindow contains "Apple Support Communities") then

                tell application "System Events"

                    keystroke "w" using {command down}

                end tell

            else

                tell application "System Events" to tell process "Safari"

                    keystroke tab using {control down} -- go to next tab

                end tell

            end if

            delay 1 -- adjust if necessary

        end repeat

    end tell

     

    Tested on OS X 10.8.3. (You must enable access for assistive devices in the Accessibility System Preference pane.)

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Apr 8, 2013 7:28 AM (in response to Pierre L.)

    Actually, I must correct what I've said in my previous post. In this particular case, it is not necessary to enable access for assistive devices in the Accessibility System Preference pane.

     

    Moreover, the script can be written much more elegantly as follows:

     

    tell application "Safari"

        activate

        set theWindow to window 1 whose id > 0

        repeat (count tabs of theWindow) times

            tell application "System Events"

                if not (name of theWindow contains "Apple Support Communities") then

                    keystroke "w" using {command down}

                else

                    keystroke tab using {control down} -- go to next tab

                end if

            end tell

            delay 1 -- adjust if necessary

        end repeat

    end tell

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Apr 8, 2013 10:05 AM (in response to davidsignal)

    My pleasure.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.