-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabgrab
executable file
·83 lines (74 loc) · 2.76 KB
/
tabgrab
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Copyright (c) 2022-2024 Benjamin Holt -- MIT License
set -eo pipefail
# set -x # Uncomment for debugging
if [ "$1" == "-h" -o "$1" == "--help" ]; then
cat <<USAGE
usage: tabgrab [-h|--help] [-c|--copy] [BROWSER]
Collect tabs from all windows as a markdown list of links with
the first tab of each window as the parent of the remaining tabs.
BROWSER defaults to the BROWSER_APP environment variable, otherwise
the default handler for 'https' is used to guess the system browser.
-c, --copy Copies the list to the pasteboard
-n, --num-windows N
Collects tabs from the first N windows, defaults to
all windows
-h, --help Print this message and exit
USAGE
exit 0
fi
CopyList="false"
NumWindows="count (every window)"
for arg in $@; do
case "$arg" in
-c|--copy)
CopyList="true"
shift
;;
-n|--num-windows)
NumWindows="$2"
shift 2
;;
esac
done
Browser="${1:-${BROWSER_APP}}"
if [ -z "$Browser" ]; then
Handler=`defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure | (grep -B1 "LSHandlerURLScheme = https" || echo -n "") | head -1 | cut -d '"' -f 2`
if [ -z "$Handler" -o "$Handler" == "com.apple.safari" ]; then
Browser="Safari"
elif [ "$Handler" == "com.google.chrome" ]; then
Browser="Chrome"
else
# Try the last token of the the handler, this would work for Safari and Chrome, but doesn't work with Firefox which doesn't offer tabs in its scripting dictionary :shrug:
Browser=`echo "$Handler" | tr '.' '\n' | tail -1`
echo "Guessing $Browser (it may not work)"
fi
Bold=`tput bold`
Off=`tput sgr0`
echo "${Bold}Grabbing tabs from ${Browser}${Off}" >&2
fi
# Hacked from [Capture all tabs in Safari as URLs to the clipboard – theconsultant.net](https://theconsultant.net/2017/06/capture-all-tabs-in-safari-as-urls-to-the-clipboard/)
osascript <<SCRIPT
tell application "$Browser"
set tabList to ""
set windowCount to $NumWindows
repeat with w from 1 to windowCount
set tabCount to number of tabs in window w
repeat with t from 1 to tabCount
if t > 1 then
set tabList to tabList & " "
end if
set tabName to name of tab t of window w
set tabURL to URL of tab t of window w as string
set tabList to tabList & "- [" & tabName & "](" & tabURL & ")" & linefeed as string
end repeat
set tabList to tabList & linefeed
end repeat
if $CopyList then
set the clipboard to the tabList
return "Copied"
else
return tabList
end if
end tell
SCRIPT