Applescript to search open Safari Tabs

General discussions about LaunchBar
Post Reply
greenblue
Rank 1
Rank 1
Posts: 38
Joined: Sun Aug 07, 2011 9:27 am

Applescript to search open Safari Tabs

Post by greenblue » Sat May 18, 2013 12:16 am

I found this great Applescript that will search through Safari's open tabs and activate it. I found that if I save it as an Applescript app, it works great with LaunchBar.

However, I'd like to input the search term directly into to LaunchBar by using the "on handle_string(theString)" somewhere in the script. Is this possible to do with this script?

Here's the script:

Code: Select all

set question to display dialog ("Find Safari tab:") default answer ""
set searchpat to text returned of question

tell application "Safari"
   set winlist to every window
   set winmatchlist to {}
   set tabmatchlist to {}
   set tabnamematchlist to {}
   repeat with win in winlist
      set ok to true
      try
         set tablist to every tab of win
      on error errmsg
         --display dialog name of win as string
         set ok to false
      end try
      if ok then
         repeat with t in tablist
            if searchpat is in (name of t as string) then
               set end of winmatchlist to win
               set end of tabmatchlist to t
               set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
               --display dialog name of t as string
            else if searchpat is in (URL of t as string) then
               set end of winmatchlist to win
               set end of tabmatchlist to t
               set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
               --display dialog name of t as string
            end if
         end repeat
      end if
   end repeat
   if (count of tabmatchlist) = 1 then
      --display dialog "one!"
      set w to item 1 of winmatchlist
      set t to item 1 of tabmatchlist
      set current tab of w to t
      set index of w to 1
   else if (count of tabmatchlist) = 0 then
      display dialog "No matches"
   else
      set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
      set AppleScript's text item delimiters to "."
      if whichtab is not equal to false then
         set tmp to text items of (whichtab as string)
         set w to (item 1 of tmp) as integer
         set t to (item 2 of tmp) as integer
         set current tab of window id w to tab t of window id w
         set index of window id w to 1
      end if
   end if
end tell


Thank you!

signupsammy
Posts: 12
Joined: Mon Mar 15, 2010 5:37 am

Re: Applescript to search open Safari Tabs

Post by signupsammy » Mon May 20, 2013 4:31 am

Hi greenblue,

Try this and see if it works for you:

Code: Select all

on handle_string(theLBString)
   tell application "LaunchBar" to hide
   
   tell application "Safari"
      activate
      set winlist to every window
      set winmatchlist to {}
      set tabmatchlist to {}
      set tabnamematchlist to {}
      repeat with win in winlist
         set ok to true
         try
            set tablist to every tab of win
         on error errmsg
            --display dialog name of win as string
            set ok to false
         end try
         if ok then
            repeat with t in tablist
               if theLBString is in (name of t as string) then
                  set end of winmatchlist to win
                  set end of tabmatchlist to t
                  set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
                  --display dialog name of t as string
               else if theLBString is in (URL of t as string) then
                  set end of winmatchlist to win
                  set end of tabmatchlist to t
                  set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
                  --display dialog name of t as string
               end if
            end repeat
         end if
      end repeat
      if (count of tabmatchlist) = 1 then
         --display dialog "one!"
         set w to item 1 of winmatchlist
         set t to item 1 of tabmatchlist
         set current tab of w to t
         set index of w to 1
      else if (count of tabmatchlist) = 0 then
         display dialog "No matches"
      else
         set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
         set AppleScript's text item delimiters to "."
         if whichtab is not equal to false then
            set tmp to text items of (whichtab as string)
            set w to (item 1 of tmp) as integer
            set t to (item 2 of tmp) as integer
            set current tab of window id w to tab t of window id w
            set index of window id w to 1
         end if
      end if
   end tell
   
end handle_string

greenblue
Rank 1
Rank 1
Posts: 38
Joined: Sun Aug 07, 2011 9:27 am

Re: Applescript to search open Safari Tabs

Post by greenblue » Fri May 24, 2013 12:20 am

signupsammy wrote:Try this and see if it works for you:


Works perfect! I saved it from Applescript Editor as a script into the LaunchBar actions folder (then updated actions) and it works flawlessly.

THANK YOU! :D

signupsammy
Posts: 12
Joined: Mon Mar 15, 2010 5:37 am

Re: Applescript to search open Safari Tabs

Post by signupsammy » Mon May 27, 2013 5:53 am

You are welcome -- glad it worked out for you.

Post Reply