Spread pages book layout in PDF documents

This week I composed a document with flyleaf and subsequent two-paged layout like in these two examples with Adobe Indesign and Microsoft Word:

But when exporting/printing to a PDF file the PDF document in Acrobat Reader shows only a single page at a time. One may manually select the view option with two-paged layout and flyleaf but I wanted the document to be always shown that way! After Googling I found a solution using Adobe Acrobat Pro where it is possible to set this option and save the changes directly to the PDF file. Unfortunately Acrobat Pro isn’t cheap and I don’t own a copy so I looked for an alternative.

Finally I found BeCyPDFMetaEdit by Benjamin Bentmann which is a small freeware Windows utility for changing the XMP meta data of a PDF document file. The viewer preference actually is just a XMP setting. The program can remove any application or pdf printer metadata as well (like “Created with …foo program” or “Printed using PDF…bar”). The result after opening the PDF document in Adobe Acrobat Reader then looks like (before vs. after):

Ta-da, A book!

Perl -e in Windows batch and Linux shell scripts

Recently I had to extract a partial string from a space-seperated list of names in a loop within a script. There have to be two versions of that script, one for Windows and one for Linux shell. To loop through one such list is quite easy, in Windows:

@echo off
set WEBSITES=Test1 Test2 Test3

echo WebsiteName=%%A


and in Linux shell:

WEBSITES="Test1 Test2 Test3"

echo "Website= $WEBSITE_ALIAS ..."

But now came a second list of string into play containing the domain names of the website aliases:

WEBSITES="Test1 Test2 Test3"
DOMAINNAMES="www.test1.lan www.test2.lan www.test3.lan"

In a conventional programming language I would just use a for-loop with an index variable and utilize that variable to access both arrays within one loop. But in that batch/shell scripting this turned out to be quite tricky. My solution here was a small inline PERL script. For Windows:

set WEBSITES=Test1 Test2 Test3
set DOMAINNAMES=www.test1.lan www.test2.lan www.test3.lan
perl -e "use strict; die("argv mismatch!") if !@ARGV or scalar(@ARGV) < 2; my @Websites = split(/[s,;|]/, $ARGV[0]); my @Domains = split(/[s,;|]/, $ARGV[1]); die("number of aliases differs from domain names!") if scalar(@Websites) != scalar(@Domains); for(my $i=0; $i<scalar(@Websites); $i++) { system('perl dosomething.pl -user /Root/'.$ENV{'WEBSITE_UNIT'}.'/admin -passwd admin -servername '.$ENV{'SERVER_NAME'}.' -alias '.$Website[$i].''); system('perl doanotherthing.pl -user /Root/'.$ENV{'WEBSITE_UNIT'}.'/admin -passwd admin -alias '.$Website[$i].' DomainName="'.$Domains[$i].'"'); }" "%WEBSITES%" "%DOMAINNAMES%"

And for Linux:

WEBSITES="Test1 Test2 Test3"
DOMAINNAMES="www.test1.lan www.test2.lan www.test3.lan"
perl -e 'use strict; die("argv mismatch!") if !@ARGV or scalar(@ARGV) < 2; my @Websites = split(/[s,;|]/, $ARGV[0]); my @Domains = split(/[s,;|]/, $ARGV[1]); die("number of aliases differs from domain names!") if scalar(@Websites) != scalar(@Domains); for(my $i=0; $i<scalar(@Websites); $i++) { system("perl dosomething.pl -user /Root/'${WEBSITE_UNIT}'/admin -passwd admin -servername ".$ENV{"SERVER_NAME"}." -alias ".$Website[$i].""); system("perl doanotherthing.pl -user /Root/'${WEBSITE_UNIT}'/admin -passwd admin -alias ".$Website[$i]." DomainName="".$Domains[$i]."""); }' "$WEBSITES" "$DOMAINNAMES"

Notice the different handling of the ticks and quotes and the different access to external parameters. In Windows there’s no difference in PERL’s $ENV hash whether accessing real environment variables or local variables set by the batch script. Not so under Linux: I can only access my system-wide exported environment variable SERVER_NAME using $ENV but not my local script’s WEBSITE_UNIT variable. When using the exec -e with perl for Windows I had to use quotes to wrap the execution PERL-code but for linux shell, I needed single -ticks which can NOT be used inside the PERL code – not event when escapting them like ‘. The single ticks are “reserved” by the shell script for being able to insert shell variables anywhere.

Disable auto word wrap in nano

Quite annoying when editing source code via console in nano is the automatic word wrap, which often screws up compilation of the edited file. To disable the word wrap, just edit the .nanorc in your home directory and add:

set nowrap

You can also do this when starting nano:

nano -w <file>

FocalApp v0.2

I changed my FocalApp java application a little bit. First, there was sometimes a memory exception as the old version did a “new File()” in each loop iteration and apparently the garbage collector couldn’t catch up. I moved the file-open operation into an own class method and now the GC seems to get its job done. Second, I added some more command line parameters for specifying the camera model, the file extensions and the file name pattern – which all filter the resulting image file list used for the focal length count. Hence one can now say “Only count the images whose EXIF data’s camera model is a Canon 300D, but only the CRW files and only the files starting with CRW_50”. In addition I added a little help screen:

javac -Xlint:unchecked -classpath .;metadata-extractor-2.4.0-beta-1.jar FocalApp.java
jar cfm FocalApp.jar Manifest.txt *.class

java -jar FocalApp.jar [Options]

For all images in the given directory and its subdirectories read the
lenses' focal length from the exif data and print a summary of how
often the particular focal lengths are used.

Currently this program works with the following image types:
* CRW - Canon Raw
* CR2 - Canon Raw v2
* NEF - Nikon Raw
* ARW - Sony Raw

-imagepath          path for the images to work on                          (required)
-camera             string with the name of the camera, the program will    (optional)
                    only count the images containing that camera name
-fileext            only count images with that file extension(s)           (optional)
-filename           only count images matching this regular expression      (optional)

java -jar FocalApp.jar -imagedir "D:testimages"
java -jar FocalApp.jar -imagedir "D:testimages" -camera "Canon EOS 30D"
-fileext "jpg,crw" -filename "^CRW(.*)"

Feel free to get the new version here. You can find the original blog post to the FocalApp here.

Find all files containing a string but NOT containing another

Find all files containing a string but NOT containing another.

Believe it or not, this was quite a challenge. I tried to use precedent and subsequente regular expressions in UltraEdit for this but UltraEdit’s regex is quite dumb. So I came up with good-old shell:

find . -name "*html" | xargs grep -l 'StringA' | xargs grep -L 'StringB'

Beginning from the current directory (“find .”) this finds all html files containing the string ‘StringA’ in the first grep statement but NOT the ones containing the string ‘StringB’ in the second grep statement. If any nerd could tell me how to do this more easily, this would bring light in my day-to-day life. 🙂 Actually this shell piping is pretty fast. I ran this over hundreds of html files in half of a second. Using PERL you’d need 30 lines and half an hour, I think.

To do this in Windows, you’ll need gfind, xargs and GNU grep (see UnxUtils website)