Copy link to clipboard
Copied
I have been saving single pages of PDF's using the GUI, but it would be much faster to use Java.
I can't work out how to set the save path, and I also need error trap any failed TIFFS, and save as JPEG's instead. Acrobat won't save TIFFS larger than the international size. Also need to increment each file name to match the page number, please.
So far, I've got...
set tAT to POSIX path of ((path to desktop as text) & "Temporary Acrobat TIFFS")
tell application "Adobe Acrobat"
activate
tell document 1
set theTempName to name of document 1
set Params to ""
set Params to Params & "var dEnd = (this.numPages-1);" & return as text
set Params to Params & "this.extractPages(0,dEnd);" & return as text
set Params to Params & "var cFlName = this.documentFileName ;" & return as text
set Params to Params & "save to : ((path to desktop as text) + \"Temporary Acrobat TIFFS\" + \":\"+ cFlName) using: \".tiff\";" & return --< this fails, correct format needed.
#set Params to Params & "var this.closeDoc(false);" as text
do script Params
end tell
end tell
Copy link to clipboard
Copied
There is no "/Desktop" folder - you need to read exactly what I typed. I did not say that there was no Desktop folder. That's different, and when it comes to programming, these small differences are important. When it comes to Unix paths (or what you refer to as Posix paths in AppleScript), a path that start with a '/' always means that it is in the "root" directory of the disk. What I said was that there is no "Desktop" folder in the root directory, which does not mean that there is no Desktop folder in a user's home directory. Every user on Mac OS does have a Desktop folder, but that is referenced differently (e.g. /Users/username/Desktop).
Take a look at the App.getPath() method: Acrobat DC SDK Documentation
This is all there is in terms of asking Acrobat about user specific (or application specific) paths. Unfortunately, there is no function to get the user's Desktop. There is however a function to get the user's Documents directory, which on a Mac is on the same level as the user's Desktop. This means you could get the Documents directory and then just replace "Documents" with "Desktop":
var pathToDesktop = app.getPath("user", "documents").replace("Documents", "Desktop");
This is dangerous, because the next version of Mac OS X could move Documents, or a user could have reconfigured Mac OS X to store Documents in a different location. This is one area where you could use your AppleScript wrapper to your advantage and get the path to the desktop as a Posix path. (e.g. set pp to POSIX path of (path to desktop) as text )
As to why you are getting error when trying to save - until you fix your path problem, it's impossible to say if that is caused by a non-existing folder ("/Desktop") or something else.
Copy link to clipboard
Copied
Thanks Karl, you're being of immense help.
I've done a LOT of reading about getting desktop paths in Java on Mac, and this is typical of postings that DON'T work.
Script to Create Folders Using Photoshop Scripting Mac/Windows
//Folder to create on the desktop
var folder1 = Folder("~/desktop/My New Folder");
//Check if it exist, if not create it.
if(!folder1.exists) folder1.create();
These sort of postings is why I've persisted in asking about desktop paths. But, now, I'll try and pass a variable to my Applescript Java script using your suggestion. BUT, does this mean there's no way to test the code in the Console?
Regards
Santa
Copy link to clipboard
Copied
You cannot mix JavaScript from one environment with a different environment: JavaScript in Acrobat runs in a "sandbox" - which means it cannot communicate (or only in a very limited fashion) with the outside world, whereas Javascript in PostScript is free to do whatever it wants (within limits). The reason being that you can store JavaScript in a PDF file and then send it on it's way. You don't want a PDF file that you downloaded from the Internet to mess around with your system. So this is a good thing, but makes a lot of stuff much harder or impossible to do.
Copy link to clipboard
Copied
Thanks again Karl.
One more question if you don't mind.
At present, the new files have a name such as (FileName).pdf.tiff.
I had to revert to saving as tiffs because saving as pdf's did not 'trim' the whole page to any existing crop box, but saving as tiffs, does.
I KNOW I read somewhere that it's possible to set a variable to a file name sans the file extension part, but cannot re-find the article.
Do you, (or anyone), know how to get the file name without the file extension, please?
Regards
Santa
Copy link to clipboard
Copied
Well, I've passed the file name, sans name extension, to the script. It was working fine, saving TIFFS to the correct folder, & correctly named.
BUT, one of the very first problems I struck with one file cropped up, saving to TIFFS can error, and the try trap I was using in the Java Script can't trap it, and neither can Applescript. I solved it in Applescript with a Timeout, then dismissed the resultant two error boxes, and saved the file page as a JPEG.
I know ZILCH about doing the same in Java Script, but on reading came across the 'set timeout()' command, but I don't think I'm using it correctly.
My Applescript is...
on setDrawPages(pp, displayName)
say 2
set Params to ""
set Params to Params & "var dEnd = this.numPages;" & return as text
set Params to Params & "var cFlName = " & "\"" & displayName & "\"" & ";" & return as text
set Params to Params & "var outputPath = " & "\"" & pp & "\";" & return as text
set Params to Params & "var outputPathTwo = outputPath " & " + \"" & "Temporary Acrobat TIFFS" & "\"" & " + " & "\"" & "/" & "\";" & return as text
set Params to Params & "for (i = 0; i < dEnd ; i++ ){;" & return as text
set Params to Params & "var oNewDoc = this.extractPages(i,i);" & return as text
set Params to Params & "var tempText = outputPathTwo " & " + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName;" & return as text
set Params to Params & "myVar = setTimeout(jpegFunction, 5000);" & return as text
set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName + " & "\"" & ".tiff" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.tiff" & "\"" & "});" & return as text
set Params to Params & "clearTimeout(myVar);" & return as text
set Params to Params & "function jpegFunction(oNewDoc.saveAs({cPath: outputPathTwo + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName + " & "\"" & ".jpeg" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.jpeg" & "\"" & "}));" & return as text
set Params to Params & "};" & return as text
tell application "System Events" to display dialog Params as text
say 3
return Params as text
end setDrawPages
The code this produces is in this left screen shot, and a screen shot of the error follows...
 
   
The Console shows...
SyntaxError: missing ) after formal parameters
10:
Which would indicate to me there's a mistake in my code, but I can't see where? I've mucked about for hours, but don't know what the heck I'm doing. Can anyone help point out the error? And am I using the timeout correctly with the function?
Regards
Santa
Copy link to clipboard
Copied
Darn it, I've been chasing shadows.
The setTimeout does NOT work in Acrobat.
I HAVE to save as TIFFS. It's the only format that will trim Acrobat pages to any crop boxes with coding. JPEG is for emergencies.
Does anyone know of a way I can trap that darn error? It's occurring in the actual JavaScript, and I can't use Applescript to deal with it.
Using Java cuts the save time dramatically.
Regards
Santa
Here's my Applescript code.
property PreserveFileName : ""
property theItem : ""
tell application "Finder"
set pp to (POSIX path of (path to desktop) as text)
set thefile to file ((path to desktop as text) & "Problem PDF.pdf") as alias as text
end tell
set my theItem to thefile
try
tell application "Finder" to set my PreserveFileName to name of file (my theItem) as text
on error errmsg number errnum
my sayTheText:("Acrobat error setting file name")
end try
tell application "Finder"
set pp to (POSIX path of (path to desktop) as text)
set p to 101.2
set tempEXT to name extension of file (my theItem)
set p to 101.3
set displayName to (characters 1 through -((count of tempEXT) + 2) of my PreserveFileName) as text
end tell
tell application "Adobe Acrobat"
activate
open thefile as alias
tell document 1
say 1
set theScript to my setDrawPages(pp, displayName)
try
say 4
do script theScript
say 5
on error errmsg
tell application "System Events" to display dialog errmsg
end try
end tell
end tell
on setDrawPages(pp, displayName)
say 2
set Params to ""
set Params to Params & "var dEnd = this.numPages;" & return as text
set Params to Params & "var cFlName = " & "\"" & displayName & "\"" & ";" & return as text
set Params to Params & "var outputPath = " & "\"" & pp & "\";" & return as text
set Params to Params & "var outputPathTwo = outputPath " & " + \"" & "Temporary Acrobat TIFFS" & "\"" & " + " & "\"" & "/" & "\";" & return as text
set Params to Params & "for (i = 0; i < dEnd ; i++ ){;" & return as text
set Params to Params & "var oNewDoc = this.extractPages(i,i);" & return as text
set Params to Params & "var tempText = outputPathTwo " & " + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName;" & return as text
set Params to Params & "try {" & return as text
set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName + " & "\"" & ".tiff" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.tiff" & "\"" & "});" & return as text
set Params to Params & "}" & return
set Params to Params & "catch(err) {" & return
set Params to Params & "app.alert(err);" & return as text
set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName + " & "\"" & ".jpeg" & "\"" & ", cConvID:" & "\"" & "com.adobe.acrobat.jpeg" & "\"" & "});" & return
set Params to Params & "}" & return
set Params to Params & "};" & return as text
tell application "System Events" to display dialog Params as text
say 3
return Params as text
end setDrawPages
Here's the screen capture of the actual code.

