• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Safari - Bring a tab containing a given URL to the front
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Safari - Bring a tab containing a given URL to the front


  • Subject: Safari - Bring a tab containing a given URL to the front
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 20 May 2017 18:18:19 -0500

Hey Folks,

I just posted with the subject “Getting Tab Index and Window ID in Safari”.

This script uses that technique to find a tab (or tabs) containing the given URL and brings the first hit to the front.

--
Best Regards,
Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/20 17:30
# dMod: 2017/05/20 18:12
# Appl: Safari
# Task: Bring a tab containing a given URL to the front.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Bring, @Tab, @Containing, @URL, @Frontmost
------------------------------------------------------------------------------

set theURL to "http://macscripter.net"
tell application "Safari" to set tabWinList to tabs of windows whose URL contains theURL

try
    tabWinList / 0
on error eMsg
    set AppleScript's text item delimiters to {"Can’t make ", " into type real.", "of application \"Safari\"", " of window id", "«class bTab»"}
    set tabWinList to text items of eMsg
    set AppleScript's text item delimiters to linefeed
    set tabWinList to tabWinList as text
    set tabWinList to words of tabWinList
    repeat with theWord in tabWinList
        try
            set contents of theWord to theWord as integer
        on error
            set contents of theWord to "null"
        end try
    end repeat
end try

if tabWinList ≠ {} then
    set {tabIndex, winID} to items 1 thru 2 of (integers of tabWinList)
    tell application "Safari"
        tell window id winID
            set index to 1
            raiseWindowOne() of me
            set current tab to tab tabIndex
        end tell
    end tell
end if

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on raiseWindowOne()
    tell application "System Events"
        tell application process "Safari"
            tell window 1 to perform action "AXRaise"
        end tell
    end tell
end raiseWindowOne
------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Safari - Bring a tab containing a given URL to the front
      • From: Axel Luttgens <email@hidden>
  • Prev by Date: Getting Tab Index and Window ID in Safari
  • Next by Date: Re: Getting Tab Index and Window ID in Safari
  • Previous by thread: Re: Getting Tab Index and Window ID in Safari
  • Next by thread: Re: Safari - Bring a tab containing a given URL to the front
  • Index(es):
    • Date
    • Thread