Parse USPS Clink-n-Ship receipt e-mail

UPDATE: I worked this out at home on Manjaro which uses GNU ‘date’ (I assume), but macOS ‘date’ isn’t as foolproof. Also can’t be used against stdin apparently. :\

The purpose being to pare their e-mail down to just the receipt part so you can print as few pages as possible.

Used to do this with AppleScript using Mail.app. Going to Thunderbird means a different option needs to be found. Using a Folder Action can do that by calling this script:

#!/bin/bash

# Set to true to write variables to a tmp file (/tmp/USPS_debug.txt).
_debug=false

# cat contents of .eml file, awk removes newlines after '=', tr removes the rest.
_mess="`cat "$1" | awk '1' RS='=\r\n' ORS= | tr -d '\r\n'`"
if [ $_debug = true ]; then echo "_mess: $_mess" > /tmp/USPS_debug.txt; fi

# Extract the UTC 0 date from the Date header, date converts it to locale
# The Linux/GNU version of 'date' needs less massaging than the one included with macOS. Same for 'sed', actually.
#_date="`echo $_mess | sed -E 's/.+Date: ([A-Za-z]{3}, [0-9]+? [A-Za-z]{3} [0-9]{4} [0-9]+(:[0-9]+){2}).+/\1 UTC/g' | date -f -`"
# The below two _date var lines are for macOS. You could subshell it into one line, but *meh*.
_date="`echo $_mess | sed -E 's/.+Date: ([A-Za-z]{3}, [0-9]{1,2} [A-Za-z]{3} [0-9]{4} [0-9]{2}(:[0-9]{2}){2}).+/\1/g'`"
if [ $_debug = true ]; then echo "_date: $_date" >> /tmp/USPS_debug.txt; fi
_date="`date -j -v-7H -f \"%a, %d %b %Y %R:%S\" \"$_date\"`"


# Extract the order number.
_order="`echo $_mess | sed -E 's/.+>([a-z0-9]{8}(-[a-z0-9]{4}){3}-[a-z0-9]{12})<.+/\1/g'`"
if [ $_debug = true ]; then echo "_order: $_order" >> /tmp/USPS_debug.txt; fi

# Extract the section containing the juicy bits, remove more e-mail chaff, fix the tracking link, break up that one big line of code.
_sec4="`echo $_mess | sed -E -e 's/.+START SECTION 4 -->(.+)<!-- END SECTION 4.+/\1/g' -e 's/(=3D)|(=C2)|(=AE)//g' -e 's/(.+)<a hrefh(.+[0-9]{34})>(.+)/\1<a href=\"h\2\">\3/g' -e 's/([a-z]+>) (<)/\1\n\2/g'`"
if [ $_debug = true ]; then echo "_sec4: $_sec4" >> /tmp/USPS_debug.txt; fi

# Get down to the nitty gritty.
echo "<html>
<head>
<style>
body { width: 576px; font-size: 0.8em; }
.pt-5 {margin-bottom: 0.5em; margin-top: 0em; }
.bold {line-height: 1em !important; }
</style>
</head>
<p class=\"pt-5\">Received: $_date</p>
<p class=\"pt-5\">Order # $_order</p>
$_sec4
</body>
</html>" > /tmp/USPS-receipt.html

open /tmp/USPS-receipt.html

Only problem I have found is the dragging of messages out of Thunderbird can be interrupted with a replace file warning at the temp folder being used to first store the dragged message.

Fuck the new WordPress

The most convoluted, dumbass BS editing I’ve ever seen. None of this shit makes sense. Can’t even scroll <code> or <pre> text*. You’re trashing my code, assholes.

Some of that “gets out of your way” malarkey that ends up adding several layers of complexity whenever you want to change anything.

Saw a reddit post where a dev responded to another complaint about their new system with, “there’s a bazillion ways to do things” THAT’S PART OF THE PROBLEM!

I guess I’m the dumb one for wanting shit to be simply usable instead of hidden or underneath a labyrinth of interface.

* Sorted this out in the current (it’s an older code, but it checks out) theme in use, Twenty Twelve. The class by itself is not enough, it has to be pre.wp-block-code. The actual current this year theme still no luck. The CSS is applied, but I suspect the wrapping is being done proactively in javascript since it is a ‘block’ based theme.

Speaking of ‘blocks’, apparently it is impossible to start a paragraph with an asterisk without it turning into a list and there is no way to change it back to a paragraph aside from re-writing it to begin with a slash. Or did that ability do such a great job of getting out of my way I can’t find it? Now I am interested in what happens to the leading slash once I click “save”… Still there. Leading space?

Hot Folder Folder Action

