Folder action to log added files & the app which added them

Hi,
This is a test form of a folder action I use to write the date of adding files to a folder and the full POSIX path of those files to a log file:

on adding folder items to this_folder after receiving added_items
	set DateTime to ("Date stamp = " & (do shell script "date +%d.%m.%y_%H:%M:%S"))
	tell application "Finder"
		set the folder_name to the name of this_folder
	end tell
	set the item_count to the number of items in the added_items
	set log_file to (path to desktop as text) & "Added_items_log.txt"
	set added_items_list to {}
	repeat with i in added_items
		copy POSIX path of i as text to the end of added_items_list
	end repeat
	tell current application
		try
			set openfile to open for access file log_file with write permission
			write DateTime & return to openfile starting at eof
			repeat with i in added_items_list
				write i & return to openfile starting at eof
			end repeat
			write "--" & return to openfile starting at eof
			close access openfile
		on error errMsg
			try
				close access file log_file
				display dialog "Error while writing items added to folder:" & return & POSIX path of this_folder & return & return & errMsg with title POSIX path of this_folder buttons {"OK"} with icon stop
			end try
		end try
	end tell
end adding folder items to

Is there any way to get and log the app name which added the files as well?

Hello, maybe you can use this, the app must then be the one that is frontmost, I think you will have problems with background apps. as there isn’t written anywhere in the files where it is coming from, and a process doesn’t open a file, so you can’t use lsofen either.


tell application "System Events"
	set nm to name of first application process whose frontmost is true and visible is true
end tell

Thank you.

Your solution does work for one the folders I’m watching. The files there, can be the output of one of four apps, open and frontmost at the time.

On the other hand, I’m disappointed about background process apps, because I intended to log the activity in all LaunchAgents, LaunchDaemons, StartupItems. This may be naive but, I did expect that there is a forensic way to establish the origin of a file. Is there really no way? Maybe, with some special helper app?

Cheers, Chris.

Hello.

Your wishes for a centralized log imposes certain problems, due to the fact that many apps may want to write to the same file at the same time, maybe overwriting something just written into it.

I suggest you play with the handler below, and implement that one, writing to the same file, which is a system log file, so I guess the appending will be good at all times.

Then you also may go into the console, and look at the log, which may be practical. :slight_smile:


my logit(appname & "wrote " & filename & " to " &destfolder,"dropfile")

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

The example above could write “2013-02-27 14:50:00 filecopier wrote filetocopy to foldertoreceive” to ~/Library/logs/dropfile.log (Which you also would have found at the sidebar in Console.app, after you had doubleclicked it in Finder for the first time.