The best way to lock PDF files

Hello,
I have found myself with the problem of preventing PDF from being modified.
I thought about locking them in DTPO.
There are so few PDFs that I need to modify once they have been created that I don’t mind unlocking one once in a while. In my workflow, PDFs are the final version — digital masters if I may say — of old archives that are fragile and not accessible on premise, so they must be maintained with even more care than the originals that will degrade over time.
However, the Locked attribute inside of DTPO only seems to prevent modification inside the default viewer which I find less user friendly and slower than Preview.
On the other hand, Preview makes it very easy to unwillingly modify a PDF (double clicking to add text anywhere or worse — and frequent — deleting a page when you think you delete a character in the search bar).
The only lock that would prevent modification in Preview would be the one in the Finder properties window, but:
— I would have to apply it manually for each new PDF (or use some kind of script that would use chflags uchg filename);
— it is meant for temporary files rather than for read-only files (for instance attachments opened in e-mails are locked);
— it is unknown whether DTPO would appreciate working with locked files when you try to move them, tag them or sync them (would they retain this property)

Therefore my question to the DEVONthink team is:
What do you think is the best way to protect my PDFs?
Thanks in advance!

Attached is a script that will toggle the locked status of selected files. It applies chflags for use with other apps but also applies a DEVONthink lock state, as DEVONthink does not show a lock for files with a uchg flag set. This is for your visual convenience.

Note that this will apply to indexed files as well as imported ones. This is not necessarily a bad thing but it may be something to consider.

The script checks the state and locks unlocked files and vice versa. Put it in your desired folder in ~/Library/Application Support/DEVONthink Pro 2/Scripts. Note: If you add it to the Toolbar folder, you can relaunch DEVONthink, right-click the Toolbar and add the script via “Customize Toolbar…"
DT FileLock Toggle RO.zip (1.39 KB)

This is of great help, thanks a lot! I tried it and it works great, even with a list of selected files.

However, I cannot open the scpt script with Script Editor, because apparently it is compiled (“read-only”). Therefore I cannot modify it to adapt it to my needs, for instance if I want a separate toolbar button to lock and to unlock instead of a toggle, in order to set to the same state a list of selected files. Would you be kind enough to post the uncompiled scpt?

Also, is there a way to automatically call the script on each import or creation of a PDF file?

Best regards

Actually, while the script works perfectly for the central computer that acts as the master database, the double lock is not retained after synchronization on the two “client” computers on which we work: the DEVONthink lock is synchronized of course, but the physical files are not locked OS X-wise, which is actually logical because DT is not aware he should do that. So now I suppose I need a script to automatically run after synchronization that gets all the DT-locked files and locks them in OS X if they’re not locked, and all the DT-unlocked files and unlock them if they are locked. Could you please help?

Hello Jim,
Could you post the source of your script?
I will then adapt it to OS-lock DT-locked files on a remote computer after a DT sync.
For now my PDFs are only protected on the main computer that serves as the sync server, but not on the laptops where my team works.
Thanks is advance!
Edouard

I am generally hesitant to hand out code that run some UNIX commands like chflags (no matter how simple it may appear).
Note the disclaimer.

-- DT FileLock Toggle
-- Copyright 2016 Jim Neumann (BLUEFROG) / DEVONtechnologies, LLC
-- Toggles the lock state of a file at filesystem and DEVONthink level
-- NOTE: Neither DEVONtechnologies, LLC nor its agents can be held responsible for adverse effects from running modified versions of this script.
-- Remember: In UNIX, there is NO FORGIVENESS.

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set fpath to path of thisRecord
		if fpath ≠ "" then
			set lockState to do shell script "ls -lO " & (quoted form of fpath) & " | awk '{print $5}'"
			if lockState = "-" then
				do shell script "chflags uchg " & (quoted form of fpath)
				set locking of thisRecord to true
			else if lockState = "uchg" then
				do shell script "chflags nouchg " & (quoted form of fpath)
				set locking of thisRecord to false
			end if
		end if
	end repeat
end tell

Thanks a lot Jim!

Well here are the scripts modified for my usage scenario. As you can see, the risk was quite minimal. :wink:

Script to “double-lock” (DT lock & OS X lock)

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set fpath to path of thisRecord
		if fpath ≠ "" then
			do shell script "chflags uchg " & (quoted form of fpath)
			set locking of thisRecord to true
		end if
	end repeat
end tell

Script to “double-unlock” (DT unlock & OS X unlock)

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set fpath to path of thisRecord
		if fpath ≠ "" then
			do shell script "chflags nouchg " & (quoted form of fpath)
			set locking of thisRecord to false
		end if
	end repeat
end tell

Script to sync the OS X lock depending on the DT lock status (for instance to run regularly after a sync on all elements of a remote DT database)

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set fpath to path of thisRecord
		if fpath ≠ "" then
			set dtLockState to locking of thisRecord
			set osxLockState to do shell script "ls -lO " & (quoted form of fpath) & " | awk '{print $5}'"
			if dtLockState = true and osxLockState = "-" then
				do shell script "chflags uchg " & (quoted form of fpath)
			else if dtLockState = false and osxLockState = "uchg" then
				do shell script "chflags nouchg " & (quoted form of fpath)
			end if
		end if
	end repeat
end tell

I added them to my toolbar with nice icons. I modified standard icons to fit the features. For those wondering, I use iConvert Icons to convert transparent PNG to icns files, that I drag and drop to the icon in the scpt file’s properties window in Finder. I’m attaching them to the post (lock-add and lock-remove are obviously for lock and unlock, lock-switch is for Jim’s “switch” script and lock-sync is for my DT to OS X “lock sync” script).

I hope this can help other users with the same multiple users needs as me, until a read-only or permissions system is added.

Thanks again for your help on the script Jim!
Icons.zip (34.4 KB)

No problem, and thanks for the icons. They’re very nice. :smiley:

I should add a word of caution, that makes me wonder if I should use OS X locking at all.
OS X locked files will not be renamed on the hard drive when the file is renamed in DEVONthink. Everything still works in DEVONthink, even the sync, but the physical file name will not be in sync. Furthermore, if a file is OS X locked on one side of the sync, and not OS X locked on the other side, after renaming a file and syncing, both files will end with the same name in DEVONthink, but a different physical name… However deletion of OS X locked files work in DT, even during as a consequence of a sync.

As I said… 8)

I know I know, but let’s be honest, my caution is not about the danger of Unix, it’s about the unpredictability of the OS X lock (that can be set manually in the Finder) interacting with the internal storage of DEVONthink. Even though all my test cases have proven that it works well, even with a sync, I prefer to think that DEVONthink will be able to automatically rename the underlying files the way it sees fit rather than to take any risk of discrepancy.
Now I’m left to hope for a permissions update to DEVONthink.
In the meanwhile, the best piece of advice I could give to the guest user was to use the space bar to view documents without risking. (The internal viewer does not handle well zooming with the trackpad).
Best regards,
Edouard