Skip to content

Commit

Permalink
Update save_page_as
Browse files Browse the repository at this point in the history
Fix abiyani#19 
P.S., you might want to remove my header, but be sure to still mention the GitHub URL, as in abiyani#20.
  • Loading branch information
jidanni committed Oct 9, 2018
1 parent 5357066 commit 77f85cd
Showing 1 changed file with 75 additions and 30 deletions.
105 changes: 75 additions & 30 deletions save_page_as
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
#!/bin/bash
# save_page_as -- Save a webpage using your browser.
# https://github.com/abiyani/automate-save-page-as
# Last Modified By: Dan Jacobson
# http://jidanni.org/
# Last Modified On: Tue Oct 9 16:23:43 2018
# Update Count : 22

set -e
set -u
set -o pipefail

# Assert existence of xdotool to begin with
if ! xdotool --help &>/dev/null; then
printf "ERROR: 'xdotool' is not present (or not in the PATH). Please visit http://www.semicomplete.com/projects/xdotool/ to download it for your platform.\n" >&2
printf "ERROR: 'xdotool' is not present (or not in the PATH).
Please visit http://www.semicomplete.com/projects/xdotool/ to download it for your platform.\n" >&2
exit 1
fi

load_wait_time=4
save_wait_time=8
scriptname="$(basename "$0")"
destination="."
browser="google-chrome"
scriptname=$(basename $0)
destination=.
browser=${BROWSER?}
suffix=""
url=""

function print_usage() {
printf "\n%s: Open the given url in a browser tab/window, perform 'Save As' operation and close the tab/window.\n\n" "${scriptname}" >&2
printf "USAGE:\n %s URL [OPTIONS]\n\n" "${scriptname}" >&2
printf "URL The url of the web page to be saved.\n\n" >&2
printf "options:\n" >&2
printf " -d, --destination Destination path. If a directory, then file is saved with default name inside the directory, else assumed to be full path of target file. Default = '%s'\n" "${destination}" >&2
printf " -s, --suffix An optional suffix string for the target file name (ignored if --destination arg is a full path)\n" >&2
printf " -b, --browser Browser executable to be used (must be one of 'google-chrome', 'chromium-browser' or 'firefox'). Default = '%s'.\n" "${browser}" >&2
printf " --load-wait-time Number of seconds to wait for the page to be loaded (i.e., seconds to sleep before Ctrl+S is 'pressed'). Default = %s\n" "${load_wait_time}" >&2
printf " --save-wait-time Number of seconds to wait for the page to be saved (i.e., seconds to sleep before Ctrl+F4 is 'pressed'). Default = %s\n" "${save_wait_time}" >&2
printf " -h, --help Display this help message and exit.\n" >&2
function print_usage() { #Not too wide. Must fit on all screens.
cat 1>&2 <<EOF
$scriptname: Open the given url in a browser tab/window,
perform 'Save As' operation and close the tab/window.
USAGE:
$scriptname URL [OPTIONS]
URL The url of the web page to be saved.
options:
-d, --destination Destination path. If a directory, then file is saved
with default name inside the directory, else assumed to be full path
of target file. Default = '$destination'
-s, --suffix An optional suffix string for the target file name
Supply any dots yourself: e.g., -s .txt will add .txt
(ignored if --destination arg is a full path).
-b, --browser Browser executable to be used (must be one listed in
$scriptname source file. Default = \$BROWSER ($BROWSER)
--load-wait-time Number of seconds to wait for the page to be loaded
(i.e., seconds to sleep before Ctrl+S is 'pressed'). Default = $load_wait_time
--save-wait-time Number of seconds to wait for the page to be saved
(i.e., seconds to sleep before Ctrl+F4 is 'pressed'). Default = $save_wait_time
-h, --help Display this help message and exit.
Note: Save to "Webpage Complete" or "HTML Only" etc.: Many browsers
remember the choice made last time you saved something.
So you might need to "prep" your browser by saving something by hand first,
if you don't like its current choice. (Maybe this program might be enhanced
one faraway day to ensure saving to the right choice...)
EOF

}

while [ "$#" -gt 0 ]

while [ $# -gt 0 ]
do
case "$1" in
-d | --destination)
Expand Down Expand Up @@ -70,7 +99,8 @@ do
exit 1
;;
*) if [ ! -z "$url" ]; then
printf "ERROR: Expected exactly one positional argument (URL) to be present, but encountered a second one ('%s').\n\n" "${1}" >&2
printf "ERROR: Expected exactly one positional argument (URL) to be present,
but encountered a second one ('%s').\n\n" "${1}" >&2
print_usage
exit 1
fi
Expand Down Expand Up @@ -99,7 +129,8 @@ function validate_input() {
fi

if [[ -d "${destination}" ]]; then
printf "INFO: The specified destination ('%s') is a directory path, will save file inside it with the default name.\n" "${destination}">&2
printf "INFO: The specified destination ('%s') is a directory path,
will save file inside it with the default name.\n" "${destination}">&2
else
local basedir="$(dirname "${destination}")"
if [[ ! -d "${basedir}" ]]; then
Expand All @@ -109,10 +140,10 @@ function validate_input() {
fi
destination="$(readlink -f "$destination")" # Ensure absolute path

if [[ "${browser}" != "google-chrome" && "${browser}" != "chromium-browser" && "${browser}" != "firefox" ]]; then
printf "ERROR: Browser (%s) is not supported, must be one of 'google-chrome', 'chromium-browser' or 'firefox'.\n" "${browser}" >&2
exit 1
fi
case $browser in
google-chrome|chromium|firefox|epiphany);;
*) echo $0: ERROR: Browser $browser is not supported. 1>&2; exit 1;;
esac

