Is it possible to do this process:
- Scan a folder and all subfolders;
- Check only pdf files;
- If (and only if) the file has not the password;
→ set password to avoid modify of the file.
The saved file must be named as the original file.
Is it possible to do this process:
The saved file must be named as the original file.
For these steps you can use the AppleScript Toolbox.osax (see my signature link). For setting the password of an PDF file you need a third party tool/app (like pdftk). Here is an example code that helps you find PDF files with no encryption using AppleScript Toolbox.osax.
replace ‘-- insert code here’ with the actual code that sets the password.
set theFolder to choose folder
-- AppleScript Toolbox.osax contains the 'AST query metadata' command
-- which is unsafe to use for this tasks. List each file and check
-- its kMDItemSecurityMethod meta if exist.
set pathToPDFFiles to AST list folder theFolder matching regex "\\.pdf$" with listing subfolders
repeat with filePath in pathToPDFFiles
-- concatenation is needed for adding a default value if not exist
set fileInfo to (AST metadata for file filePath) & {kMDItemSecurityMethod:"none"}
if kMDItemSecurityMethod of fileInfo is in {"", "none"} then
-- file 'contents of filePath' is not encrypted
-- **insert code here**
end if
end repeat
Hi,
but what is “unsafe”? there are any risks?
No, there is no security risk at all. Only a slight chance that not all non-encrypted PDF files are listed when you use the AST query metadata command. AST query metadata will search for files using spotlight, the command completely relies on the reliability of spotlight. The code above can be replaced with:
set theFolder to choose folder
AST query metadata "kMDItemSecurityMethod != 'Password Encrypted' && kMDItemFSName = '*.pdf'" only in folders theFolder
[b]Thanks to all, this is the code that works well…
[/b]
--to be also installed: /Library/ScriptingAdditions/AppleScript Toolbox.osax
--to be purchased PDFKey, installed, and installed also command line from menu
--add also as job
beep
set ap_sup to "/Users/YOURDEFAULTPATH/"
set defaultpassword to "YOURPASSWORD"
set theFolder to POSIX path of ap_sup
set PasswordYN to display dialog "Do you want to set a specific folder and password? (Choose in 10 seconds)" buttons {"Cancel", "OK"} default button 1 giving up after 10
if button returned of result = "OK" then
set theFolder to choose folder
set display_text to "Please enter your password:"
repeat
considering case
set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
if (final_pass = init_pass) then
exit repeat
else
set display_text to "Mismatching passwords, please try again"
end if
end considering
end repeat
set defaultpassword to final_pass
else
end if
set pathToPDFFiles to AST list folder theFolder matching regex "\\.pdf$" with listing subfolders
--set pathToPDFFiles to «event ASTlstdr» theFolder with «class |rcv» given «class |rex»:"\\.pdf$"
--set pathToPDFFiles to AST list folder theFolder matching regex "\\.pdf$" with listing subfolders
--AST query metadata "kMDItemSecurityMethod != 'Password Encrypted' && kMDItemFSName = '*.pdf'" only in folders theFolder
--display dialog defaultpassword
repeat with filePath in pathToPDFFiles
set fileInfo to (AST metadata for file filePath) & {kMDItemSecurityMethod:"none"}
if kMDItemSecurityMethod of fileInfo is in {"", "none"} then
--beep
set zpdftk to "/usr/local/bin/pdfkey"
set macPath to quoted form of POSIX path of filePath
set script1 to "dirname " & macPath
set origDir to quoted form of (do shell script script1)
set comandopdf to zpdftk & " lock -o " & defaultpassword & " --allow-print -d " & origDir & " " & macPath
do shell script comandopdf
end if
end repeat