Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Create a Time Machine-like feature for Safari tabs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a Time Machine-like feature for Safari tabs
Authored by: emailboy on Dec 02, '10 02:46:18PM

I found that, on 10.5.8, I was getting slews of launchd errors about the job respawning too quickly, so I added some keys to the launchd.plist to set KeepAlive false. I also added some comments to the script and set the expiration period to 30 days (personal preference), and added some logging to /tmp (just for safety):

Here's an updated ~/Library/LaunchAgents/com.cannasoftware.lastsessiontimemachine.plist file:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>com.cannasoftware.com.lastsessiontimemachine</string>
    <key>OnDemand</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/USERNAME/Library/Safari/com.cannasoftware.lastsessiontimemachine.sh</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/USERNAME/Library/Safari/LastSession.plist</string>
    </array>
    <key>StandardErrorPath</key>
        <string>/tmp/lastsessiontimemachine.log</string>
    <key>StandardOutPath</key>
        <string>/tmp/lastsessiontimemachine.log</string>
</dict>
</plist>

And, here's an updated ~/Library/Safari/com.cannasoftware.lastsessiontimemachine.sh file:



#!/bin/bash

#
# Script to create backup copy of Safari's "Last Session" state, to
# protect against accidently quitting Safari without clicking
# "Reopen all windows from last session".
#
# Script is run by LaunchAgent in ~/Library/LaunchAgents directory
# with LastSession.plist as watchfile.
#

#
# Create "LastSessionTimeMachine" directory if it doesn't exist
#
mkdir -p ~/Library/Safari/LastSessionTimeMachine

#
# Copy Safari's last session state file to the "LastSessionTimeMachine"
# directory, with current date and time appended.
#
cp ~/Library/Safari/LastSession.plist \
    ~/Library/Safari/LastSessionTimeMachine/LastSession`date "+-%Y-%m-%d-%H-%M-%S"`.plist

#
# Delete any saved "last session" files which are more than 30 days old
#
find ~/Library/Safari/LastSessionTimeMachine -name 'LastSession*' \
                                             -type f \
                                             -ctime +30 \
                                             -delete

exit 0


[ Reply to This | # ]
Create a Time Machine-like feature for Safari tabs
Authored by: emailboy on Jan 28, '11 04:57:55PM
I found I was getting way too many saved files (as in, thousands and thousands), plus I was still getting launchd errors. I modified the script to keep all of today's files, but only the oldest file from each previous day:

#!/bin/bash

#
# Script to create backup copy of Safari's "Last Session" state, to
# protect against accidently quitting Safari without clicking
# "Reopen all windows from last session".
#
# Script is run by LaunchAgent in ~/Library/LaunchAgents directory
# with LastSession.plist as watchfile.
#

#
# Echo a line to denote start of execution
# (makes it easier when looking in the log)
#
echo "---------- $(date) ----------"

#
# Create "LastSessionTimeMachine" directory if it doesn't exist
#
mkdir -p ~/Library/Safari/LastSessionTimeMachine

#
# Copy Safari's last session state file to the "LastSessionTimeMachine"
# directory, with current date and time appended.
#
DATE=$(date "+-%Y-%m-%d-%H-%M-%S")
cp ~/Library/Safari/LastSession.plist \
    ~/Library/Safari/LastSessionTimeMachine/LastSession${DATE}.plist

# get a list of saved LastSession files, sorted newest to oldest
FILES=$(/bin/ls ~/Library/Safari/LastSessionTimeMachine/LastSession-*.plist | sort -r)

prevYear=0
prevMonth=0
prevDay=0
prevHour=0
prevMin=0
prevSec=0

# get current date
# note: fails if these commands are split across midnight
nowYear=$(date "+%Y")
nowMonth=$(date "+%m")
nowDay=$(date "+%d")

for AFILE in $FILES
do

    # parse filename into timestamp component
    timestamp=$(echo $AFILE | awk -F'[-\.]' '{print $2,$3,$4,$5,$6,$7}')

    # some weird code I found that parses a string into an array    
    set -- "$timestamp" 
    OLDIFS=$IFS; IFS=" "; declare -a Array=($*) 

    year=${Array[0]}
    month=${Array[1]}
    day=${Array[2]}
    hour=$Array
    min=${Array[4]}
    sec=${Array[5]}
        
    # delete file if we already have one from the same day but
    # skip today's files
    if [ "$year"  -eq "$nowYear"   -a \
         "$month" -eq "$nowMonth"  -a \
         "$day"   -eq "$nowDay"    ]; then
        echo "$timestamp is today's"
    elif [                            \
         "$year"  -eq "$prevYear"  -a \
         "$month" -eq "$prevMonth" -a \
         "$day"   -eq "$prevDay" ]; then
        echo "$timestamp is an older previous day's -- deleting"
        rm -f $AFILE
    else
        echo "$timestamp is newest from that day"
    fi
    
    prevYear=$year
    prevMonth=$month
    prevDay=$day
    prevHour=$hour
    prevMin=$min
    prevSec=$sec

done

exit 0



[ Reply to This | # ]