Ghostscript PDF Code Page

21 Feb 2012 - 07:43

-- version 0.1

-- © 2010 - 2012 Jürgen Schell

-- No guarantee whatsoever.

-- Regard as training example

-- http://www.j-schell.de

-- Distribution allowed if the copyright remains intact


-- Purpose of this script:

-- Use Ghostscript to convert PostScript print jobs, EPSF files and PDF files

-- to PDF.

-- Script works as folder action script and should be copied to

-- /Library/Scripts/Folder Action Scripts/

-- Ghost script executable is expected to be /usr/local/bin/gs


-- if other Ghostscript output devices are needed, check handler find_job



-- Changes February 2012:

-- Transfered EPS bounding box stuff from bitmap version of script.

-- Now, EPSF files are distilled with their proper bounding box

-- Put compatibility level in separate property to make changes simpler.

-- Switched on Preserve separations. This keeps spot colours

-- intact and should not have any negative effect on other uses.


property compatibility_level : "1.4"


property gsSettinglst : "/gs -dSAFER -dCompatibilityLevel=?¿?comp_level -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=?¿?outfile -dPreserveSeparation=true -dPDFSETTINGS=/?¿?quality ?¿?add_opts?¿? -c .setpdfwrite -f ?¿?infile"


property standardGSPaths : "/usr/local/bin" -- standard paths for gs


property qualities : {"prepress", "printer", "screen", "ebook", "default"}

property folder_names : {"prepress", "printer", "screen", "ebook", "default"}

property magic_bytes : {"%PDF-", "%!PS-Adobe", "?–”?"}


property out_folder : "PDFs"



property use_quality : 1




on adding folder items to theFolder after receiving theFiles

main_action(theFolder, theFiles)

end adding folder items to




on run

-- mainly for debugging

set f to choose file

tell application "System Events"

set theFolder to (path of (container of f))

end tell

set theFolder to alias theFolder

main_action(theFolder, {f})

end run


on main_action(theFolder, theFiles)

--try

tell application "System Events"

if not (exists folder out_folder of theFolder) then

make new folder at end of theFolder with properties {name:out_folder}

end if

set f_name to name of theFolder

end tell

set out_path to (POSIX path of theFolder) & out_folder & "/"

--set pathVar to addtoPATH(standardGSPaths)

--set PATHcall to "PATH=" & quoted form of pathVar & ";"

set use_quality to find_in_list(f_name, folder_names)

--display dialog (theFiles as text)

repeat with X from 1 to count theFiles

set curr to item X of theFiles

set file_name to clean_name(curr)

if check_magic(curr) then

set thePath to POSIX path of curr

(POSIX path of theFolder)

set thePath2 to out_path & file_name & ".pdf"

tell application "System Events"

try

(POSIX file thePath2) as alias

set thePath2 to out_path & file_name & " " & my date_string() & ".pdf"

end try

end tell

set thePath to quoted form of thePath

set thePath2 to quoted form of thePath2

--set currCommand to standardGSPaths & gsSettinglst

set currCommand to gsSettinglst

set currCommand to textReplace(currCommand, "?¿?infile", thePath)

set currCommand to textReplace(currCommand, "?¿?outfile", thePath2)

set currCommand to textReplace(currCommand, "?¿?quality", item use_quality of qualities)

set currCommand to textReplace(currCommand, "?¿?comp_level", compatibility_level)

-- activate crop according to bounding box comment, if in file is EPSF

if check_epsf(curr) then

set currCommand to textReplace(currCommand, "?¿?add_opts?¿?", " -dEPSCrop")

else

set currCommand to textReplace(currCommand, "?¿?add_opts?¿?", "")

end if

set currCommand to standardGSPaths & currCommand

try

do shell script currCommand

log currCommand

tell application "Finder"

delete curr

end tell

on error theError number theNum

if theNum ? -128 then

display dialog theError

end if

end try

end if

end repeat

end main_action



--======================================


on find_in_list(the_val, the_list)

repeat with X from 1 to count the_list

if item X of the_list = the_val then

return X

end if

end repeat

return 5

end find_in_list


--======================================


on clean_name(file_ref)

tell application "System Events"

set f_name to name of file_ref

set f_suffix to (name extension of file_ref)

end tell

if f_suffix is not "" then

set suffix_length to (length of f_suffix) + 2

set f_name to characters 1 thru (suffix_length * -1) of f_name as text

end if

return f_name

end clean_name


--======================================



on textReplace(mainText, fromText, toText)

set oldDelims to AppleScript's text item delimiters

set AppleScript's text item delimiters to {fromText}

set textList to every text item of mainText

set AppleScript's text item delimiters to {toText}

set mainText to textList as text

set AppleScript's text item delimiters to oldDelims

return mainText

end textReplace


--======================================


on check_magic(file_ref)

set check_strg to "xxxxxxxxxx"

try

set fh to open for access file_ref

set check_strg to (read fh from 1 to 12)

end try

try

close fh

end try

repeat with X from 1 to count magic_bytes

if (offset of (item X of magic_bytes) in check_strg) = 1 then

return true

end if

end repeat

return false

end check_magic


--======================================


on check_epsf(file_ref)

-- check for EPSF at beginning of file

set r to false

set check_strg to "xxxxx"

try

set fh to open for access file_ref

set check_strg to (read fh from 1 to 100)

end try

try

close fh

end try

if check_strg contains "EPSF" then

set r to true

end if

return r

end check_epsf


--======================================


on date_string()

set now to current date

return (((year of now) as string) & "-" & pad_2((month of now) as integer) ¬

& "-" & pad_2(day of now) & " - " & pad_2(hours of now) & "-" & pad_2(minutes of now) & "-" & pad_2(seconds of now))

end date_string


--======================================


on pad_2(num)

set num to num + 100

return ((characters 2 thru 3 of (num as text)) as text)

end pad_2