Folder Action and Repeat Problem

So I think I’m close, and I know where the problem is. I just don’t know how to fix it.

Here’s what I want this script to do. When a file OR a folder is dropped into a specified folder, the script should get entire contents of all files being added. Then repeat a specific process on each file.

This is part of a much larger script, but I simplified it for the sake of debugging. It works great when adding a folder filled with files. But when you add just one file it doesn’t work.
Here’s the script:

on adding folder items to this_folder after receiving added_items
	
	tell application "Finder"
		set original_element to added_items
		
		try
			set contents_list to the name of every file of entire contents of folder original_element as alias list
		on error
			set contents_list to name of original_element ----------> Problem Here.
		end try
		
		if class of contents_list is not list then set contents_list to {contents_list}
		
		repeat with file_list in contents_list
			display dialog file_list
		end repeat
		
	end tell
	
end adding folder items to

So if I change to a choose folder script, then it seems to work just fine on files. But I need to make this into a folder action script and work with both files AND folders.

set added_items to choose file

tell application "Finder"
	set original_element to added_items
	
	try
		set contents_list to the name of every file of entire contents of folder original_element as alias list
	on error
		set contents_list to name of original_element
	end try
	
	if class of contents_list is not list then set contents_list to {contents_list}
	
	repeat with file_list in contents_list
		display dialog file_list
	end repeat
	
end tell

I don’t understand why the script will work one way but not the other. Is there something wrong with my syntax??? Please help. I’ve been racking my brain on this all day.

Thanks!

Hi

I’ve not tested this but reckon if you get entire contents of this_folder instead of added_items then your script
should pick up both folders and files within the folder that the action is attached to.
At the minute i think your having trouble because your being too specific.

Your script is only looking for folders like here above where you have “folder original_element”

you could say:

get files of entire contents of this_folder

or

get folders of entire contents of this_folder

or less specific

get items of entire contents of this_folder

I hope this helps

Cheers

The only problem with referencing this_folder is that if there are already other files in the folder it will process all of them and not just the added_items.

I was hoping that the TRY statement above would reference a Folder being added first and then on error it would reference files. I don’t think my syntax is right for referencing files.

Right now I can only get the Path of added_items. What I really need is to get the name of the file added_items. I can reference it as a list later in the script.

Thanks for the suggestions.

Ok I figured it out. It was my syntax for referencing files. I was trying to get the name of the variable added_items and instead, I needed to get the name of info for added_items.

Here is the working script:

on adding folder items to this_folder after receiving added_items
	
	tell application "Finder"
		set original_element to added_items
		
		try
			set contents_list to the name of every file of entire contents of folder original_element as alias list ----->this applies if added_items is a folder
		on error
			set contents_list to name of (info for added_items) ------>this applies if added_items is a file
		end try
		
		if class of contents_list is not list then set contents_list to {contents_list} ------>This turns the added_items into a list
		
		repeat with file_list in contents_list
			display dialog file_list -------->Insert processing of each file here
		end repeat
	end tell
end adding folder items to