Wednesday, February 6, 2013

Change extension to lowercase from terminal

When I recently copied some photos to my pc I noticed that the extension was uppercase (JPG).
A simple way to fix this is
rename -n 's/.JPG$/.jpg/' *.JPG
This works if the files are all in the same folder.

Thursday, December 20, 2012

How to print multiple pages per sheet (even strange values)

I was using psnup to generate ps/pdf files with multiple pages per sheet...

but one day I needed to arrange 24 pages per sheet (it was a thumbnail document, one small image per page...) and I did not manage to make it work with psnup..

this solved my problem:
pdfjam --suffix nup --noautoscale true --nup '4x6' inputfile.pdf

enjoy

Tuesday, December 18, 2012

How to make an A5 sized booklet with A4 paper

Actually it is very easy to make an A4 sized booklet, and you can find this option even in some printer's settings..

With A4 sized booklet I mean printing the pages with A5 size, landscape, on A4 sheet so that if you take the printed papers and you fold in half you obtain a sort of book.

Actually it is a little bit harder to make a booklet with A5 paper.. and the main reason is that I do not have A5 paper..
Another reason is that in my opinion it is easier to print on A4 sheets without trouble (i.e. without feeding and paper alignment problems)

So here what I did:

  • take your pdf and convert to postscript: pdftops in.pdf out.ps
  • make a standard booklet ordered document: psbook out.ps outbook.ps
  • (here is the tricky part) insert two blank pages every two pages (this is explained later)
  • print to a new file, 4 pages per page (i did this because with evince i could not print double sided, but with other programs it added extra margins)
  • print this last document two sided, with the printer.. (eventually to save some paper you can print the first half pages (two sided), then turn the sheets and print the remaining pages on the other half of the sheets)
  • cut the pages and place them in the correct order

It sounds tricky but it is the simplest solution I found...

As you probably understood I inserted the blank pages to print only on the top half of each sheet.. this is necessary since I did not find a software able to order the pages for this kind of booklet.

To insert the blank pages I used two scripts:

#!/bin/bash
if [ $# -ne 3 ]
then
  echo "Usage example: ./pdfInsertBlankPageAt 5 src.pdf res.pdf"
  exit $E_BADARGS
else
  pdftk A=$2 B=blank.pdf cat A1-$(($1-1)) B1 A$1-end output $3
fi

require "optparse"
options = {:default => "args"}
ARGV.options do |opts|
opts.banner = "Usage:  #{File.basename($PROGRAM_NAME)} [OPTIONS] OTHER_ARGS"
opts.separator ""
opts.separator "Specific Options:"
opts.on( "-p pages", "--pages", Integer,
        "Num of pages" ) do |opt|
options[:pages] = opt
end
opts.separator "Common Options:"
opts.on( "-h", "--help",
        "Show this message." ) do
puts opts
exit
end
begin
opts.parse!
rescue
puts opts
exit
end
end
pdf = ARGV[0].chomp
range = (3..options[:pages]).step(2)
range.reverse_each do |pagenum|
%x{./pdfibpa #{pagenum} #{pdf} tmp#{pdf}}
%x{./pdfibpa #{pagenum} tmp#{pdf} #{pdf}}
end 

The first script is a shell script which inserts a page at a specified position. I named it pdfibpa.. I generated a blank page (blank.pdf) to do the trick..

The second script is a ruby script which adds two blank pages every two pages...

Maybe in the future I will build a simpler solution and publish a gem..

Sunday, September 2, 2012

How to make a screencast video with ubuntu

Sometimes I needed to make some screencast videos, for example when I do magic with Sublime Text 2 sometimes I want to record it ;)

I found that xvidcap works very well

Paper Reference Manager

Some time ago I started using mendeley, to organize some papers but I got soon disappointed..
The user interface of the client software is quite unusable..

First of all the tag management is not effective, since to add a tag is a pain: click, write, click in another place to remove focus (otherwise you can not do anything.. not even switching workspace) without any keyboard shortcut!
Also searching for tags is frustrating.. you can not save searches and if you search for tag 'read' you will also find papers with tag 'toread'..
Another bad point is the sort order: spaces are ignored so 'a zebra' comes after 'an elephant', since z>n but if you make a list of titles everywhere else, the sorting keeps spaces into account.. hence you will have mismatching lists..

A good feature of mendeley desktop, for which I kept mendeley for some time, is the internal editor, that allows for highlights and notes..

But..

Zotero have shortcuts, save advanced searches, notes, tag management and if you open pdfs with okular you can even highlight or add notes into pdf..

I'll try zotero for a while, syncing pdfs with dropbox...

Thursday, August 23, 2012

Ruby helps in gaming

Recently I found the game Clockwords: prelude (http://www.kongregate.com/games/gabob/clockwords-prelude) in which you have to write words to shoot letters and kill spiders.

Well after some levels the game become boring so I decided to find another solution... thanks to the xsendkeycode command and a simple ruby script...

first of all install xsendkeycode by typing
sudo apt-get install lineakd
then copy and paste this code into a file, make it executable and run it.
#!/usr/bin/env ruby
words = [
'congregationalists',
'chlorofluorocarbon',
'chlorofluorocarbons',
'compartmentalizing',
'conceptualizations',
'contradistinctions',
'conversationalists',
'counterintelligence',
'counterrevolutionaries',
'uncharacteristically']
keycodes = {
'q' => 24,
'w' => 25,
'e' => 26,
'r' => 27,
't' => 28,
'y' => 29,
'u' => 30,
'i' => 31,
'o' => 32,
'p' => 33,
'a' => 38,
's' => 39,
'd' => 40,
'f' => 41,
'g' => 42,
'h' => 43,
'j' => 44,
'k' => 45,
'l' => 46,
'z' => 52,
'x' => 53,
'c' => 54,
'v' => 55,
'b' => 56,
'n' => 57,
'm' => 58
}
sleep 2
words.each do |word|
  word.each_char do |letter|
    `xsendkeycode #{keycodes[letter]} 1`
     sleep 0.2
     `xsendkeycode #{keycodes[letter]} 0`
  end
  `xsendkeycode 36 1`
  sleep 0.2
  `xsendkeycode 36 0`
end

note that you need to quickly switch to the game windows and focus the game area with a click.
You also need to interrupt the script (ctrl+c) when the match is finished..

Long words are not the best.. I lost at level 9.. maybe short words with lots of letters contained in the boiler are the best.

Thursday, July 19, 2012

Ruby serialport and the missing byte

While developing a project @iralab about an Arm Cortex board which should control a DC motor, I found a very strange behaviour: sometimes I received from the serial port only 89/90 bytes.. other times 3/4 and so on..

So I googled something like 'ruby serialport missing byte' and I found a lot of useful hints such as

Unluckily none of these worked...

After some debug i found that only some chars were not sent/received.. 0xa0 to 0xbf..

And then I realized what I was doing wrong! I used the getc method to read from serial!
Switching to getbyte completely fixed the problem.

Here some reference:

Hope it helps