if ! command -v "${browser}" &>/dev/null; then
printf "ERROR: Command '${browser}' not found. Make sure it is installed, and in path.\n" >&2
Expand All @@ -121,13 +152,17 @@ function validate_input() {

local num_regexp='^.[0-9]+$|^[0-9]+$|^[0-9]+.[0-9]+$' # Matches a valid number (in decimal notation)
if [[ ! "${load_wait_time}" =~ $num_regexp || ! "${save_wait_time}" =~ $num_regexp ]]; then
printf "ERROR: --load-wait-time (='%s'), and --save_wait_time(='%s') must be valid numbers.\n" "${load_wait_time}" "${load_wait_time}" >&2
printf "ERROR: --load-wait-time (='%s'), and --save_wait_time(='%s') must be valid numbers.
" "${load_wait_time}" "${load_wait_time}" >&2
exit 1
fi

if [[ $(has_non_printable_or_non_ascii "${destination}") -eq 1 || $(has_non_printable_or_non_ascii "${suffix}") -eq 1 ]]; then
printf "ERROR: Either --destination ('%s') or --suffix ('%s') contains a non ascii or non-printable ascii character(s). " "${destination}" "${suffix}" >&2
printf "'xdotool' does not mingle well with non-ascii characters (https://code.google.com/p/semicomplete/issues/detail?id=14).\n\n" >&2
if [[ $(has_non_printable_or_non_ascii "${destination}") -eq 1 ||
$(has_non_printable_or_non_ascii "${suffix}") -eq 1 ]]; then
printf "ERROR: Either --destination ('%s') or --suffix ('%s') contains a non ascii
or non-printable ascii character(s). " "${destination}" "${suffix}" >&2
printf "'xdotool' does not mingle well with non-ascii characters
(https://code.google.com/p/semicomplete/issues/detail?id=14).\n\n" >&2
printf '!!!! Will NOT proceed !!!!\n' >&2
exit 1
fi
Expand All @@ -153,7 +188,9 @@ xdotool windowactivate "${browser_wid}" key --clearmodifiers "ctrl+s"
sleep 1 # Give 'Save as' dialog box time to show up

# Resolve the expected title name for save file dialog box (chrome & firefox differ in this regard)
if [[ "${browser}" == "firefox" ]]; then
if [[ "${browser}" == "epiphany" ]]; then
savefile_dialog_title="Save"
elif [[ "${browser}" == "firefox" ]]; then
savefile_dialog_title="Save as"
else
savefile_dialog_title="Save file"
Expand Down Expand Up @@ -188,8 +225,10 @@ if [[ ! -z "${suffix}" ]]; then
# but this is the only fix I can think for this special case right now. Of course it's easy to tweak the number of
# Left key moves you need if you know your file types in advance.
if [[ "${is_kde}" -eq 1 ]]; then
printf "INFO: Desktop session is found to be '${DESKTOP_SESSION}', hence the full file name will be highlighted. " >&2
printf "Assuming extension .html to move back 5 character left before adding suffix (change accordingly if you need to).\n" >&2
printf "INFO: Desktop session is found to be '${DESKTOP_SESSION}',
hence the full file name will be highlighted. " >&2
printf "Assuming extension .html to move back 5 character left
before adding suffix (change accordingly if you need to).\n" >&2
xdotool windowactivate "${savefile_wid}" key --delay 40 --clearmodifier End Left Left Left Left Left
else
xdotool windowactivate "${savefile_wid}" key --delay 20 --clearmodifiers Right
Expand All @@ -204,7 +243,8 @@ if [[ ! -z "${suffix}" ]]; then
xdotool type --delay 10 --clearmodifiers "${extraarg}" "${suffix}"
fi

# Activate the 'Save File' dialog and type in the appropriate filename (depending on ${destination} value: 1) directory, 2) full path, 3) empty)
# Activate the 'Save File' dialog and type in the appropriate filename
#(depending on ${destination} value: 1) directory, 2) full path, 3) empty)
if [[ ! -z "${destination}" ]]; then
if [[ -d "${destination}" ]]; then
# Case 1: --destination was a directory.
Expand All @@ -223,10 +263,15 @@ printf "INFO: Saving web page ...\n" >&2
# Wait for the file to be completely saved
sleep ${save_wait_time}

case $browser in epiphany) is_kde=1;; esac

# Close the browser tab/window (Ctrl+w for KDE, Ctrl+F4 otherwise)
if [[ "${is_kde}" -eq 1 ]]; then
xdotool windowactivate "${browser_wid}" key --clearmodifiers "ctrl+w"
else
xdotool windowactivate "${browser_wid}" key --clearmodifiers "ctrl+F4"
fi
printf "INFO: Done!\n">&2
#Maybe also say:
# echo See: #or Proof:
# ls -l $destination

0 comments on commit 77f85cd

Please sign in to comment.