• 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
Re: need explanation for trivial Safari script behavior
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: need explanation for trivial Safari script behavior


  • Subject: Re: need explanation for trivial Safari script behavior
  • From: Christopher Stone <email@hidden>
  • Date: Tue, 19 Jul 2016 08:49:14 -0500

On Jul 18, 2016, at 13:43, Mitchell L Model <email@hidden> wrote:
Good to know — never occurred to me to look for the document of a browser window.
…
set winList to windows where its document is not missing value


Hey Mitchell,

I spent many hours working with Safari windows and tabs before discovering this solution.

Interestingly — to me anyway — this does NOT work:

repeat with theWindow in the windows
if the document of theWindow is missing value then …

For windows without a document I get the error "No result was returned from some part of this _expression_", with "missing value" selected.

In my opinion this is a bug.

-------------------------------------------------------------------------------------------
set noDocList to {}

tell application "Safari"
   repeat with theWindow in the windows
      set winDoc to document of theWindow
      try
         set docName to name of winDoc
      on error
         set end of noDocList to name of theWindow
      end try
   end repeat
end tell

noDocList
-------------------------------------------------------------------------------------------

winDoc can be a null-result and (I think) should produce a missing value instead.

Appended is a script I originally wrote back in 2004 or 2005 and rewrote several years ago to use only vanilla AppleScript. (It used to rely on the Satimage.osax.)

I spent a lot of time trying to make it work properly and to make it as fast as possible.

--
Best Regards,
Chris

------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# dCre: 2010/10/16 00:42
# dMod: 2016/07/19 08:21
# Appl: Safari
# Task: Display a list of tab names and also 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
-------------------------------------------------------------------------------------------

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 (every window where its document is not missing value)
         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
      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
      tabNames
      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 _tab to choose from list tabNames with title "•• Safari Tab Names ••" with prompt hdrText ¬
         default items {currentTabName} with empty selection allowed

      

      if _tab = false then return

      

      set _tab to item 1 of _tab # De-listify.
      set AppleScript's text item delimiters to " · "
      set tabName to text item 1 of _tab

      

      set AppleScript's text item delimiters to {"· ["}
      set tabIndex to (text item 2 of _tab)
      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

      

      if winOfTabIdDoc ≠ frontWindoc then
         set newDoc to make new document
         set index of winOfTabID to 1
         close newDoc
      end if

      

   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

-------------------------------------------------------------------------------------------

 _______________________________________________
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: need explanation for trivial Safari script behavior
      • From: Mitchell L Model <email@hidden>
References: 
 >need explanation for trivial Safari script behavior (From: Mitchell L Model <email@hidden>)
 >Re: need explanation for trivial Safari script behavior (From: Axel Luttgens <email@hidden>)
 >Re: need explanation for trivial Safari script behavior (From: Christopher Stone <email@hidden>)
 >Re: need explanation for trivial Safari script behavior (From: Mitchell L Model <email@hidden>)
 >Re: need explanation for trivial Safari script behavior (From: Christopher Stone <email@hidden>)
 >Re: need explanation for trivial Safari script behavior (From: Mitchell L Model <email@hidden>)

  • Prev by Date: Re: Help for optimization of my script
  • Next by Date: Re: need explanation for trivial Safari script behavior
  • Previous by thread: Re: need explanation for trivial Safari script behavior
  • Next by thread: Re: need explanation for trivial Safari script behavior
  • Index(es):
    • Date
    • Thread