Save Thunderbird and Sylpheed emails using YAD and xdotool

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

Save Thunderbird and Sylpheed emails using YAD and xdotool

#1 Post by kjdixo »

I have been saving my Thunderbird emails offline in a custom format, then deleting them from the server.

Code: Select all

#! /bin/bash
yad --no-buttons --on-top --title="Email" --geometry='300x50+80+1200' --form --field="Save":fbtn /root/mail.sh

You might need to tweak the above button YAD dialog --geometry to suit your screen size and Thunderbird window positioning.
The button (code above) is useful to have on the desktop . . . whilst using mail.sh (code below).

Code: Select all

#! /bin/bash
# manually with mouse highlight thunderbird email date
# 06/11/15 13:21
# example equates to November 6th 2015 at 1:21 pm
# run this script (from button or menu item)
xdotool search --classname Mail windowactivate
dialog=$(yad --on-top --mouse --width=800 --title "Copy and Paste <From (To) Field>" --form --field="")
ret=$?
if [[ $ret -eq 1 ]];then exit 0;fi
from=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')
if [[ $from = "" ]]; then exit 0; fi

dialog=$(yad --on-top --mouse --width=600 --title "Highlight <Date Time Field>" --text="Then Click OK")

xdotool key "ctrl+c";
xdotool search --classname Mail windowactivate
sleep 0.5
xdotool key "ctrl+s";
sleep 0.5
xdotool key "Home";
# load clipboard into variable using xclip
var1=$(xclip -out -selection c)
# replace slashes and spaces with hyphens
# 06-11-15-13:21
# swap chars 1 + 7
# swap chars 2 + 8
# 15-11-06-13:21
var2=$(echo $var1 | sed 's/\//-/g' | sed 's/ /-/g' | awk '{print substr($0,7,2) substr($0,3,4) substr($0,1,2) substr($0,9,6)}')
# append - prepend 20
var3="20"$var2"-"
# 2015-11-06-13:21-
echo $var3$from | xclip -selection c
xdotool key "ctrl+v";
# then yad text input "from field" append sender details

I use Xfe file manager to assist me with the whole process. Xfe also has a good 'Search Files' tool.

Emails are saved as timestamped .eml files which will open in Thunderbird.
I create timestamped folders manually for saved attachments.
Last edited by kjdixo on Tue 15 Nov 2016, 11:44, edited 2 times in total.

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#2 Post by kjdixo »

Code below . . . from=$from"-" . . . adds a hyphen between fields in .eml filename.
First line and last line already exist, middle bit adds an extra hyphen in the .eml filename to improve readability.

Code: Select all

if [[ $from = "" ]]; then exit 0; fi

#2016-08-26 next line adds a hyphen after from field
from=$from"-"

dialog=$(yad --on-top --mouse --width=600 --title "Highlight <Date Time Field>" --text="Then Click OK")

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#3 Post by kjdixo »

1. Please see from code in the first post, that xclip is required for it to work.

2. Yesterday I adapted the code to work with Sylpheed a lighter weight email client.

For this I am using an Asus Eee PC 2G Surf (using a cut down version of LX Pup 14.10).
I noticed that a Thunderbird install is 70Mb and the program is very slow to start.
Sylpheed only requires a 7Mb install and starts in a second or two.
So Sylpheed is the better option if resources are low.
LXTask and Gdmap can be used to further verify resources used.
That was all the incentive I needed to write another script called 'Script to save Sylpheed emails using YAD, xdotool and xclip'. I will post that later today.
I realise that Sylpheed has a built in option to change the date format, but I could not get it to work, the date code stubbornly refused to change from the default of
Date: Fri, 4 Nov 2016 12:55:38 +0000
That date code is actually very clear and readable and I don't mind keeping it the way it is.
However, the whole point of me changing it in this exercise is to get chronological lists of filenames, of .eml files, in my file manager Xfe.
My preferred format for this is
2016-11-04-12:55-
So before and until I post the Sylpheed code as a new topic with its own title (to distinguish it from this topic for Thunderbird), I will just paste the Sylpheed code here.
For Sylpheed not Thunderbird
The code is very similar to the Thunderbird code and is best viewed in a text editor with syntax highlighting, as there is a lot of commenting to explain each step.

Code: Select all

#! /bin/bash
# manually with mouse highlight sylpheed email date
#
# Date: Fri, 4 Nov 2016 12:55:38 +0000
# or
# Fri, 4 Nov 2016 12:55:38 +0000
#
# run this script (from button or menu item)
xdotool search --classname main_window windowactivate
dialog=$(yad --on-top --mouse --width=800 --title "Copy and Paste <From (To) Field>" --form --field="")
ret=$?
if [[ $ret -eq 1 ]];then exit 0;fi
from=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')
if [[ $from = "" ]]; then exit 0; fi

# next line adds a hyphen after from field
from=$from"-"

dialog=$(yad --on-top --mouse --width=600 --title "Highlight <Date Time Field>" --text="Then Click OK")

