forked from Rasukarusan/fzf-chrome-active-tab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome-tab-activate
executable file
·50 lines (45 loc) · 1.43 KB
/
chrome-tab-activate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
function getTabs() {
osascript << EOF
set _output to ""
tell application "Google Chrome"
set _window_index to 1
repeat with w in windows
set _tab_index to 1
repeat with t in tabs of w
set _title to get title of t
set _url to get URL of t
set _output to (_output & _window_index & "\t" & _tab_index & "\t" & _url & "\t" & _title & "\n")
set _tab_index to _tab_index + 1
end repeat
set _window_index to _window_index + 1
if _window_index > count windows then exit repeat
end repeat
end tell
return _output
EOF
}
function setActiveTab() {
local _window_index=$1
local _tab_index=$2
osascript -- - "$_window_index" "$_tab_index" << EOF
on run argv
set _window_index to item 1 of argv
set _tab_index to item 2 of argv
tell application "Google Chrome"
activate
set index of window (_window_index as number) to (_window_index as number)
set active tab index of window (_window_index as number) to (_tab_index as number)
end tell
end run
EOF
}
function main() {
local selected
IFS=$'\t' read -r -a selected < <(
getTabs | sed '/^$/d' | fzf --delimiter $'\t' --with-nth 4 --preview 'echo {3}' --preview-window down:1 "$@"
)
[ ${#selected[@]} -lt 2 ] && return 130
setActiveTab ${selected[0]} ${selected[1]}
}
main "$@"