Search code examples
bashshellgoogle-chromemhtml

Script works, captures web page as mht file, but chrome is deleting


Download works properly but Google removes it the moment i select "Show in Finder" -

Removed File

Safe Browsing set to "No protection (not recommended)"

My goal is to create a simple script that will download and timestamp webarchives that I can run daily with cron or iCal

Ran script seems to work well, downloads web page as archive but Chrome somehow deleted the download

Any help appreciated

#!/bin/bash

# Set the URL of the website to open in Chrome
URL="https://thescottsdaleherald.com/"

# Set the directory to save the web archive file to
SAVE_DIR="$HOME/Desktop/"

# Get the current date in the format "day-month-year"
DATE=$(date +'%H-%d-%m-%Y')

# Open the website in Chrome
open -a "Google Chrome" "$URL"

# Wait for Chrome to load the website
sleep 5

# Get the title of the page
PAGE_TITLE=$(osascript -e 'tell application "Google Chrome" to return title of active tab of window 1')

# Replace any commas in the page title with hyphens
PAGE_TITLE=${PAGE_TITLE//,/}

# Save the web archive file with the format "pagetitle,day,month,year.webarchive"
FILE_NAME="$PAGE_TITLE,$DATE.mht"
FULL_PATH="$SAVE_DIR/$FILE_NAME"
osascript -e 'tell application "Google Chrome" to save active tab of window 1 in file "'"$FULL_PATH"'" '

Solution

  • According to AppleScript Guide, a file object is structured as

    file "VolumeName:FolderName:SubfolderName:FileName"
    

    So essentially, your script needs 2 updates:

    1. Add volume name to the beginning of SAVE_DIR, e.g.

      SAVE_DIR="Macintosh SSD/$HOME/Desktop"

      Ensure to use your own volume name instead

    2. When call the FULL_PATH, replace all / with :

      ...to save active tab of window 1 in file "'"${FULL_PATH//\//:}"'" '