(*
strawhousepig.net

Hot folder action! The jokes write themselves.

Takes the name of the folder it is attached to and sends received items to the
corresponding printer.

How to: Find a place to keep a folder that is named as the printer ID (not the name), which
can be found using 'lpstat -v', or seen in the cups web interface, or as "Device name"
in the GUI printer settings, and attach this script to it as a Folder Action. The run handler
is here just for testing, but could be set-up to send files selected in the Finder.

I keep my hot folders in a child folder of the Desktop, so it's fairly convenient to drop
items into. If dropping isn't your bag, you could place the hot folder in ~/Library/PDF Services,
or write a seperate script or workflow in PDF Services to have the best of both worlds.
*)

on adding folder items to this_folder after receiving these_items
my do_thing(these_items, this_folder)
end adding folder items to

on run
set this_folder to (choose folder)
set these_items to {}
tell application "System Events" to set these_items to every item of (this_folder)
my do_thing(these_items, this_folder)
end run

on do_thing(these_items, this_folder)
repeat with i in these_items
set i to quoted form of POSIX path of (i as alias) -- Undo this and the shell scripting if you want to try the "System Events" route.
set _script to "lp -d " & name of (info for this_folder) & " " & i & "; mv " & i & " ~/.Trash/"
--log _script --for debuging
do shell script _script
-- tell application "System Events" to print i with properties {target printer:this_printer} -- Bugged. Does not work. *smh*
end repeat
end do_thing

Printing black from Firefox

…is a mixed bag.

By printing “black” I mean printing using only black ink or toner, also known as grayscale. This works on Windows (10) but not macOS. Unsure about GNU/Linux OSes, but according to this page— [CUPS] In some printers, page is printed with colors even if “Black and white” mode is selected from print preview —it may not work there as well.

Also from that page is a hint to enable it. If you open about:config (from the location bar of Firefox) and search for “monochrome” you can find (or create if need be) a setting titled print.cups.monochrome.extra_settings and enter the appropriate variable and value for your printer. Which may require some sleuthing inside the PPD for your printer as not all companies use the same pair. In my case, printing to the parasite (an EFI/Fiery controller) on my Konica/Minolta “digital press” requires a value of EFColorMode:Grayscale in the above mentioned setting.

Also works in Thunderbird.

AppleScript + friends to generate proof image of PDF

Often when you create artwork for use as part of your making a living you don’t charge for it and just keep it to yourself. You also have to show it to the customer for approval, but sending them the original art means risking them taking it Someplace Else. You can of course charge up front or after the fact, but you still risk being out priced by the shop not doing the layout. So my best practice has been to create a lower than optimal resolution image with a watermark (aka stamp) placed over the artwork. Continue reading

Shell script to write today’s and next weekday’s date

As is this script has limited purpose, and includes RTF markup not useful to anyone else other than as example.

But, what is useful is the date function that increments to the next ‘weekday’ day of week. Be aware that this script formats into Americanized short date (e.g., 12-31-69). Which may seem odd since I just wrote (and re-wrote a couple dozen times) a script to obliterate that format in file names. Except that I’m using this script to fill out the dates on an order form where the dates are expected to be the familiar, informal conversational format used in the US. Whereas for a file name in a computer, YYYY-MM-DD is a useful sorting method.

A lot of ‘others’ don’t take the time to realize that. I’ve run into vandalized man pages for date that attempt to make admonitions against the US informal conversational date format into some moral indictment. That format appears from the way one would speak the date in the US: December thirty-first, nineteen sixty-nine. The same as one would use more words to say: The thirty-first of December, nineteen sixty-nine. It’s not hard, Europeans. 😀

Also posting the plist used with launchd to run this script every morning.

#! /bin/sh
# http://strawhousepig.net/

# Used with launchd to run every morning user is logged in.
# Runs "at load" in case log in happens after the scheduled time (8:15).
# Purpose: $outfile is meant to be placed as a linked text object in InDesign document.
# But, InDesign (CS2) won't keep text style when updating the link
# unless you load it up with (double escaped) RTF markup. :|

outfile=~/Documents/date-today.rtf

# Today's (formatted) date.
todate=$(date -j "+%m-%d-%y" | sed -E 's/0([0-9])/\1/g')

# Next weekday:
# Use today's day-of-week to count days until Monday if day-of-week is greater than 4 (Thursday).
dofw=$(date +%w)
nextdate=$(date -j -v+$(( ( $dofw>4 )?8-$dofw:1))d "+%m-%d-%y" | sed -E 's/0([0-9])/\1/g')

# ProTip: Use '$todate' and '$nextdate' as placeholder text in your RTF file.
# ie., format a sample RTF file then open, copy, & paste it as (double escaped) plain text here.
rtf="{\\\rtf1\\\ansi\\\ansicpg1252\\\cocoartf1038\\\cocoasubrtf360
{\\\fonttbl\\\f0\\\fswiss\\\fcharset0 Helvetica;}
{\\\colortbl;\\\red255\\\green255\\\blue255;}
\\\margl1440\\\margr1440\\\vieww9000\\\viewh8400\\\viewkind0
\\\pard\\\tx720\\\tx1440\\\tx2160\\\tx2880\\\tx3600\\\tx4320\\\tx5040\\\tx5760\\\tx6480\\\tx7200\\\tx7920\\\tx8640\\\sb160\\\ql\\\qnatural\\\pardirnatural

