Monitoring file moved or trashed in Finder

Dear all,

Is there a way to monitor if a file:

  1. has been moved to a different folder, and separately
  2. has been moved to the Trash, and
  3. if has been deleted.
    This is “limited” to PDF files (but it would be interesting also for all kind of files).

This information I will then pass it to a script.

Is that possible?
Would this collapse my CPU usage?

Thanks!

L.

It is possible. It would not collapse your CPU, if you set bigger timeout for on idle handler, 1 hour, for example. Checking if the PDF was moved or deleted is easy. Create list of paths for PDFs and check each 1 hour (for example) if the PDF exists on old path location.

Something as that (should be saved as stay-open applet to work):


Global myPDFs

to initialize()
	set myPDFs to choose file of type {"com.adobe.pdf"} with multiple selections allowed
end initialize

on run
	initialize()
end run

on idle
	tell application "Finder"
		repeat with i from 1 to count of myPDFs
			set nextPDF to item i of myPDFs
			if not (exists nextPDF) then
				display dialog "File " & (nextPDF as string) & ¬
					" is removed!" buttons {"Ignore", "Try find and put back"} ¬
					default button {"Try find and put back"}
			end if
			-- put here code to process removed PDF (search & put back or ignore)
		end repeat
	end tell
	return 5 -- for testing only 5 seconds. For 1 hour should be 3600.
end idle

on quit
	display dialog "Are you sure you want to quit?" buttons {"No", "Yes"}
	if button returned of result is "Yes" then continue quit
end quit

Thanks a lot.
It is a good option.

Although my original intention was to monitor every PDFs on my HD.
I should test if this collapse my Mac.

In any case every time I add/move/delete a file the File-system keep track of that.
I was think if there is any way to hijack/read that info (filtering for PDFs , as an example) and report back to me., of course allowing to continue with add/move/delete task.

The following stay-open applet lists all PDF files on your hard disk at start of applet (1 second on my computer). Then it monitors if some of that PDFs was moved:


use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use script "Metadata Lib" version "2.0.1"
global myPDFs

to initialize()
	set theFolder to POSIX path of (path to startup disk)
	set myPDFs to perform search in folders {theFolder} ¬
		predicate string "kMDItemContentTypeTree CONTAINS %@" search arguments {"com.adobe.pdf"}
end initialize

on run
	initialize()
end run

on idle
	tell application "Finder"
		repeat with i from 1 to count of myPDFs
			set nextPDF to (item i of myPDFs) as POSIX file
			if not (exists nextPDF) then
				display dialog "File " & (nextPDF as string) & ¬
					" is moved!" buttons {"Ignore", "Try find and put back"} ¬
					default button {"Try find and put back"}
			end if
			-- put here code to process removed PDF (search & put back or ignore)
		end repeat
	end tell
	return 5 -- for testing only 5 seconds. For 1 hour should be 3600.
end idle

on quit
	display dialog "Are you sure you want to quit?" buttons {"No", "Yes"}
	if button returned of result is "Yes" then continue quit
end quit

Nice and elegant! Thanks again!

The only problem I see is that if multiple files have the same names and get moved, it will be impossible to know who is who, unless we can use a kind if file ID (if such thing exists …).
I check with “mdls” but unfortunately it seems that there is no any file identifier in the metadata.

Is impossible existing of 2 same files on 1 path, so all files have initially different paths.