Here's the two consecutive errors, which have to be dismissed manually. Everything MUST be automatic. Cropping and reducing resolution don't work. I need to now save as a JPEG, after the error.


Copy link to clipboard
Copied
Even though you found a way to get around this, here is how you would get rid of the ".pdf" in the filename. I've already introduced you to the String.replace() method when we talked about how to get the path to the user's Desktop folder. You would use the same approach here, but replace with an empty string. In this case however, the simple replace may not work because you may have something like filename.pdf.pdf and when you run "filename.pdf.aaa.pdf".replace(".pdf", "") (yes, you can do this in JavaScript), you will end up with "filename.aaa.pdf" - it only replaced one - the first instance - of the ".pdf"s in the name. You very likely want to replace the actual extension, which would be the last instance of ".pdf". This is where you would use regular expressions to identify the actual file extension:
var cFlName = this.documentFileName;
cFlName = cFlName.replace(/.pdf$/i, "");
This will find the ".pdf" which is directly followed by the end of the string, regardless of the case (e.g. .pdf and .PDF are both found, as would be .PdF) and replaces it with the empty string .
As far as your second problem goes, there is nothing you can do in JavaScript to get around this problem: Acrobat's automation interface is not meant for "lights out" operation, it assumes that somebody is sitting in front of the screen and reads error messages and clicks on the "OK" button when such an error occurs. The condition you are encountering is handled by the user interface, and there is no way to - from within JavaScript - do anything about that. You will not be able to simulate a click on the button, or internally tell Acrobat to continue with the script. You are actually not allowed to use Acrobat on a server, or on a server like environment (see the EULA for more information), so that's Adobe's way of making this server use more complicated.
What I would try is to find out what the limitations of the export to TIFF functions are, and therefore what the maximum page size is that you can process this way. You would then have to analyze your PDF document to see if the page you are about to export is larger than that maximum allowed size, and if that's the case, export in a different format (before I would use JPEG, I would try PNG first - JPEG is only good for photographic images).
If this is for a server or server-like environment, you will have to look at other tools.
Copy link to clipboard
Copied
G'day Karl.
Thank you, you're a mine of information.
I'm testing for documents greater than A4 size, which is the maximum I can print. I handle those as PDF's.
However, I'm beginning to think, from the dearth of information on the net, that the maximum TIFF size you can save is set by the maximum size of the paper your printer can handle.
If there's anyone reading this, that has an A3 printer, would you mind running an Acrobat test with A3.
The sort of steps to take are as follows.
Please do the following.
1. Open GraphicConverter.
2. Create a new document with a size of A3 (GC should recognize your printer can print A3).
3. Type something large on the window document.
4. Hit command-p
5. Choose the bottom left pop up PDF, and choose ‘Save as PDF...’. Save it to the desktop. Close it.

