General recipe for focusing next/previous tab

Hello

I’m quite new to applescript. I have written my script scripts following examples of script posted online to focus the Nth tab in Safari and Terminal.

I would like now to have a script to focus the next tab and the previous tab ; so far my efforts produced this:

tell front window of application “Terminal”
set TabCur to selected tab + 1
set selected tab to TabCur
end tell

and this:

tell front window of application “Safari”
set TabCur to current tab + 1
set current tab to TabCur
end tell

Neither work, because TabCur and tab are of different classes I guess. Could I ask for some help?

Is there a more general recipe to focus the next/previous tab of any tab-supporting app?

Thanks
G

Hi,

there is no general recipe because there is no general guideline to manage tabs and there is no general tab scripting terminology

for terminal, Safari etc

use Cmd+Shift+{
and
Cmd+Shift+}
use UI scripting to continue keystroking them for n times

Hello

Cmd-Shift-{ may be fine on a US keyboard, but is quite complicated on my canadian keybord because { is option-7.

That turns the command into Cmd-Option-Shift-7 for terminal, and that doesn’t work with Safari.

Using FastScript, I want to remap both applications to something easy and identical, like Option-Left or Cmd-Left (the latter being used in iTerm which I was familiar with).

I guess something like that should work, but it doesn’t:

tell application “System Events”
tell application process “Terminal”
key code 17 using {command down, option down, shift down}
end tell
end tell

In any case, I’d rather avoid sending keycodes and use the set tab commands instead.

If there’s no general recipe, I’ll simply do one script per application!

Could I ask some help with the scripts I posted in the first message?

for Safari:



tell application "Safari"
	tell window 1
		set maxIndex to count tabs
		set currentIndex to index of current tab
		set currentIndex to currentIndex + 1
		if currentIndex > maxIndex then set currentIndex to 1
		set current tab to tab currentIndex
	end tell
end tell

Works great thanks a lot!

For terminal I tried to adapt it but it doesn’t work:


tell application "Terminal"
	tell window 1
		set maxIndex to count tabs
		set currentIndex to index of selected tab
		set currentIndex to currentIndex - 1
		if currentIndex > maxIndex then set currentIndex to 1
		set selected tab to tab currentIndex
	end tell
end tell

Chockes on the “index” keyword.

Is there somewhere a documentation of the applescript commands terminal.app accepts? I’m trying to figure out the commands from google.

I’m also wondering how window 1 could be replaced by frontmost window, or if they mean the same. I guess I’ll try when I have replaced “index” by what Terminal expects - like “current” is replaced by “selected”.

you should see the Terminal Dictionary.
Script Editor>> File Menu >> Open Dictionary

every scriptable application has a dictionary, which can be opened in Script Editor.
The dictionary contains all commands, classes and other information.
As tabs in Terminal.app have no index property, you have to identify the index with a repeat loop


tell application "Terminal"
	tell window 1
		set theTabs to (get tabs)
		set maxIndex to count tabs
		repeat with idx from 1 to maxIndex
			if selected tab is item idx of theTabs then exit repeat
		end repeat
		set idx to idx + 1
		if idx > maxIndex then set idx to 1
		set selected tab to tab idx
	end tell
end tell