Safari Tabs — Choose-From Dialog

safari

(Christopher Stone) #1

Hey Folks,

Back in early 2005 I got sick of Safari’s lame UI for managing tabs and windows, and I wrote a script to improve upon things.

It’s been reworked and optimized a number of times, and the most current version is 3.0 – which had to be modified due to changes in Safari 10.1 on macOS 10.12.4.

I run mine from FastScripts.

-Chris


Display ⇢ Tab Names.zip (16.3 KB)

------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2010/10/16 00:42
# dMod: 2017/05/16 12:29
# Appl: Safari
# Task: Display a list of tab names and a count of both documents & tabs.
#     : Select a tab name to bring it to the front.
#     : Adjusted to deal properly with duplicate tab names on 2013-05-30.
# Libs: Only stock OSX components used.
# Osax: None 
# Tags: @Applescript, @Safari, @Choose, @Display, @Tabs, @Windows, @Count
# Note: Altered to accommodate a change in macOS 10.12.4.
# Reqs: macOS 10.12.4
# Vers: 3.00
------------------------------------------------------------------------------

try
   
   set sep to "----------------------------------------------------------------------------------------------------"
   
   tell application "Safari"
      
      if (count of document) ≥ 1 then
         set frontWindoc to document of front window
      else
         error "No pages are open in Safari"
      end if
      
      tell windows
         set docCount to count
         set currentTabIndex to index of current tab
         set tabCount to count of tabs
         set tabNames to name of tabs
      end tell
      
      if docCount < 10 then set docCount to "0" & docCount
      if tabCount < 10 then set tabCount to "0" & tabCount
      set hdrText to "Window Count:	" & docCount & return & "       Tab Count:	" & tabCount
      
      # Add tab-index to tab names to deal with duplicate names.
      set ndx to 0
      repeat with i in tabNames
         repeat with n from 1 to (length of i)
            if n < 10 then
               set _num to "0" & n
            else
               set _num to n
            end if
            tell (a reference to item n of i) to set its contents to its contents & " · [" & _num & "]"
         end repeat
      end repeat
      
      # Mark Current Tabs of each document window with '[✓]'
      # tabNames is a list of lists.
      set ndx to 0
      repeat with i in tabNames
         set ndx to ndx + 1
         tell (a reference to i's item (item ndx of currentTabIndex)) to set its contents to (its contents) & " ✓"
         if ndx = 1 then
            set currentTabName to result
         end if
         set end of i to sep
      end repeat
      
      set beginning of item 1 of tabNames to sep
      set AppleScript's text item delimiters to return
      set tabNames to paragraphs of (tabNames as text)
      
      tell me to set tabList to choose from list tabNames with title "Safari Tab Names by Window" with prompt hdrText ¬
         default items {currentTabName} with empty selection allowed and multiple selections allowed
      
      if tabList = false then return
      
      repeat with theTab in tabList
         
         set AppleScript's text item delimiters to " · "
         set tabName to text item 1 of theTab
         
         set AppleScript's text item delimiters to {"· ["}
         set tabIndex to (text item 2 of theTab)
         set AppleScript's text item delimiters to {"]"}
         set tabIndex to (text item 1 of tabIndex) as integer
         
         tell windows
            set tabID to (tabs whose name contains tabName and index is tabIndex)
         end tell
         
         repeat with i in tabID
            try
               set tabID to item 1 of i
               exit repeat
            end try
         end repeat
         
         try
            tabID as number
         on error eStr
            set AppleScript's text item delimiters to " of "
            repeat with i in (text items of eStr)
               if i contains "window id" then
                  set winOfTabID to run script ((contents of i) & " of application \"Safari\"")
                  exit repeat
               end if
            end repeat
         end try
         
         set winOfTabIdDoc to winOfTabID's document
         if current tab of winOfTabID ≠ tabID then set current tab of winOfTabID to tabID
         set index of winOfTabID to 1
         
      end repeat
      
      raiseWindowOne() of me
      
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
   if button returned of dDlg = "Copy" then set the clipboard to e
end try

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

(Ed Stockly) #2

This was very helpful. I’ve been working on a script that gets all the Safari tab URL, along with tab names, and displays them in a MyriadTables window, which I use as a script palette.

Then when I want to see a particular page I double-click on the item in the palette and it either goes to the tab in a window or opens a new tab. This helped in a couple ways.


(Jim Underwood) #3

That sounds great, Ed. Would you mind sharing your script?