\\\f0\\\fs32 \\\cf0 $todate\\\

$nextdate}"

printf "$rtf" > "$outfile"

Launchd properties file (reflects the above code being named “date-today-write-to-file.sh” and placed into the user’s ‘Documents’ folder) should be placed in the LaunchAgents folder of the users Library folder, not the root Library (e.g., ~/Library/LaunchAgents/date-today.write-to-file.plist) and loaded with launchctl load ~/Library/LaunchAgents/date-today.write-to-file.plist

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>Label</key>
		<string>date-today.write-to-file</string>
		<key>Program</key>
		
<string>/Users/EXAMPLE/Documents/date-today-write-to-file.sh</string>
		<key>RunAtLoad</key>
		<true/>
		<key>StartCalendarInterval</key>
		<dict>
			<key>Hour</key>
			<integer>08</integer>
			<key>Minute</key>
			<integer>15</integer>
		</dict>
	</dict>
</plist>

AppleScript to write Finder tag from folder name

This is a weird one that came from a request on reddit.

Not really useful (to me) as intended, but I envision it could be re-worked to a FolderAction that would help tag photos. Especially if coupled with a file sorter. But incorporating the tag writing into the sorter would be much more sane.

The commented out do shell script lines were meant as a way to read existing tags and then add the folder name after them. That didn’t work out thanks to xattr -p spitting out hexadecimal instead of a plist array, which is the format used when writing the tags. WTF, Apple…

-- http://strawhousepig.net/
on run
  set _drop to {}
  set end of _drop to (choose folder)
  my do_it(_drop)
end run

on open _drop
  my do_it(_drop)
end open

on do_it(_drop)
  display dialog "WARNING: This script will overwrite ALL tags of files in or targeted from the opened folder with the name of the opened folder." with icon 0
  repeat with d in _drop
    if folder of (info for d) is true then
      set f to {}
      try
        tell application "Finder"
          set _tag to "" & name of (info for d) & ""
          set _files to (every item in d)
          repeat with f in _files
            if alias of (info for f as alias) is true then
              set f to original item of f as alias
            end if
-- 'xattr -p' will print the value for a named metadata ID. Naturally that value is printed as hexadecimal. :|
-- set f_plist to (do shell script "xattr -p com.apple.metadata:_kMDItemUserTags " & quoted form of POSIX path of (f as alias))
-- set _tag to (do shell script "echo \"" & f_plist & "\" | egrep -o \"*\"") & _tag
            set tag_plist to "" & _tag & ""
            do shell script "xattr -w com.apple.metadata:_kMDItemUserTags " & quoted form of tag_plist & " " & quoted form of (POSIX path of f)
          end repeat
        end tell
      on error _err
        display dialog _err
      end try
    end if
  end repeat
end do_it

Script to re-write dates in file names

Annnnnd I’m done.

Did I say “done?” Now using a regex that will (should) only pick viable dates, although other strings can match. Notably a time string that meets the [01-12].[01-31].[00-99] format. Also added some error checking… or at least that’s the idea. Unsure if working at this time. I don’t believe it do. Only if `date` can’t reformat the string it’s fed will it be able to notice, but that is printed to stdout already. It’s the “Warning:” lines that are ambiguous.

Removed the “error checking” which didn’t work. I suggest making note of any errors reported to stdout while the script runs.

The main extended regex that matches the mm.dd.(yy)yy pattern is now a variable.

#!/bin/sh
# http://strawhousepig.net/

# The following `egrep` (or `grep -E`) should prove useful when checking the
# storage file. If your text editor supports grep based 'find all' you
# may be able to highlight the original text that is to be changed.
# egrep -nv '(1[0-2]|0?[1-9])[.-/,]([0-2]?[1-9]|[1-3][0-1])[.-/,](20)?[0-9]{2}(\.[[:alnum:]]+)?[[:space:]]/'

# Name of file in which to store 'mv' statements.
mvstore="mvstore.sh"

# The number of parentheses here affects the `sed` for $e. Currently "\4".
dateregex="(1[0-2]|0?[1-9])[.-/,]([0-2]?[1-9]|[1-3][0-1])[.-/,](20)?[0-9]{2}"

usage="
This script looks recursively for the date pattern mm.dd.yy and similar
in file and directory names beginning at 'pwd'. It then generates an 'mv'
statement for each item with the pattern re-written by 'sed' & 'date'
to yyyy-mm-dd.