6. Drag the new file from the Desktop to the Acrobat icon.
7. Choose the menu item as shown below.

8. Click on ‘Save’
9. If no error window pops up, it means that A3 TIFFS can be saved.
10. Try from 7. again. If you're asked if you want to replace, you've definitely succeeded.
Please post results.
Regards
Santa
Copy link to clipboard
Copied
On Windows I can export A2 and A3 documents.
Copy link to clipboard
Copied
I just run a number of tests on Mac OS X 10.11.6 with Adobe Acrobat Pro DC (version 2015.023.20053). I also can export both A2 and A3 formats - both wide and tall - without any problems. There is no page size limit for TIFF files - there is a file size limit, but unless your files are larger than 2GB (or 4GB depending on what's inside the TIFF file), you should not run into problems with that.
Here are the options that I used (I did change the color space to all three options: color/grayscale/monochrome to rule out any potential problems that only show up for one of the color spaces):

What options are you using? What resolution for the output TIFF file? You do set these output options in Acrobat's Preferences in the "Convert From PDF" section.
Copy link to clipboard
Copied
THANK YOU KARL.
You're a scholar and a gentleman. That worked a treat, and it's FAST.
Thank you to everyone else who pitched in and helped as well.
Regards
Santa
tell application "Finder" to set pp to (POSIX path of (path to desktop) as text)
tell application "Adobe Acrobat"
activate
open ((path to desktop as text) & "Retainer Invoice.pdf") as alias
do shell script ("sleep 1")
tell document 1
set theScript to my setDrawPages(pp)
do script theScript
end tell
end tell
on setDrawPages(pp)
set Params to ""
set Params to Params & "var dEnd = this.numPages;" & return as text
set Params to Params & "var cFlName = this.documentFileName;" & return as text
set Params to Params & "var outputPath = " & "\"" & pp & "\";" & return as text
set Params to Params & "var outputPathTwo = outputPath " & " + \"" & "Temporary Acrobat TIFFS" & "\"" & " + " & "\"" & "/" & "\";" & return as text
set Params to Params & "app.alert(outputPathTwo);" & return as text
set Params to Params & "app.alert(cFlName);" & return as text
set Params to Params & "for (i = 0; i < dEnd ; i++ ){;" & return as text
set Params to Params & "app.alert(i + \" First\");" & return as text
set Params to Params & "var oNewDoc = this.extractPages(i,i);" & return as text
set Params to Params & "app.alert(i + \" Second\");" & return as text
set Params to Params & "var tempText = outputPathTwo " & " + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName" & return as text
set Params to Params & "app.alert(tempText);" & return as text
set Params to Params & "oNewDoc.saveAs({cPath: outputPathTwo + " & "\"" & "Page " & "\"" & " + (i + 1) + " & "\"" & " of " & "\"" & " + cFlName" & "});" & return as text
set Params to Params & "app.alert(tempText);" & return as text
set Params to Params & "};" & return as text
return Params as text
end setDrawPages
Copy link to clipboard
Copied
It isn't clear whether you ever verified the path name syntax or whether you've just stuck with /Desktop... In reply 10 /Desktop was used as an example "Let's say it was..."
 
					
				
				
			
		
 
					
				
				
			
		
Find more inspiration, events, and resources on the new Adobe Community
Explore Now