Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

macro to save attachments renamed

590 views
Skip to first unread message

Maneesh Patel

unread,
Mar 2, 2012, 9:17:49 PM3/2/12
to
I would like to define a mutt macro to save attachments to a
particular directory with an improved filename. The following line in
my .muttrc file:

macro attach S "<save-entry><bol>$HOME/Mail/Attachments/<eol>"

does half of what I want: it allows me to save the attachment with its
original filename to the desired directory. I would like to save the
attachment with a new filename, however, in which all spaces are
replaced by underscores, all letters are changed to lowercase, and all
single-close-quote marks are removed.* In other words, if possible I
would like my .muttrc macro to do something like execute the bash
command

echo $filename | tr 'A-Z' 'a-z' | sed "s/'//g" | sed "s/ /_/g"

and use the output as the new filename under which to save the
attachment in the desired directory. Is this possible?

Kind regards,
--maneesh

*If I could convince my friends using Windows to stop sending me Word
documents with poor filenames that would solve the problem, but I'm
not too optimistic about this solution.

Ulrich Thorwarth

unread,
Mar 3, 2012, 12:13:14 PM3/3/12
to
Maneesh Patel <mnsh...@gmail.com> schrieb:
> I would like to define a mutt macro to save attachments to a
> particular directory with an improved filename. The following line in
> my .muttrc file:
>
> macro attach S "<save-entry><bol>$HOME/Mail/Attachments/<eol>"
>
> does half of what I want: it allows me to save the attachment with its
> original filename to the desired directory. I would like to save the
> attachment with a new filename, however, in which all spaces are
> replaced by underscores, all letters are changed to lowercase, and all
> single-close-quote marks are removed.* In other words, if possible I
> would like my .muttrc macro to do something like execute the bash
> command
>
> echo $filename | tr 'A-Z' 'a-z' | sed "s/'//g" | sed "s/ /_/g"
>
> and use the output as the new filename under which to save the
> attachment in the desired directory. Is this possible?


Not really the answer you want, but some ideas about it:

1) sed is fine, but have a look at
filename=`echo $filename | tr -d \'\"\)\(\:\,\?\!\/\-`
filename=`echo $filename | tr [:blank:] _`
filename=`echo $filename | tr 'A-Z' 'a-z'`

2) I'm afraid you have to use mv for that