Running the '-e' option without first creating and checking the storage
file is not recommended.

-w Write the 'mv' statements to the file '$mvstore'
in the present working directory.
-e Evaluate the 'mv' statements as they are generated.
-h Displays this helpful text.
\n"

function mvdatef() {
# Here, 'tail' reverses the order of 'find' after 'egrep' filters the result.
# That way files in a directory are renamed before the directory is.
# 'find' might be able to use the regex, but I couldn't work it out. YMMV.
# This only looks for the pattern at the end of the line or just before an extension.
# If something isn't working, the regex here is probably where it started.
find "`pwd`" | egrep '[/[:space:]]'"$dateregex"'(\.[[:alnum:]]+)?$' | tail -r | while read a
do

# Escape certain characters so they don't wreck the 'mv' statement later.
# double quote, single quote, parens, ampersand, dollar sign, and space.
# Single quote, parens, and ampersand are escaped for the shell after breaking out of the 'sed' statement.
b=$(echo "$a" | sed -E 's/(["'\'\(\)\&'[:space:]$])/\\\1/g')
if [ "$b" == "" ]; then
echo "Error: Could not escape $a" >> $mvstore
fi
# Suck out the last instance of our hated date pattern (00.00.00 or 00.00.0000).
# Also replace dashes, slashes (not working :| ), and errant commas for dots
# because we've come too far not to.
c=$(echo "$a" | egrep -o "$dateregex" | tail -1 | sed -E 's/[-/,]/\./g')
if [ "$c" != "" ]; then
# 'date' will not accept a 2 OR 4 digit year.
if [ $(echo $c | egrep -o "[0-9]{4}$") ]; then
dform="%m.%d.%Y"; else
dform="%m.%d.%y"
fi
d="$(date -j -f "$dform" "$c" "+%Y-%m-%d")"
if [ "$d" == "" ]; then
echo "Error: Could not format date from $c" >> $mvstore
fi
# This is the 'sed' that finds the date and replaces it with what we made just above.
# It looks for the pattern at the end of the line (path) but includes the extension if there.
# It is possible that a version number of some sort will also match.
# Also possible to do away with the EOL in the regex and just go for the pattern.
e="$(echo "$b" | sed -E 's/'"$dateregex"'(\.[[:alnum:]]+)?$/'$d'\4/')"
if [ "$e" == "" ]; then
echo "Error: Could not replace $c in $a" >> $mvstore
fi
# After all that dicking around, this is the mv statement.
f="mv -- $b $e"

if [ $1 ]; then
eval $f
else
echo "$f" >> "$mvstore"
fi
else
echo "Error: $c - Could not pull viable date from $b" >> $mvstore
fi
done
}

if [[ $1 = "-h" ]] ; then
printf "$usage"
exit
elif [[ $1 = "-e" ]]; then
echo "Evaluating 'mv' statements as they are generated..."
mvdatef -e
elif [[ $1 = "-w" ]]; then
echo "Generating file `pwd`/$mvstore and writing 'mv' statements to it..."
printf "#/bin/sh\n# `date`\n" > "$mvstore"
mvdatef
else
printf "$usage"
exit
fi

Original, less-good version: http://strawhousepig.net/shell-script-to-re-format-poor-date-format-in-filenames/

Shell script to re-format poor date format in filenames

New, more-good version here: New script to re-write dates in file names

After testing and some using, this is going to change to just write the mv statements to a file and to accept an argument to run them instead. No logging since that will serve the same purpose. This post does not contain the final version. It’s here for the sake of history.

And boom. Or so I hope. This *should* handle dates as “01.02.03” and the equivalent “01.02.2003”.
To-do: Check for administrator privileges or just write the log to ~/. MacOS (nee OS X (nee Mac OS)) has a default user log directory, but GNU does not. I suppose we could check for “~/.log/” and mkdir if false…

Title explains it pretty well. Uses egrep to find date, sed to edit it, mv to rename, and read for user confirmation prompt. Which, btw, can’t be done if you are already doing a read without feeding the new read from somewhere other than stdin. Hence the < /dev/tty at that point in the script. So many strangers to thank for sharing their knowledge online. Thanks, strangers! That should do it.

Why this exists is I had a co-worker who added dates to tons of files in the format “01.02.03”, which resulted in files named “Example filename 01.02.03.xmpl” Same for phone numbers. Clearly this is wrong and something must be done about it (now that new ones won’t be cropping up since he.is.gone). You may be asking yourself how he got away with not getting an e-mailed file bounced back for multiple extensions? My educated guess is that he did and kept doing it the dumb way despite that.

Corrects the above format to “+%Y-%m-%d” (YYYY-MM-DD).

This could probably be done with fewer lines or overall be more betterer, but this is like my first whole shell script. Maybe my years of AppleScript are showing. *meh*
Continue reading