Help with Folder Action

I have been scripting for about 6 weeks now. And I finally hit two solid brick walls.

I ventured out to do my first folder action script, and for the life of me, I can’t figure out what I’m doing wrong. When I dropped files in the folder, nothing happens. The goal of the script is: When you drip files on to one folder, it moves it to another folder, and deletes the original. (I started simply with the duplicate. I will then worry about deleting the file.) If anyone has any idea what is wrong with the syntax, please let me know. I checked every book, every example, and the internet, and the syntax seems correct to me. Here is the script:


set destination_path to "ATG_HSQ:Applescript_Test:"

on adding folder items to this_folder after receiving these_items
	repeat with current_file from 1 to count these_items
		tell application "Finder"
			duplicate file current_file to destination_path
		end tell
	end repeat
end adding folder items to

One thing I found really difficult about troubleshooting this script is you can’t break it up and see where the problem is happening as you can when you run a script in script editor. Any suggestions on how folks deal with troubleshooting Folder Action Scripts would be appreciated.

While trying to troubleshoot this script, I tried to duplicate the functionality in a script that I could run from Script Editor so I could at least test the basic functionality, and I ran into another problem that makes no sense. I spent hours on this simple thing, and I can’t figure it out. The script is:


set destination_path to "ATG_HSQ:Applescript_Test:"
set the_folder to (choose folder) as string
set these_items to list folder alias the_folder ¬
	without invisibles

set result to these_items

repeat with i from 1 to count these_items
	tell application "Finder"
		duplicate file i to destination_path replacing yes
	end tell
end repeat

The script is randomly copying files from my desktop to the destination_path. It is happening on more than one computer. (It always copies the same files. If the_folder has 5 files in it, then it copies 5 files from the desktop. If the_folder has 3, it copies 3)

I removed the repeat loop from the script and added the line:

set result to these_items

The script is returning the correct file names from these_items. Yet, as soon as I return the repeat loop it continues to copy files from the desktop.

I spent several hours yesterday, and explored every avenue I could think of to figure this out and hit a brick wall. Any help anyone can offer with these two issues would be greatly appreciated. Thanks in advance.

Hi, wexie. The value of ‘current_file’ in this sort of repeat is a number between 1 and ‘(count these_items)’! The Finder is duplicating its own ‘file 1’, ‘file 2’, etc., which, since their location isn’t specified, are assumed to be on the Desktop. You really want to duplicate the numbered items in the list ‘these_items’. The other thing is that your script is trying to duplicate the items to the string “ATG_HSQ:Applescript_Test:” instead of to a proper folder specifier. In the Finder, this can be the string with the keyword ‘folder’ in front of it.

set destination_path to "ATG_HSQ:Applescript_Test:"

on adding folder items to this_folder after receiving these_items
	repeat with current_file from 1 to (count these_items)
		tell application "Finder"
			duplicate (item current_file of these_items) to folder destination_path
		end tell
	end repeat
end adding folder items to

Instead of using loop numbers, you could, if you liked, use references to the items in the list, phrasing the repeat like this:

set destination_path to "ATG_HSQ:Applescript_Test:"

on adding folder items to this_folder after receiving these_items
	repeat with current_file in these_items
		tell application "Finder"
			duplicate current_file to folder destination_path
		end tell
	end repeat
end adding folder items to

However, if you’re duplicating all the files to the same folder, the Finder’s clever enough to do it in bulk:

set destination_path to "ATG_HSQ:Applescript_Test:"

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		duplicate these_items to folder destination_path
	end tell
end adding folder items to

Thanks so much for your reply. I had gotten a script working, but thanks for explaining to me what I did wrong in that script, so I will know what not to do in the future.

The script I came up with was:


on adding folder items to the_folder after receiving these_items
	tell application "Finder"
		delay 20
		try
			set filesToMove to every file of the_folder
			repeat with aFIle in filesToMove
				move aFIle to folder "HS_Highs:00.0ImagePlacement" with replacing
				delay 5
				delete aFIle
			end repeat
		on error the error_message number the error_number
			set the error_text to "Error: " & the error_number & ". " & the error_message
			display dialog the error_text & return & ¬
				"Call The Help Desk." buttons {"OK"} default button 1
			return the error_text
		end try
		
		try
			set subFolders to (every folder of the_folder)
			repeat with aFolder in subFolders
				move aFolder to folder "HS_Highs:00.0ImagePlacement" with replacing
				delay 5
				delete aFolder
			end repeat
		on error the error_message number the error_number
			set the error_text to "Error: " & the error_number & ". " & the error_message
			display dialog the error_text & return & ¬
				"Call The Help Desk. xxx-xxx-xxxx" buttons {"OK"} default button 1
			return the error_text
		end try
	end tell
end adding folder items to