3) for a similar purpose I use procmail:
:0 c
* ^From:.*specialem...@Iknowit.com
|munpack -C $HOME/attachments && \
chmod 755 $HOME/attachments/*.* && \
rm $HOME/attachments/*.desc

not exactly what you need, but might be an idea
(this one was to save daily messages from one particular sender)

4) you probably know antiword with an autoview setting and the
appropriate $HOME/.mailcap entry??

Ulrich

Maneesh Patel

unread,
Mar 3, 2012, 6:38:17 PM3/3/12
to
Thank you, sir, particularly for the improved version of my bash
command. I do use procmail for certain things, but had not considered
this particular use. And I agree that the program antiword is
extremely useful for converting Word files to ASCII (as are wvPDF and
abiword for conversion to PDF and other types).

Yet I would still like to know if it is possible to write a .muttrc
macro that invokes a bash command or script to produce the default
"save to" filename. I've tried a great many things like

macro attach S "<save-entry><bol>$HOME/Mail/Attachments/$(%f | tr 'A-
Z' 'a-z' | sed "s/'//g" | sed "s/ /_/g")"

which do not produce the desired result. (I often use mutt over an
ssh connection and then view and sometimes edit attachments on the
remote machine by saving them from mutt and using scp. Thus it is
slightly inconvenient to have attachment filenames with spaces. Your
antiword suggestion might solve at least the immediate problem.)

Kind regards,
--maneesh

Sylvain Soliman

unread,
Mar 4, 2012, 5:49:43 PM3/4/12
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hi,

> Yet I would still like to know if it is possible to write a .muttrc
> macro that invokes a bash command or script to produce the default
> "save to" filename. I've tried a great many things like
>
> macro attach S "<save-entry><bol>$HOME/Mail/Attachments/$(%f | tr 'A-
> Z' 'a-z' | sed "s/'//g" | sed "s/ /_/g")"

I'm afraid that neither <save-entry> nor <pipe-entry> has access to
something like '%f'...

You can still postprocess your Attachments directory or write a specific
mailcap entry (testing for $SSH_TTY if you want) that does what you want.

> which do not produce the desired result. (I often use mutt over an
> ssh connection and then view and sometimes edit attachments on the
> remote machine by saving them from mutt and using scp. Thus it is
> slightly inconvenient to have attachment filenames with spaces. Your
> antiword suggestion might solve at least the immediate problem.)

What is the problem with spaces? Just quote them or let bash completion do
it for you ;)

Best,

Sylvain Soliman

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)

iQIVAwUBT1PxhcYrB++Yy5CuAQi4GQ/9FcUcUgFgA1UbpsYYlCt9tAi0eP1vxfdF
w+L4U5L9GToH12eJlaX+b0LpywqEYq+YzDBoP8bu1Wf2bJ9nA14Sf8kVKQ62z4gl
9pmKQLz/sblWymCQuQn05/Z7lVVDt9ivoHlPMzgLAFMLguJ6WrzjUk1D6oijotGZ
Rx68SqXw14MatvLLYx8dUOZbtY8DH2MO5dhzM2g5N+d4VWB9vZengJz4z28sIPML
/N8JvYUQQVSsSrh5wfgYkvC+8hXJ/FF8p6+h2X/ku8xLin8PLE6Znbsa/Yx4iIlk
S09EbR41K6vydwloIl7UPxOVxNfN+yE/Ti2Gha6X2aAGdCO8hYxCNKGMKh6fSAsX
9/UnsfWvOw41RONzDzbkkbxAdllmJscMrU4oFqAoExDV4d5YHCBeKlFOKgXy/2CA
8VDoQYGpbqWjMc8dynyq/aMrlM/cN14qi5dYhXKuXwNEjWKdeNcsX8bFESubvk78
g3U0vpQM8qsVFipub/JNxJeTowK6Y3ZxI2SsGL/PZwL3k5+BvuOu/lB0QGEb0s/D
7374JzkStZT4M615Xt13u8L/aZXYO/t5bqA3ZACgDcoIAqDRsr+MstzGCCwxByAv
FioC6WoeZ5uwOuWXeRd2gEDk48hZrujxbNt0XGcS69TiQX8P/gnMm3OohUTFykaM
J2YY/edPTa0=
=1EZR
-----END PGP SIGNATURE-----

--
Sylvain Soliman <Sylvain...@m4x.org.invalid> GPG Key: 0x0F53AF99
Page personnelle http://contraintes.inria.fr/~soliman/

Maneesh Patel

unread,
Mar 5, 2012, 12:25:26 PM3/5/12
to
I didn't want to rant about this, but since you asked...

The problem with spaces is, for example, that a bash function like
this:

bulkget ()
{
files="";
for file in $@;
do
files="$files `pwd | sed 's/[remote tree]/[local tree]/g'`/
$file";
done;
scp -p user@[IP address]:"$files" .
}

won't work (even if you do enter, e.g., bulkget "File With
Spaces.docx"). Yes, you can rewrite the function as:

bulkget ()
{
files="";
for file in "${@}";
do
files="$files `pwd | sed 's/[remote tree]/[local tree]/g'`/
\"$file\"";
done;
scp -p user@[IP address]:"$files" .
}

but it's a hassle particularly for a non-expert in bash scripts to
figure out how to fix the syntax. This is one example; there are all
sorts of instances where some script or other fails on filenames with
spaces. (I admit that scripts should be written in the first place to
accommodate files with spaces and other junk; however, it would be
better if the files had nice names, for exactly the same reason that
one should create HTML 4.01 Transitional websites and not simply rely
on browsers programmed to deal with lousy HTML.)

Thank you for suggesting rewriting the mutt macro to include
postprocessing of my Attachments directory; I'll take another look at
the manual to see how to go about implementing this.

Kind regards,
--maneesh

Jorgen Grahn

unread,
Mar 5, 2012, 3:38:51 PM3/5/12
to
On Mon, 2012-03-05, Maneesh Patel wrote:
> I didn't want to rant about this, but since you asked...
>
> The problem with spaces is, for example, that a bash function like
> this:
...
> won't work (even if you do enter, e.g., bulkget "File With
> Spaces.docx"). Yes, you can rewrite the function as:
...
> but it's a hassle particularly for a non-expert in bash scripts to
> figure out how to fix the syntax. This is one example; there are all
> sorts of instances where some script or other fails on filenames with
> spaces. (I admit that scripts should be written in the first place to
> accommodate files with spaces and other junk; however, it would be
> better if the files had nice names [...]

I'd put it a bit differently: if 99.9% of all files you handle don't
have funny characters, it becomes a bit of a hassle to deal those that
do.

The command line is more of a problem than shell scripts IME. You can
say
% find -something -print0 | xargs -0 do_something
but it's more error-prone and less flexible than
% find -something | xargs do_something
which works well most of the time but breaks on names with spaces.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Ulrich Thorwarth

unread,
Mar 5, 2012, 4:20:46 PM3/5/12
to
ok, no really solution so far, here is my next idea:
Unfortunately I'm not too familiar with macros in mutt, don't know about
<shell-esacpe> or calling scripts, BUT...

If it is one particular directory, what about a scripts like this (might
be triggered by a mutt-macro, cronjob or manually):


#!/bin/sh
if [ -z "$1" ]
then
infiles=`ls -x *.doc`
else
infiles="$@"
fi

for i in $infiles; do
NAME=`echo $i | tr -d \'\"\)\(\:\,\?\!\/\-`
NAME=`echo $NAME | tr [:blank:] _`
NAME=`echo $NAME | tr 'A-Z' 'a-z'`
mv -v $i $NAME
done


If treating all files in the directory is too much, take
infiles=`ls -tr1 | tail -1`

and you only treat the newest!

(all untested)
Ulrich



B.H.

unread,
Mar 16, 2016, 4:58:49 AM3/16/16
to

Rather than reinventing the wheel with sed, awk, etc, consider getting detox.
It's in many distros' standard repos.
Detox rocks, and has some customization options.
You can save your attachments to a given folder, and then run detox on that
folder, probably all in one macro, but if not certainly with a 2nd one/sorry,
really sleepy, not at my most creative.

macro attach 0 "!/usr/bin/detox $HOME/attachments/*\n"

seems like it will work, but I've not tsted it.
You may be able to combine your macro with this, but I can't think of a
similar compound command I run or have seen in somones muttrc at the moment.
This is an old thread, so if you are still around, reply and I'll work on this
with you ASAP.
Regards,
0 new messages