Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#353666 - 31/07/2012 21:35 Help translate this PHP to bash
hybrid8
carpal tunnel

Registered: 12/11/2001
Posts: 7738
Loc: Toronto, CANADA
I've just started using some command-line PDF tools from the pdfjam package (uses Tex with pdfpage package) and decided to try and integrate it with some scripts I'd previously created.

The PHP script below works as desired from shell but the shell-exec to run the pdfjam script doesn't seem to do anything when this is all included in Mac OS X's Automator (which I've long-ago extended to support PHP scripts).

Since tying this all together with other components requires Automator at the moment, I'd really like to find a solution to fit into that process. I think replacing this PHP script with a bash script to do the same thing will get me what I need. I've given up trying to figure out why the shell_exec (or simply exec) doesn't work when going through Automator.


Code:
<?php

// take a bunch of PDF files from one folder, parse their filenames, create folders based
// on the parsed information and finally pass the files through pdfjam to crop them,
// sending new files to the appropriate previously created folders

// PDF source filenames are always in the format "YEAR-MM-DD Some Text.pdf"  example: "2012-08-01 Recent Scan.pdf"

// 2 unix paths are passed in as arguments

putenv("TZ=America/Montreal"); // change time zone to Eastern

// trim the bogus first parameter - automator seems to pass a "-" as a parameter preceeding the actual params
array_shift ($argv);

// base path to the folders we'll later create and put files in
$baseFolder = $argv[0];

// source path to the PDF files to be manipulated
$pdfTempFolder = $argv[1];
	
$dirHandle = opendir($pdfTempFolder);
	
// read all (matching) filenames from the pdftempfolder
while( ($fileName = readdir($dirHandle)) !== false ) {

	// (only build list of PDF files starting with "2"
	if( (substr($fileName, strrpos($fileName, '.') + 1) == "pdf") && (substr($fileName,0,1) == "2" ) ) { 
		$pdfFiles[] = $fileName;
	}
}

if ($pdfFiles) {  // only try the loop if there were files listed

	// loop through all the files ($currentFile) and move them to their new folder with new name	 
	foreach ($pdfFiles as $currentFile) {

		// grab the date from the begining of the filename so we can figure out the destination path
		$currentShipDate = substr ($currentFile, 0, strpos ($currentFile, " ") );

		// create the new filename from the rest of the filename
		$cleanFileName = substr ($currentFile, strpos ($currentFile, " ") +1);

		$yearFolder = date("Y", strtotime($currentShipDate) );  // 4-digit year, eg: "2012"
		$dayFolder = date("M d", strtotime($currentShipDate) ); // 3 char month & 2 digit day, eg: "Aug 01"

		// create new folders if they don't already exist
		if (!is_dir("$baseFolder/$yearFolder"))
			mkdir("$baseFolder/$yearFolder"); 

		if (!is_dir("$baseFolder/$yearFolder/$dayFolder"))
			mkdir("$baseFolder/$yearFolder/$dayFolder"); 

		shell_exec ("pdfjam '$pdfTempFolder/$currentFile' --trim '0in 5.5in 0in 0in' --clip true 
--papersize '{8.5in,5.5in}' --outfile '$baseFolder/$yearFolder/$dayFolder/$cleanFileName'");

		echo "$baseFolder/$yearFolder/$dayFolder/$cleanFileName"."\n";
	}		
}		


?>


Edited by hybrid8 (01/08/2012 02:56)
_________________________
Bruno
Twisted Melon : Fine Mac OS Software

Top
#353678 - 01/08/2012 02:52 Re: Help translate this PHP to bash [Re: hybrid8]
hybrid8
carpal tunnel

Registered: 12/11/2001
Posts: 7738
Loc: Toronto, CANADA
I've come up with an entirely better solution which leaves this script as it was prior to the use of the shell_exec, so there's no need to go the bash route anymore.

Now I'm just trying to figure out why my Automator solution works brilliantly as an app with drag and drop and directly from within Automator, but not as a print plugin. Having tried a really simple file-moving test, the best I can come up with so far is that Automator-based print plugins are not working in Mac OS X 10.8 at all at this time.
_________________________
Bruno
Twisted Melon : Fine Mac OS Software

Top