pdf security script not working as expected

Hi
I am using pdftk_server-2.02 from pdflabs.com to turn off security in pdf’s that I have created, this code will be part of a larger work flow in my prepress area.

The idea is to have the code select the secured pdf, disable security, generate a new pdf in the same folder called “_unsecured.pdf”

What I get is a pdf that acrobat doesn’t recognize and script editor just keeps freezing up, I have to quit it and restart.

any ideas how to make this code work correctly please?

set theApp to "/opt/pdflabs/pdftk/bin/pdftk"
set theFile to "/Users/Budgie/Desktop/163648/163648-PF.pdf" --as POSIX file
set thePath to "/Users/Budgie/Desktop/163648/163648_unsecured.pdf/" --as POSIX file

do shell script (quoted form of theApp & space & quoted form of POSIX path of theFile & space & "input_pw" & space & "XXXXX" & " output " & thePath)

You needn’t use those third-party tools to do this, as AppleScriptObjC can do it for you. Assuming that, by “security”, you meant that you password-protected the PDF file, then unlocking the PDF can be done like this:

use framework "Quartz"
use framework "Foundation"

property this : a reference to current application

property NSString : a reference to NSString of this
property NSURL : a reference to NSURL of this
property PDFDocument : a reference to PDFDocument of this

set pw to "mypassword"

set PDFFilePath to "~/Google Drive/Scripting/AppleScriptLanguageGuide.pdf"
set PDFWritePath to "~/Google Drive/Scripting/AppleScriptLanguageGuide_unlocked.pdf"


tell (PDFDocument's alloc()'s initWithURL:fileURLWithPath_(PDFFilePath))
		if its isLocked() as boolean then unlockWithPassword_(pw)
		writeToURL_(my fileURLWithPath:PDFWritePath)
end tell

on fileURLWithPath:(filepath as text)
		local filepath
		
		NSURL's fileURLWithPath:((NSString's ¬
				stringWithString:filepath)'s ¬
				stringByStandardizingPath())
end fileURLWithPath:

If, however, the PDF has been encrypted, then that would be tougher.

AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.13

Hi CK
This is so much more in keeping with my not wanting to use 3rd party tools, as I would have to install on all of my prepress mac’s, not really what I wanted.

This how ever will fit perfectly into the rest of my work flow, and works like a champ, .18 in script debugger.

Very much appreciated, can you point me to where you got the info for the isLocked, unlockWithPassword info please.

again cheers

You and I are similar in this matter, as I very much dislike using third-party tools, scripting additions, or other people’s script libraries. When Mojave was released and everyone’s scripts broke because they had been reliant on Samitage scripting additions, or what have you, I admittedly felt a bit quietly smug about not having to go through that ordeal.

Of course. I actually rely almost solely on Apple’s onine documentation, which I find, on the whole, quite informative and useful. In this particular situation, I got the necessary information from these pages: isLocked() and unlockWithPassword:, which both come under the section about Read Operations.

Also, if it’s useful to know, on the page that handles the main Objective-C class, PDFDocument, it had a link that took me to constants pertaining to PDFDocumentWriteOptions, which I had to look up first in order to password protect my file initially, as I don’t have Adobe Acrobat and needed to create a password-protected PDF in order to then check my script would successfully unlock it. It was a lot simpler than I thought it would be, as there are only two constants defined—one for the user password, and one for the ownership password—and those two constants serve as dictionary keys to which you assign values that become the passwords with which to lock the PDF file, using the appropriate method:

set PDF to PDFDocument's alloc()'s initWithURL:fileURLWithPath_(PDFFilePath)
PDF's writeToURL:fileURLWithPath_(PDFWritePath) ¬
				withOptions:{PDFDocumentUserPasswordOption:("mypassword") ¬
								  , PDFDocumentOwnerPasswordOption:"adm1npassword"}

thank you for the information CK.

The adding of security looks interesting, at the moment i’m calling a .jsx script from my code to add security to the PDF at the time of export from Indesign CC, this works really well, I found this at the time to be the most effective way of what I wanted to achieve, but now after seeing the security code you added, I think I will see what I can achieve to have that handled with out using the .jsx code as it’s really unfamiliar to me, but still adding my security needs upon export from Indesign.

heres the discussion:https://forums.adobe.com/thread/2595258

  function isOurFolder(arg) 
   { 

         if(arg.name.match(/^Q.*?/)) 

              return true 

         else 

              return false 

    } 

    var myFolder = Folder.desktop.getFiles(isOurFolder) 

    var myDoc = app.activeDocument 

    var docName = myDoc.name.replace(/.indd$/i,"") 

    var _dest = myFolder[0].fsName 

    try 

    { 
       var unusedSwatches = myDoc.unusedSwatches 

         for(var i = 0; i < unusedSwatches.length; i++) 

              unusedSwatches[i].remove() 

    } 
catch(e){} 

  var screenSettings = app.pdfExportPresets.itemByName("USE THIS TO DISTILL-1") 

   
var mySettings = app.pdfExportPreferences;
with (mySettings) {
pageRange = PageRange.ALL_PAGES;
viewPDF = true
useSecurity = true;
viewPDF = true;
disallowPrinting = false;
disallowCopying = true;
disallowChanging = true;
disallowDocumentAssembly = true;
disallowFormFillIn = true;
changeSecurityPassword = "XXXXX";
}
    
app.activeDocument.exportFile(ExportFormat.PDF_TYPE, _dest + "/" + docName + ".pdf", false, screenSettings) 

    myDoc.close(SaveOptions.YES)

Hi
Ive been using the code from CK to check to see if a PDF is locked, but for some reason, of which I don’t no why the script now stops at

if its isLocked() as boolean 

and returns an error [format]Can’t get isLocked of missing value.[/format] any ideas why this may be happening?

Hi.

If the PDF document is ‘missing value’ then it’s not being initialised. Try expanding the tildes in the paths to the appropriate values.

thanks Nigel, that did the trick for me, appreciated