Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

pdf - cropmarks with applescript.

Hello,

I'm not complete pleased with the (batch)automation in acrobat pro. So I wonder if this is possible with applescript and maybe you did this already.

I would like to cut all pdf files from 1 folder ... (select folder)
Cut the pdf files at the crop marks and 'save as' the pdf with at the end of the filename "-CROP".pdf in file name.
The only special part is that the pdf files are not always with the same page formats. So sometimes the format of paper is different in the files. So sometimes also a mix of paper formats in the same pdf.

But all the crop boxes are always the same : DIN A4 format. 210 to 297 mm

Isn't this possible with image events ??
Prefer no use of acrobat for this.


If you think you can help me?
Many Thanks!

null

Different workstations from 10.3.9 to latest// G5 server - OS 10.3.9, Mac OS X (10.5.6)

Posted on Sep 14, 2010 4:45 AM

Reply
6 replies

Sep 20, 2010 7:57 AM in response to Colin @ mac.com

Can some one help me with this script?
It does nothing after changing some parts.
I try to say to finder all pdf files from this folder.
Then use image events
and try to crop the pdf.

I had it working once but then it was only 1 page. Did some changes.
Now the pdf stays original.


---------
property FileTypeList : {"PDF"}
property extension_list : {"pdf"}

on run
display dialog "Don't doubleclick me! Drop a folder on top of me to execute program!" buttons {"OK"}
end run

on open these_items

with timeout of 600 seconds
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items) as alias
set this_info to info for this_item
if folder of the this_info is true then
process folder(thisitem)
else
try
set this_extension to the name extension of this_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of this_info
on error
set this_filetype to ""
end try
if (folder of the this_info is false) and (alias of the this_info is false) and ((this_filetype is in the FileTypeList) or (this_extension is in the extension_list)) then
process item(thisitem)
end if
end if

end repeat
end timeout
end open

on process folder(thisfolder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
set the this_info to info for this_item
if folder of the this_info is true then
process folder(thisitem)
else
try
set this_extension to the name extension of this_info
on error
set this_extension to ""
end try

try
set this_filetype to the file type of this_info
on error
set this_filetype to ""
end try
if (folder of the this_info is false) and (alias of the this_info is false) and ((this_filetype is in the FileTypeList) or (this_extension is in the extension_list)) then
process item(thisitem)
end if

end if

end repeat
end process_folder

on process item(theseFiles)
tell application "Image Events"
repeat with _thisFile in _theseFiles
set _image to open _thisFile
set _dimensions to dimensions of _image
set _width to item 1 of _dimensions
set _height to item 2 of _dimensions
try
if _width < 343 then
if _height < 485 then
tell application "Finder"
display dialog ¬
"Image width and height (" & _width & ") is too small to crop."
end tell
exit repeat
end if
end if
if _width > 343 then
if _height < 485 then
tell application "Finder"
display dialog "Image height (" & _height & ") is too small to crop."
end tell
exit repeat
end if
end if
if _width < 343 then
if _height > 485 then
tell application "Finder"
display dialog "Image width (" & _width & ") is too small to crop."
end tell
exit repeat
end if
end if
on error errmess
tell application "Finder"
display dialog "This error occured: " & errmess buttons "OK" default button 1
end tell
end try

-- 2 - 3
try
if _width > _height then
crop _image to dimensions {_width, _height}
save _image
close _image
else if _width < _height then
crop _image to dimensions {343, 485}
save _image
close _image
else if _width = _height then
-- crop _image to dimensions {_height, _height}
-- save _image
close _image
end if
on error errmess2
tell application "Finder"
display dialog "This error occured: " & errmess2 buttons "OK" default button 1
end tell
end try

end repeat
end tell


end process_item
---------

me 3

Sep 21, 2010 4:23 AM in response to Colin @ mac.com

Hello

Couple of things I noticed.

First of all, I doubt you can use 'Image Events' for your purpose because, as fas as I know, Image Events can read PDF but cannot write PDF, which means you cannot produce PDF output.
Also I doubt it can handle multi-page PDF file correctly.

As I understand it, you can use Image Events only to convert one-page PDF to some image format such as JPEG but cannot use it to edit multi-page PDF and yield PDF output.

---
That's being said, the script needs at least two fixes. (Even after these fixes being applied, I'm afraid it won't work as expected because of the above reasons)

1) The current process_item() expects list (of aliases) as its parameter while you're passing alias to it. So you may change the process_item() handler :

on processitem(theseFiles)
tell application "Image Events"
repeat with _thisFile in _theseFiles
set _image to open _thisFile
-- omitted (the same as original)

end repeat
end tell
end process_item

to :

on processitem(thisFile)
tell application "Image Events"
set _image to open _thisFile
-- omitted (the same as original)

end tell
end process_item

Or you may leave the process_item() as it is and change the calling statement :

processitem(thisitem)

to :

processitem({thisitem})


2) The file type is of four characters. So your FileTypeList should be :

property FileTypeList : {"PDF "} -- # note the space after 'PDF'


After all, Acrobat Pro would be your friend.
Regards,
H

Sep 21, 2010 4:40 AM in response to Hiroto

Hello Hiroto,

Thanks for the advice. I will read en test today.
My previous test works indeed with 1 page and image events only save the first page.
I do Have acrobat pro. But would like to do this without 3partie software.
Have some time today and will learn a bit more.

If image events only works on 1 image at time.
Step 1 : Is maybe possible to extract all pages from 1 pdf
Step 2 : edit/crop the pdf pages with image events
step 3 : and then put the pages back together in 1 pdf.

I tested the image events part and It crops and save the pdf.
Thanks Hiroto !

xxx

Sep 22, 2010 4:45 PM in response to Colin @ mac.com

Hi,

Here is a script that runs on *Mac OS X 10.4* or later.

just replace the function *on process_item(theseFiles)* by this

on process_item(f)
set posixPDFPath to POSIX path of f
do shell script "export VERSIONERPYTHON_PREFER_32BIT=yes
/usr/bin/env python -c 'import os, sys, CoreGraphics
pdf_filename = "" & (posixPDFPath) & ""
pdf_name, ext = os.path.splitext(pdf_filename)
this_pdf = CoreGraphics.CGPDFDocumentCreateWithProvider( CoreGraphics.CGDataProviderCreateWithFilename( pdf_filename ) )
if this_pdf is None:
print "Error reading PDF document"
sys.exit(1)
newfilepath = "%s-CROP.pdf" % (pdf_name)
cnt = this_pdf.getNumberOfPages()
writeContext = CoreGraphics.CGPDFContextCreateWithFilename(newfilepath, CoreGraphics.CGRectMake (0, 0, 0, 0))
for page_number in range( 1, cnt + 1 ):
currRect = thispdf.getPage(pagenumber).getBoxRect(CoreGraphics.kCGPDFMediaBox)
_width = currRect.getWidth()
_height = currRect.getHeight()
x1 = ((595 - _width)/2) # --> A4 210 mm is 595 points
y1 = ((842 - _height)/2) # --> A4 297 mm is 842 points
if x1 < 0:
currRect.origin.x = x1
width=width + x1 + x1
if y1 < 0:
currRect.origin.y = y1
height=height + y1 + y1

pageRect = CoreGraphics.CGRectMake (0, 0, _width, _height)
writeContext.beginPage(pageRect)
writeContext.drawPDFDocument(currRect, this_pdf, page_number)
writeContext.endPage()

writeContext.finish()'"

end process_item

pdf - cropmarks with applescript.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.