Wednesday, August 17, 2011

Scala script to merge and archive PDF Files...

Just configure for example thunar action, in order to be able to select N pdf files, right-click and then launch the script. It will merge the pdf file into a single one with a new name starting with a timestamp. Previous files are backup into a Trash directory, each file being renamed with its path. Example :

bill.pdf check.pdf order.pdf   ==> 20110810-bill.pdf

#!/bin/sh
exec scala -nocompdaemon -savecompiled "$0" "$@"
!#
import sys.process._
if (args.size>0) {
  def now     = new java.util.Date()
  val defname = args(0).split("/").last
  val sdf     = new java.text.SimpleDateFormat("yyyyMMdd")
  val dest    = "%s-%s".format(sdf.format(now),defname)
  val res = Process("/usr/bin/pdftk"::args.toList:::"cat"::"output"::dest::Nil) !!
  val trash = scala.util.Properties.userHome+"/"+"Trash"+"/"
  Process("mkdir"::"-p"::trash::Nil)!
  
  for (file <- args) {
    Process("mv"::file::(trash+file.replace("/","++")+"-"+sdf.format(now))::Nil)!
  }
}


Of course this script relies on "app-text/pdftk-1.44" (gentoo package name), for more information see here : pdftk-the-pdf-toolkit.
CONTEXT : Linux Gentoo / Scala 2.9.0 / Java 1.6.0_26 / XFCE 4.8.0 / Thunar 1.2.1

No comments:

Post a Comment