9 Replies Latest reply: Nov 11, 2014 3:38 PM by Jacques Rioux
Kenneth Hjulstrom Level 2 (440 points)

Hi,

 

I would like to move the current tab in a Safari window all the way to the left via Applescript, to accomplish the same result as can be done by dragging the tab to that position.  I tried saying

 

set index of current tab to 1


, but that resulted in an error.  Any advice would be appreciated.

 

Thanks,

 

Ken


12 G4 PowerBook, Mac OS X (10.5.8), 1.125 Gb RAM
  • X423424X Level 6 (14,215 points)

    tell front window of application "Safari" to set current tab to tab 1

  • Frank Caggiano Level 7 (25,722 points)

    Things like

     

    tell application "Safari"

           tell front window

                  move last tab to before first tab

           end tell

    end tell

     

    or else

     

    tell application "Safari"

              tell front window

                        move tab 1 to after last tab

              end tell

    end tell


    use to work. But just tried it in 5.1.1 and the window closes with no error messages anywhere. So not sure when thsi stopped working or if there is another way in 5.1 to accomplish this.

     

    BTW the reason you get the error is that the tab index is read only.

  • Kenneth Hjulstrom Level 2 (440 points)

    Hi X423424X,

     

    Your suggestion seems to give focus to the specified tab, as if the script language were saying "make tab 1 be the current tab".  I'm looking for code that will actually change the order that the tabs are shown in the browser window, not a focus change, though your code suggestion might be useful as part of the solution.

     

    Thanks,

     

    Ken

  • Kenneth Hjulstrom Level 2 (440 points)

    Hi Frank,

     

    It looks to me like your code is correct, but i can also confirm that when I try to run it on Safari 5.1.1, the window closed with no error message.  I found essentially your exact code here:

     

    http://books.google.com/books?id=ssaTrBGoO50C&pg=PA339&lpg=PA339&dq=applescript+ %22move+tab%22&source=bl&ots=_Lhd5IT8TB&sig=JRo_TX5XR19pRoEi1J9MfG354E0&hl=en&ei =2ayuTqvHLa690QGjrq2RDw&sa=X&oi=book_result&ct=result&resnum=6&ved=0CFEQ6AEwBQ#v =onepage&q&f=false

     

    but that code just closed the Safari window as well.  If nobody comes up with working code to do this, I'll submit it to Apple as a possible bug.

     

    Thanks,

     

    Ken

  • X423424X Level 6 (14,215 points)

    Kenneth Hjulstrom wrote:

     

    Hi X423424X,

     

    Your suggestion seems to give focus to the specified tab, as if the script language were saying "make tab 1 be the current tab".

     

    You're original post said,

    I would like to move the current tab in a Safari window all the way to the left via Applescript, to accomplish the same result as can be done by dragging the tab to that position.

     

    I interpreted that as if you physically grab the current tab, which has the focus by definition, and drag it to the far left, where it would still have the focus.  You said nothing about changing focus in that post.  You wanted to know what was wrong with your applescript line so that's what I specifically replied to.

  • Kenneth Hjulstrom Level 2 (440 points)

    Hi X423424X,

     

    Your original interpretation of my question was exactly correct.  However, when I implemented your Applescript statement, it did not move the tab to the left as was what I had requested.  When I ran your Applescript statement, it had the effect of switching the focus to to tab 1, which is the the tab farthest to the left.  I tried this with a Safari window that had three tabs, and with the rightmost tab having focus.  After running your Applescript statement, none of the tabs changed position, but tab 1 had the focus after running your statement.  Since you know exactly what my original question meant when you posted this solution, obviously I'm doing something wrong.  Please explain how you got this statement:

     

    tell front window of application "Safari" to set current tab to tab 1

     

    to move the tab with focus to the far left.

     

    Thank you,

     

    Ken

  • X423424X Level 6 (14,215 points)

    Hmm, just re-ran it.  Looks like you are correct.  All it does was set the current tab (focus) to the first tab, not move the right-most tab to the left-most position.

  • Kenneth Hjulstrom Level 2 (440 points)

    Hi,

     

    A bit more than three years after I originally posted this question, I have an answer.  The following script creates a new Safari tab, which by default, is positioned at the far right of the tab list, then moves it to the beginning (far left) of the tab list, sets focus to the new tab, and then populates it:

     

    ++++++++++++++++++++++++++++++++++++++++++++

     

    tell application "Safari"

      tell window 1

      set current tab to (make new tab)

     

      move tab (get index of last tab) to beginning of tabs

      set current tab to tab 1

     

      set URL of front document to "http://www.apple.com"

      end tell

    end tell

     

    ++++++++++++++++++++++++++++++++++++++++++++

     

    Ken

  • Jacques Rioux Level 4 (3,082 points)

    Hi,

     

    You can create a new tab at the specified position without using the move command.

     

    tell application "Safari"
        tell window 1 to set current tab to (make new tab at beginning of tabs with properties {URL:"http://www.apple.com"})
    end tell