xdotool key "ctrl+c";
xdotool search --classname main_window windowactivate
sleep 0.5
xdotool key "ctrl+s";
sleep 0.5
xdotool key "Home";
# load clipboard into variable using xclip
v1=$(xclip -out -selection c)
# Date: Fri, 4 Nov 2016 12:55:38 +0000
# change months to numerical
v1=${v1/Jan/01}
v1=${v1/Feb/02}
v1=${v1/Mar/03}
v1=${v1/Apr/04}
v1=${v1/May/05}
v1=${v1/Jun/06}
v1=${v1/Jul/07}
v1=${v1/Aug/08}
v1=${v1/Sep/09}
v1=${v1/Oct/10}
v1=${v1/Nov/11}
v1=${v1/Dec/12}
# Date: Fri, 4 11 2016 12:55:38 +0000
# replace spaces with hyphens
var2=$(echo $v1 | sed 's/ /-/g')
# highlight date string (some people might highlight "Date: " as well as the date, so allow for this) 
# Just use the last 25 characters
# -4-11-2016-12:55:38-+0000
var2=${var2:(-25)}
# Now use the first 16 characters
# -4-11-2016-12:55
var3=${var2:0:16}
# If first character is a hyphen then change it to a zero
# use position specific replacement
v4=$(echo $var3 | sed '/^-/s/./0/1')
# 04-11-2016-12:55
# detect  201 ... 209 (years 2010 ... 2099) and remove the beginning '20'
v4=${v4/201/1}
v4=${v4/202/2}
v4=${v4/203/3}
v4=${v4/204/4}
v4=${v4/205/5}
v4=${v4/206/6}
v4=${v4/207/7}
v4=${v4/208/8}
v4=${v4/209/9}
# 04-11-16-12:55
# swap chars 1 + 7
# swap chars 2 + 8
# 16-11-04-12:55
var5=$(echo $v4 | sed 's/\//-/g' | awk '{print substr($0,7,2) substr($0,3,4) substr($0,1,2) substr($0,9,6)}')
# prepend 20 append -
v6="20"$var5"-"
# 2016-11-04-12:55-
echo $v6$from | xclip -selection c
xdotool key "ctrl+v";
# then yad text input "from field" append sender details

This works with the current date which has a single digit number for the day 4 or 5.
It should also work with 2 digit days like 23 or 24.

The purpose was to allow me to uninstall Thunderbird from my low resource netbook and that has been successful.
I can now, from Sylpheed, save all my emails as .eml files offline on SD cards or USB memory sticks.

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#4 Post by kjdixo »

For Sylpheed
I updated the above code following a suggestion by MochiMoppel to try using busybox to convert the date.
http://www.murga-linux.com/puppy/viewtopic.php?t=108061
This has reduced the script size considerably.

Code: Select all


#! /bin/bash
# manually with mouse highlight sylpheed email date
#
# Date: Fri, 4 Nov 2016 12:55:38 +0000
# or
# Fri, 4 Nov 2016 12:55:38 +0000
#
# run this script (from button or menu item)
xdotool search --classname main_window windowactivate
dialog=$(yad --on-top --mouse --width=800 --title "Copy and Paste <From (To) Field>" --form --field="")
ret=$?
if [[ $ret -eq 1 ]];then exit 0;fi
from=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')
if [[ $from = "" ]]; then exit 0; fi

dialog=$(yad --on-top --mouse --width=600 --title "Highlight <Date Time Field>" --text="Then Click OK")

xdotool key "ctrl+c";
xdotool search --classname main_window windowactivate
sleep 0.5
xdotool key "ctrl+s";
sleep 0.5
xdotool key "Home";
# load clipboard into variable using xclip
v1=$(xclip -out -selection c)
# Date: Fri, 4 Nov 2016 12:55:38 +0000
# Use busybox as suggested by MochiMoppel http://www.murga-linux.com/puppy/viewtopic.php?t=108061
v6=$(busybox date -D 'Date: %a, %d %b %Y %T' -d "$v1" +%Y-%m-%d-%R-"$from"-)
echo $v6 | xclip -selection c
xdotool key "ctrl+v";
# then yad text input "from field" append sender details

Last edited by kjdixo on Mon 07 Nov 2016, 08:02, edited 2 times in total.

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#5 Post by kjdixo »

For Thunderbird
An update to the Thunderbird script considerably reduces the code for converting the date from an unusual format to a more usable format.
This follows a suggestion by MochiMoppel that I try using busybox to do the date conversion.
It reduces several lines of hacking down to a single one liner.
Thanks MochiMoppel.

http://www.murga-linux.com/puppy/viewtopic.php?t=108061

The modified Thunderbird .eml saving script is shown below.

Code: Select all

#! /bin/bash
# manually with mouse highlight thunderbird email date
# 04/11/16 12:55
# example equates to November 4th 2016 at 12:55 pm
# run this script (from button or menu item)
xdotool search --classname Mail windowactivate
dialog=$(yad --on-top --mouse --width=800 --title "Copy and Paste <From (To) Field>" --form --field="")
ret=$?
if [[ $ret -eq 1 ]];then exit 0;fi
from=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')
if [[ $from = "" ]]; then exit 0; fi

dialog=$(yad --on-top --mouse --width=600 --title "Highlight <Date Time Field>" --text="Then Click OK")

xdotool key "ctrl+c";
xdotool search --classname Mail windowactivate
sleep 0.5
xdotool key "ctrl+s";
sleep 0.5
xdotool key "Home";
# load clipboard into variable using xclip
var1=$(xclip -out -selection c)
# convert date 04/11/16 12:55 to 2016-11-04-12:55-
# Use busybox as suggested by MochiMoppel http://www.murga-linux.com/puppy/viewtopic.php?t=108061
# 04/11/16 12:55
var3=$(busybox date -D '%d/%m/%y %H:%M' -d "$var1" +%Y-%m-%d-%R-"$from"-) 
# 2016-11-04-12:55-
echo $var3 | xclip -selection c
xdotool key "ctrl+v";
# then yad text input "from field" append sender details


Post Reply