Folder Action File Detection Help

Hi there,

Just wanna say thanks in advance to anyone who can help me out. I have a simple script that I use as a folder action script. Im fairly new to applescript. This script works for single files but if I copy multiple files to the folder it mostly only recognizes and processes the first file. Is there anyway I can modify this to make it wait and take all files into consideration.

Code which I’ve made slightly skinnier to simply reading for you guys. The code within the tells definitely works and does not affect file detection.


on adding folder items to watchedFolder after receiving addedFiles
	repeat with i from 1 to number of items in addedFiles
		set currentFile to (item i of addedFiles)
		try
			tell application "Finder"
				if name extension of currentFile is "randomFileExtension" then
					--Code	
				end if	
			end tell
		end try
		try
			tell application "RandomApp"	
				--Proccess Final File.
			end tell
		end try
	end repeat
end adding folder items to

Hi. Welcome to MacScripter.

Whatever’s going wrong is happening in the code you haven’t posted. Since it’s all in ‘try’ statements, the error(s) are simply causing everything after them in those statements to be skipped and you’re getting no feedback about what the problem(s) are. Try adding ‘on error’ statements to the ‘try’ blocks. They won’t fix the script, but should give a handle on what’s going wrong.


on adding folder items to watchedFolder after receiving addedFiles
	repeat with i from 1 to (count addedFiles)
		set currentFile to (item i of addedFiles)
		try
			tell application "Finder"
				if name extension of currentFile is "randomFileExtension" then
					--Code	
				end if	
			end tell
		on error errMsg
			display dialog "First 'try' block:" & return & errMsg
		end try
		try
			tell application "RandomApp"	
				--Proccess Final File.
			end tell
		on error errMsg
			display dialog "Second 'try' block:" & return & errMsg
		end try
	end repeat
end adding folder items to

Ok I did get an error but to demonstrate I’m going give the full code:


on adding folder items to watchedFolder after receiving addedFiles
	
	repeat with i from 1 to number of items in addedFiles
		
		set currentFile to (item i of addedFiles)
		try
			tell application "Finder"
				
				if name extension of currentFile is "avi" then
					
					set originalFilePath to POSIX path of currentFile
					set quotedOriginalFilePath to quoted form of originalFilePath
					set newFilePath to (characters 1 through -4 of originalFilePath as string) & "mp4"
					set quotedNewFilePath to quoted form of newFilePath
					set shellCommand to "rm -f " & quotedOriginalFilePath
					do shell script shellCommand
					set finalFilePath to ((POSIX file newFilePath) as string)
					
				end if
				
			end tell
		on error errorMessage
			display dialog "First 'Try' block:" & return & errorMessage
		end try
		try
			tell application "TestApp"
				
				process finalFilePath
				
			end tell
		on error errorMessage
			display dialog "Second 'Try' block:" & return & errorMessage
		end try
	end repeat
	
end adding folder items to

The error I get is in the second try block and it happens on the second file. The error is

Second ‘Try’ block:
The variable finalFilePath is not defined.

Thanks for the help

Trying your code out of context, I get an error with the line which sets finalFilePath. You’ve got it in a Finder ‘tell’ statement and the Finder apparently doesn’t understand the ‘POSIX file’ specifier.

But in fact, you don’t need either the Finder or ‘POSIX file’:


on adding folder items to watchedFolder after receiving addedFiles
	
	repeat with i from 1 to number of items in addedFiles
		
		set currentFile to (item i of addedFiles)
		try
			set originalFilePath to POSIX path of currentFile
			
			if (originalFilePath ends with ".avi") then
				
				set quotedOriginalFilePath to quoted form of originalFilePath
				set shellCommand to "rm -f " & quotedOriginalFilePath
				do shell script shellCommand
				set finalFilePath to text 1 thru -4 of (currentFile as text) & "mp4"
				
			end if
			
		on error errorMessage
			display dialog "First 'Try' block:" & return & errorMessage
		end try
		try
			tell application "TestApp"
			
				process finalFilePath
			
			end tell
		on error errorMessage
			display dialog "Second 'Try' block:" & return & errorMessage
		end try
	end repeat
	
end adding folder items to

Note though that your “TestApp” code needs to be inside the ‘if’ block, otherwise when you add an item whose name doesn’t end with “.avi”, the application will still try to process finalFilePath, which either won’t have been defined or will have a value from a previous item.

Ok so I decided to completely rewrite this as nothing I tried worked. Im going to post what I did so that anyone else with the same problem can fix it(as I hate seeing posts ending with Yeah! I fixed it! bye see you never)

Also thanks to Nigel for his help!


on adding folder items to watchedFolder
	tell application "Finder"
		set size1 to (info for watchedFolder) --This section deals with waiting for files to be finished copying (My original problem).
		delay 3
		set size2 to (info for watchedFolder)
		repeat while size1 is not equal to size2
			set size1 to size2
			delay 3
			set size2 to (info for watchedFolder)
		end repeat
		set addedFiles to every file of entire contents of (watchedFolder as alias) --Searches for files after the files are fully transferred.
	end tell
	set extensionList to {"randomExtension"} --Extension property list just in case you want to neatly add more.
	repeat with i from 1 to number of items in addedFiles
		set currentFile to (item i of addedFiles)
		tell application "Finder" --To check for disired files with correct extension in property list above
			if name extension of currentFile is in extensionList then
				set extensionCheck to true
			else
				set extensionCheck to false
			end if
		end tell
		if extensionCheck is true then --Only performed if desired files are found.
			--AppleScript's code here.
			tell application "RandomApp"
				--RandomApp's code here.
			end tell
		end if
	end repeat
end adding folder items to