Creating a Folder Action Script - Copy folder into dropped folder

I am a total novice to scripting and have been trying to learn Applescript via Lynda.com In doing this I have learned that Applescript is very straight forward and I event got my script to work once, but now I can’t get it to work again. Each time I save my script and re-open it the first line characters appear as asian text.


on adding folder items to "users:johnnaptown:desktop:new art" after receiving added_items
	tell application "Finder"
		try
			duplicate "users:johnnaptown:desktop:new client art folder" to added_items
		end try
	end tell
end adding folder items to

Problem: when I re-open the file the line showing:


on adding folder items to "ã¨€ç”€çŒ€æ”€çˆ€çŒ€ã¨€æ¨€æ¼€æ €æ¸€æ¸€æ„€ç€€ç€æ¼€çœ€æ¸€ã¨€æ€æ”€çŒ€æ¬€ç€æ¼€ç€€ã¨€æ¸€æ”€çœ€€æ„€çˆ€ç€" after receiving added_items

shows the path as asian characters…

Any thoughts on why this is happening?

Hi John. Welcome to MacScripter.

The ‘on’ line of a handler shouldn’t contain any real values. Only variables to receive those values when the handler’s called. So you should have a variable name instead of “users:johnnaptown:desktop:new art” after the ‘to’ ” although you won’t actually need to use the value in your script.

Secondly, you want to duplicate the added items to the other folder, not it to them. Also, “users:johnnaptown:desktop:new client art folder” is just text, not a disk item as such. It’s also not a valid HFS path, since it doesn’t begin with a disk name. You should include the disk name and put ‘folder’ in front of it (when using the FInder or System Events). For instance, if your disk’s called “Macintosh HD”:

But in fact, with the Finder, if the folder’s on the current user desktop, you only need:

So:


on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			duplicate added_items to folder "new client art folder" of desktop
		end try
	end tell
end adding folder items to

NG - Thank you for your reply. While this worked it didn’t do what I was intending. I was trying to take a folder and drop it into a “HOTFOLDER” and have a folder script run where it was copying a certain number of other folders to that folder and every folder that is dropped in there I decided the following would be the best approach:


on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			make new folder in added_items with properties {name:"CLIENT FILES"}
			make new folder in added_items with properties {name:"HBA FILES"}
			make new folder in added_items with properties {name:"PRINT"}
		end try
	end tell
end adding folder items to

however, I am not stuck in wanting to move files from within the dropped folder to the new folders that I created. For example. Folder I dropped contains; Document Fonts Folder, Links Folder, InDesign File. I want to move these three items into the newly created Client Files. I was thinking the Move to class would work but I cannot get this to take hold ideas?

Best, John

Hi John.

Having read your second post several times, I think I understand what you’re saying:

When one or more folders are added to the hot folder to which this script is attached:

  1. Create three folders in each added folder.
  2. Move two folders and a file already in the added folder to the just-created folder called “CLIENT FILES”.

The script below should work as is except for the ‘move file’ line, which’ll probably need rewriting as it’s unlikely there’ll actually be a file called “InDesign File”.


on adding folder items to this_folder after receiving added_items
	-- added_items is a _list_ of the items just added, even when there's only one. Deal with each item in turn.
	repeat with this_item in added_items
		tell application "Finder"
			-- 'item this_item' gets a Finder reference to the item so that its class can be determined.
			if (class of item this_item is folder) then
				try
					-- Make new folders in this dropped folder.
					set client_files_folder to (make new folder in this_item with properties {name:"CLIENT FILES"})
					make new folder in this_item with properties {name:"HBA FILES"}
					make new folder in this_item with properties {name:"PRINT"}
					
					-- Move the following items from the dropped folder to the just-created "CLIENT FILES" folder.
					move folder "Document Fonts Folder" of this_item to client_files_folder
					move folder "Links Folder" of this_item to client_files_folder
					-- Unless there really is a file called "InDesign File", this line won't work. More information needed.
					move file "InDesign File" of this_item to client_files_folder
				on error errMsg
					display dialog errMsg
				end try
			end if
		end tell
	end repeat
end adding folder items to

NG - THANK YOU FOR THE DIRECTION ON THIS!!!

I have been able to use the script to build my final script. Your assistance and direction for this project has been very helpful. Are there any books/online tutorials or any reference material that you might recommend for me to keep learning?

Also, below is my final script:


on adding folder items to this_folder after receiving added_items
	-- added_items is a _list_ of the items just added, even when there's only one. Deal with each item in turn.
	repeat with this_item in added_items
		tell application "Finder"
			-- 'item this_item' gets a Finder reference to the item so that its class can be determined.
			if (class of item this_item is folder) then
				try
					-- Make new folders in this dropped folder.
					set client_files_folder to (make new folder in this_item with properties {name:"CLIENT FILES"})
					make new folder in this_item with properties {name:"HBA FILES"}
					make new folder in this_item with properties {name:"PRINT"}
					
					-- Move the following items from the dropped folder to the just-created "CLIENT FILES" folder.
					move folder "Document Fonts" of this_item to client_files_folder
					move folder "Links" of this_item to client_files_folder
					-- Unless there really is a file called "InDesign File", this line won't work. More information needed.
					move (every file of this_item whose name extension is in {"indd", "idml", "txt", "pdf"}) to client_files_folder
				on error errMsg
					display dialog errMsg
				end try
			end if
		end tell
	end repeat
end adding folder items to