Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use AppleScript to select specific tabs in Safari 3 Web Browsers
One thing that really bugs me about Safari is the inability to have a keyboard shortcut for each tab. In Firefox, you can use Command-1 to get the first tab, Command-2 to get the second tab, and so on. (You can cycle between all open tabs with Command-{ and Command-}, but you can't just to a specific tab.) With the release of Safari 3, there is now AppleScript support for selecting tabs. To select tab 1 you can do this:
tell front window of app "Safari" to set current tab to tab 1
I wrote nine similar scripts for tabs one through nine, and a script for activating the last tab. These go in ~/Library » Scripts » Applications » Safari. Then I assigned each script to a trigger in Quicksilver that is only available when Safari is active. If you have the scripts menu active, you should be able to assign keyboard shortcuts to each file. However, these shortcuts don't work for me within Safari for some reason. Perhaps someone else can figure this one out.

The following script should generate the AppleScripts for you:
#!/usr/bin/env bash

SAFARI_SCRIPTS_PATH="$HOME/Library/Scripts/Applications/Safari"

mkdir -p "$SAFARI_SCRIPTS_PATH"

# tabs 1-9
for (( i = 1; i < 10; i++ )); do
script_filename="$SAFARI_SCRIPTS_PATH/Activate Tab $i"
osascript<<-EOF
  script setTab
    tell front window of app "Safari" to set current tab to tab $i
  end script
  store script setTab in posix file "$script_filename" replacing yes
EOF
echo "Created $script_filename"
done

# last tab
script_filename="$SAFARI_SCRIPTS_PATH/Activate Last Tab"
osascript<<-EOF
  script setTab
    tell front window of app "Safari" to set current tab to last tab
  end script
  store script setTab in posix file "$script_filename" replacing yes
EOF
echo "Created $script_filename"
echo "Done"
[robg adds: The shell script (and the AppleScripts it created) worked for me with Safari 3.0.3. To use the shell script, copy and paste the text into your favorite pure text editor, save it somewhere convenient, then cd to that directory in Terminal. Make your script executable (chmod a+x script_name), and then execute it (./script_name).]
    •    
  • Currently 2.50 / 5
  You rated: 4 / 5 (8 votes cast)
 
[16,987 views]  

Use AppleScript to select specific tabs in Safari 3 | 2 comments | Create New Account
Click here to return to the 'Use AppleScript to select specific tabs in Safari 3' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use AppleScript to select specific tabs in Safari 3
Authored by: bbeck13 on May 09, '08 06:45:24PM
I'm using this hint coupled with a trigger in quicksilver to emulate the firefox tab switching behavior for cmd-1 through cmd-9. Short video (not mine) about this here One problem I've noticed is that Quicksilver has a Leopard only bug that unfortunately makes the scope portion of a trigger not functional, so if you setup a trigger to these scripts, the trigger will fire not matter what application you're in. This is pretty easy to fix in the applescript itself so that's what I did. I'm not applescript expert, but here's what I did:

#!/usr/bin/env bash

SAFARI_SCRIPTS_PATH="$HOME/Library/Scripts/Applications/Safari"

mkdir -p "$SAFARI_SCRIPTS_PATH"

# tabs 1-9
for (( i = 1; i < 10; i++ )); do
script_filename="$SAFARI_SCRIPTS_PATH/Activate Tab $i"
osascript<<-EOF
  script setTab
    tell application "System Events"
      set app_name to name of the first process whose frontmost is true
    end tell
    
    if app_name is equal to "Safari" then
      tell front window of app "Safari" to set current tab to tab $i
    end if
  end script
  store script setTab in posix file "$script_filename" replacing yes
EOF
echo "Created $script_filename"
done

# last tab
script_filename="$SAFARI_SCRIPTS_PATH/Activate Last Tab"
osascript<<-EOF
  script setTab
    tell application "System Events"
      set app_name to name of the first process whose frontmost is true
    end tell
    
    if app_name is equal to "Safari" then
      tell front window of app "Safari" to set current tab to last tab
    end if
  end script
  store script setTab in posix file "$script_filename" replacing yes
EOF
echo "Created $script_filename"
echo "Done"


[ Reply to This | # ]
Use AppleScript to select specific tabs in Safari 3
Authored by: azeitona on Jul 09, '11 10:51:57PM

There's a plugin to do just this (tested on Lion only https://github.com/rs/SafariTabSwitching[/link]



[ Reply to This | # ]