1

Trying to switch tabs in Safari with osascript. I have tried multiple versions of the following:

osascript -e 'tell Application "Safari"' -e 'keystroke "2" using command down' -e 'end tell

Tried replacing "Safari" with "System Events and "2" with "-2". But nothing I do is working. Can someone tell me what I'm doing wrong?

0

Use the key code 19 command, because the keystroke "2" command simulate the key code 84 (numeric keypad) instead of key code 19.

tell application "System Events" to key code 19 using command down

To know the key code of some key, download this application --> https://itunes.apple.com/us/app/key-codes/id414568915?mt=12


But, you do not need GUI Scripting to select a tab in Safari, use this script

tell application "Safari" to tell front window to set current tab to tab 2 -- this select the second tab
  • Thank you both of these examples work. Quick question - How would it be phrased if the command uses 2 or more keyboard modifiers like command and alt at the same time? – Seamus May 4 '16 at 19:28
  • Never mind I got it. In case anyone else needs this its osascript -e 'tell application "System Events" to key code "19" using {option down, command down}' -e 'end tell' Thanks for your help. – Seamus May 4 '16 at 20:44
  • @Seamus, you do not need to use -e 'end tell' when the tell statement in in the to form. In other words tell application "AppName" to ... negates the need for an end tell. – user3439894 May 4 '16 at 20:56
1

It's System Events that handles the keystrokes.

In normal AppleScript the code would look like:

tell application "Safari"
    activate
    delay 0.5
    tell application "System Events"
        key code 19 using command down
    end tell
end tell

As an osascript command line, it would be:

osascript -e 'tell application "Safari" to activate' -e 'delay 0.5' -e 'tell application "System Events" to key code 19 using command down'

The osascript command line from above shown below as multiple lines for clarity:

osascript -e 'tell application "Safari" to activate' \
          -e 'delay 0.5' \
          -e 'tell application "System Events" to key code 19 using command down'

Notes: Before telling System Events to keystroke, the target of the keystroke must have focus. So first, Safari must be made the frontmost window with activate. You must then allow a moment for it to have focus before the keypress and that is done with delay 0.5 and the value of delay may need to be adjusted in seconds and or decimal fractions thereof. So once Safari is the frontmost window, then System Events can issue the keystrokes.

This of course assumes Safari is already open with at least one window having two tabs!

The keystroke used herein with Safari is a Safari 9 (El Capitan) keyboard shortcut as shown in: Safari 9 (El Capitan): Safari keyboard and other shortcuts

Select one of your first nine tabs      Command-1 to Command-9

  • This isn't working for me. It works if I manually press Cmd 2 but otherwise it just stays on the same tab after safari activates. I tried delaying for 4.0 and still nothing. – Seamus May 4 '16 at 9:31
  • You need to allow setting the tab via cmd-number in settings of safari under the tabs setting – Haroen Viaene May 4 '16 at 9:33
  • I've done that. If I manually push it on the keyboard it works fine. But nothing happens with the script. – Seamus May 4 '16 at 9:37
  • @Seamus, I'm not sitting at a system that's running OS X 10.11 and Safari 9 to test this however later today I can test this under OS X 10.11. This works under OS X 10.8.5 when substituting the appropriate keystroke and I assumed the same paradigm would work, sorry. Also the delay doesn't need to be 4.0 and typically 1 second of fraction thereof is adequate. – user3439894 May 4 '16 at 9:49
  • I appreciate your help. Yeah the 4 second delay was overkill but I just thought I'd try it to rule out the possibility. It is strange. It works just fine using the same format with letter presses like 'keystroke "a" using command down' so I don't see why this would be any different. – Seamus May 4 '16 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .