Hi all
Cobbled together a script which worked in testing, but then → stopped working, could not understand what went wrong, please help
It creates a file with correct name but not encrypted.
Tried with password protected pdf → creates empty PDF with correct name
it wont password protect !
My Try → to select pdf file ( once this works, wud try to extend to multiple selection) in Finder,
input dialog to set password, and create a password protected file
with suffix encrypted in same directory.
I wud like to use the password protection to prevent file opening
use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4"
use scripting additions
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
property NSDictionary : a reference to NSDictionary of this
property QuartzFilter : a reference to QuartzFilter of this
# set the password. "pw"
set theReturnedItems to (display dialog "What is the Password?" default answer "Example" buttons {"Quit", "OK"} default button 2)
if the button returned of the result is "Quit" then
return
end if
set pw to the text returned of theReturnedItems
# Select PDF File
# Let Finder Selection be the input
-- https://macscripter.net/viewtopic.php?id=42583. -- Set Finder selection as posix
tell application "Finder" to set sel to selection
set thePDF to POSIX path of (item 1 of sel as alias)
if thePDF does not end with ".pdf" then return
set aURL to (NSURL's fileURLWithPath:thePDF)
set origPDFdoc to PDFDocument's alloc()'s initWithURL:aURL
# construct output file names -- Viking OSX.
set ws_file to NSString's stringWithString:thePDF
set ws_ext to ws_file's pathExtension()
set ws_base to ws_file's stringByDeletingPathExtension()
set bPOSIX to ((ws_base's stringByAppendingString:"_enCrypted")'s stringByAppendingPathExtension:ws_ext)
# Create options for the document
-- https://forum.latenightsw.com/t/scripting-pdf-security-options/739
-- Template from https://macscripter.net/viewtopic.php?id=45941 #2
set aDict to NSDictionary's dictionaryWithObjects:{pw} forKeys:{current application's kCGPDFContextUserPassword}
origPDFdoc's writeToFile:(bPOSIX) withOptions:(aDict)
I was interested in your script. Although I had to tinker a bit with your mistakes, I don’t regret it:
use framework "Foundation"
use framework "CoreFoundation"
use framework "PDFKit"
use AppleScript version "2.4"
use scripting additions
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
property NSDictionary : a reference to NSDictionary of this
property QuartzFilter : a reference to QuartzFilter of this
property kCGPDFContextUserPassword : a reference to "kCGPDFContextUserPassword"
property kCGPDFContextOwnerPassword : a reference to "kCGPDFContextOwnerPassword"
property CFString : a reference to CFString of this
property kCGPDFContextAllowsCopying : a reference to kCGPDFContextAllowsCopying of this
-- set the password. "pw"
set theReturnedItems to (display dialog "What is the Password?" default answer "Example" buttons {"Quit", "OK"} default button 2)
if the button returned of the result is "Quit" then return
set pw to (the text returned of theReturnedItems) as «class utf8»
set pw to (NSString's stringWithString:pw)
tell application "Finder" to set theAliases to (get selection as alias list)
repeat with anAlias in theAliases
if (anAlias as text) ends with ".pdf" then
set thePDFpath to POSIX path of anAlias
set aURL to (NSURL's fileURLWithPath:thePDFpath)
set origPDFdoc to (PDFDocument's alloc()'s initWithURL:aURL)
-- construct output file names -- Viking OSX.
set ws_file to (NSString's stringWithString:thePDFpath)
set ws_ext to ws_file's pathExtension()
set ws_base to ws_file's stringByDeletingPathExtension()
set bPOSIX to ((ws_base's stringByAppendingString:"_enCrypted")'s stringByAppendingPathExtension:ws_ext)
-- Create options for the document
set aDict to (NSDictionary's dictionaryWithObjects:{CFString's noCopy, pw, pw} forKeys:{kCGPDFContextAllowsCopying, kCGPDFContextUserPassword, kCGPDFContextOwnerPassword})
-- save password protected file
(origPDFdoc's writeToFile:(bPOSIX) withOptions:(aDict))
end if
end repeat
Thanks @KniazidisR for ur help
This works perfectly
I have done another cobbled up script, heavily borrowed, hope it helps somebody
# URL: https://www.macscripter.net/viewtopic.php?id=47872
# URL: https://macscripter.net/viewtopic.php?id=25916
# Appl: Quartz Filter Script
# Tags: @PDF @QuartzFilters @Compress @Reduce
use framework "Foundation"
use framework "Quartz"
use framework "QuartzCore"
use AppleScript version "2.4"
use scripting additions
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
property NSDictionary : a reference to NSDictionary of this
property QuartzFilter : a reference to QuartzFilter of this
# Select the Filter
-- This Path for default Filters of ColorSync app (System Folder) https://www.macscripter.net/viewtopic.php?id=47872 (peavine)
-- set filterFolder to ((path to system folder as text) & "Library:Filters:") as alias
-- This Path for "User" Created Filters of ColorSync app
set filterFolder to ((path to home folder as text) & "Library:Filters:") as alias
tell application "Finder" to set filterFiles to the name of every file in filterFolder
repeat with aFile in filterFiles
set contents of aFile to text 1 thru -9 of aFile
end repeat
choose from list filterFiles with prompt "Select a filter:" with title "Quartz Filter" default items (item 1 of filterFiles)
set selectedFilter to result
if selectedFilter = false then
error number -128
else
set aFilterURL to (filterFolder as text) & (item 1 of selectedFilter) & ".qfilter" as alias
-- set filterFile to POSIX path of filterFile
end if
tell application "Finder" to set theAliases to (get selection as alias list) -- KniazidisR
repeat with anAlias in theAliases
if (anAlias as text) ends with ".pdf" then
set thePDFpath to POSIX path of anAlias
set aURL to (NSURL's fileURLWithPath:thePDFpath)
set origPDFdoc to (PDFDocument's alloc()'s initWithURL:aURL)
-- construct output file names -- Viking OSX.
set ws_file to (NSString's stringWithString:thePDFpath)
set ws_ext to ws_file's pathExtension()
set ws_base to ws_file's stringByDeletingPathExtension()
set bPOSIX to ((ws_base's stringByAppendingString:"_Filtered")'s stringByAppendingPathExtension:ws_ext)
-- Create options for the document -- Shane Stanley
set aQFilter to (QuartzFilter's quartzFilterWithURL:aFilterURL)
set aDict to (NSDictionary's dictionaryWithObjects:{aQFilter} forKeys:{"QuartzFilter"})
-- save Filtered file
(origPDFdoc's writeToFile:(bPOSIX) withOptions:(aDict))
end if
end repeat
You coerce filterFolder reference firstly from alias to text, then back from text to alias. You can work with text (HFS path), so coercion to alias you can omit.
As I understand, using repeat loop, you cut filter files extensions (“.qfilter”). You can cut them without repeat loop.
Your if statement can be shortened.
Your aFilterURL should be NSURL.
Some parentheses can be omitted.
# URL: https://www.macscripter.net/viewtopic.php?id=47872
# URL: https://macscripter.net/viewtopic.php?id=25916
# Appl: Quartz Filter Script
# Tags: @PDF @QuartzFilters @Compress @Reduce
use framework "Foundation"
use framework "Quartz"
use framework "QuartzCore"
use AppleScript version "2.4"
use scripting additions
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
property NSDictionary : a reference to NSDictionary of this
property QuartzFilter : a reference to QuartzFilter of this
# Select the Filter
-- This Path for default Filters of ColorSync app (System Folder) https://www.macscripter.net/viewtopic.php?id=47872 (peavine)
-- set filterFolder to ((path to system folder as text) & "Library:Filters:") as alias
-- This Path for "User" Created Filters of ColorSync app
set filterFolder to (path to home folder as text) & "Library:Filters:"
tell application "Finder" to set filterFiles to (name of files of folder filterFolder) as text
set AppleScript's text item delimiters to ".qfilter"
set filterFiles to text items of filterFiles
set AppleScript's text item delimiters to ""
choose from list filterFiles with prompt "Select a filter:" with title "Quartz Filter" default items (item 1 of filterFiles)
set selectedFilter to result
if selectedFilter = false then error number -128
set aFilterURL to NSURL's fileURLWithPath:(POSIX path of (filterFolder & item 1 of selectedFilter & ".qfilter"))
tell application "Finder" to set theAliases to (get selection as alias list) -- KniazidisR
repeat with anAlias in theAliases
if (anAlias as text) ends with ".pdf" then
set thePDFpath to POSIX path of anAlias
set aURL to (NSURL's fileURLWithPath:thePDFpath)
set origPDFdoc to (PDFDocument's alloc()'s initWithURL:aURL)
-- construct output file names -- Viking OSX.
set ws_file to (NSString's stringWithString:thePDFpath)
set ws_ext to ws_file's pathExtension()
set ws_base to ws_file's stringByDeletingPathExtension()
set bPOSIX to ((ws_base's stringByAppendingString:"_Filtered")'s stringByAppendingPathExtension:ws_ext)
-- Create options for the document -- Shane Stanley
set aQFilter to (QuartzFilter's quartzFilterWithURL:aFilterURL)
set aDict to (NSDictionary's dictionaryWithObjects:{aQFilter} forKeys:{"QuartzFilter"})
-- save Filtered file
(origPDFdoc's writeToFile:bPOSIX withOptions:aDict)
end if
end repeat
This one to get encryption info of PDF
Script improvement suggestions are welcome, again cut & paste
Cheers
-- Original 2015-11-02 13:48:32 +0900 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
tell application "Finder"
set thePath to (get selection) as alias
end tell
set aPath to POSIX path of thePath
set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of aPath)
set origPDFdoc to current application's PDFDocument's alloc()'s initWithURL:aURL
set aRes to my retProtectedPDFPermissions(origPDFdoc, aPath)
on retProtectedPDFPermissions(aPDFdoc, aPath)
set anEncF to aPDFdoc's isEncrypted()
if anEncF = false then return display dialog "File : " & name of (info for aPath) & return & "------------------" & return & "Not Encrypted" -- missing value
set passLocked to aPDFdoc's unlockWithPassword:""
set cpPerm to aPDFdoc's allowsCopying() as boolean
set prPerm to aPDFdoc's allowsPrinting() as boolean
set cmPerm to aPDFdoc's allowsCommenting() as boolean
set caPerm to aPDFdoc's allowsContentAccessibility() as boolean
set daPerm to aPDFdoc's allowsDocumentAssembly() as boolean
set dcPerm to aPDFdoc's allowsDocumentChanges() as boolean
set ffPerm to aPDFdoc's allowsFormFieldEntry() as boolean
set myList to {openPermission:passLocked, copyPermission:cpPerm, printPermission:prPerm, commentPermission:cmPerm, contentAccessPermission:caPerm, assemblyPermission:daPerm, docchangePermission:dcPerm, formPermission:ffPerm}
set PerList to Rec2UserKeyValues(myList) of me
display dialog "File : " & name of (info for aPath) & return & "------------------------------" & return & PerList as string
end retProtectedPDFPermissions
on Rec2UserKeyValues(recAny)
-- https://macscripter.net/viewtopic.php?pid=143257
-- USE THE CLIPBOARD TO MAKE THE RECORD KEYS LEGIBLE
set the clipboard to recAny
set recLegible to (the clipboard as record)
set lngPairs to count of (recAny as list)
if lngPairs < 1 then return {}
-- COLLECT ANY USER-DEFINED KEY-VALUE PAIRS
set lstKeyValue to {}
try
set lstUser to list of recLegible
on error
display dialog (do shell script "osascript -e 'the clipboard as record'") buttons "OK" default button 1 with title "Contents of record"
return {}
end try
repeat with i from 1 to (length of lstUser) - 1 by 2
set end of lstKeyValue to {item i of lstUser, " : ", item (i + 1) of lstUser} & return & return
end repeat
-- display dialog lstKeyValue as string
return lstKeyValue
end Rec2UserKeyValues
Thanks. Again.
Well, that’s not a bad idea. But the task of the script is simple, so it’s better to shorten it:
-- Get PDF's permissions as record
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
tell application "Finder" to tell (item 1 of (get selection)) to set {aPDF, aName} to {it, name of it}
set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of (aPDF as alias))
set origPDFdoc to current application's PDFDocument's alloc()'s initWithURL:aURL
set aRes to my retProtectedPDFPermissions(origPDFdoc, aName)
on retProtectedPDFPermissions(aPDFdoc, aName)
if false = aPDFdoc's isEncrypted() then ¬
return display dialog "File : " & aName & return & "------------------" & return & "Not Encrypted"
tell aPDFdoc to set aRecord to {openPermission:(its unlockWithPassword:""), copyPermission:its allowsCopying(), printPermission:its allowsPrinting(), commentPermission:its allowsCommenting(), contentAccessPermission:its allowsContentAccessibility(), assemblyPermission:its allowsDocumentAssembly(), docchangePermission:its allowsDocumentChanges(), formPermission:its allowsFormFieldEntry()}
set RR to return & return
set PerList to "File : " & aName & RR
set PerList to PerList & "Opening Permissions: " & aRecord's openPermission & RR
set PerList to PerList & "Copying Permissions: " & aRecord's copyPermission & RR
set PerList to PerList & "Printing Permissions: " & aRecord's printPermission & RR
set PerList to PerList & "Commenting Permissions: " & aRecord's commentPermission & RR
set PerList to PerList & "Content Access Permissions: " & aRecord's contentAccessPermission & RR
set PerList to PerList & "Assemblying Permissions: " & aRecord's assemblyPermission & RR
set PerList to PerList & "Document Changing Permissions: " & aRecord's docchangePermission & RR
display dialog PerList with title "File : " & aName
return aRecord
end retProtectedPDFPermissions