Drag & Drop & Copy

I’m writing an application that will do something that seems like it should be fairly common: copy the file that is dropped on the application to a known location.

The code I wrote seems to cause the program to fuzz out - when even copying only one file - with the result that I get the whirling beach ball of death for several seconds before the copy operation happens. And this pause does definitely happen just prior to the copy operation. I’ve had it display dialogs before and after that statement, and everything happens quickly right up until the ‘copy file’ line. Yet, on its own, of course the copy line is fine.

I was hoping someone could tell me what’s wrong with the following code (which is stripped down at this point and doesn’t actually do anything with the dropped file names, for simplicity - but run on my machine, it does display the behavior explained above):


on drop theObject drag info dragInfo
	if the name of theObject is "genreTableView" then
		set dataTypes to types of pasteboard of dragInfo
		if "file names" is in dataTypes then
			set theFiles to {}
			set preferred type of pasteboard of dragInfo to "file names"
			set theFiles to contents of pasteboard of dragInfo
			if (count of theFiles) > 0 then
				tell application "Finder"
						copy file "Macintosh HD:Users:Ransom:Desktop:To Do.txt" to folder "Macintosh HD:Users:Ransom:Documents"
				end tell
			end if
		end if
		set preferred type of pasteboard of dragInfo to ""
		return true
	end if
end drop

Update:

Swapping out the finder/copy block with a do shell script line solves the problem. So why does talking to Finder in this circumstance cause such a delay?

-Joe

Use ‘duplicate’ instead of ‘copy’.

Thank you, but that doesn’t make the delay go away.

Hows about using the ignoring statement:

tell application “Finder”
ignoring application responses
copy file “Macintosh HD:Users:kel:Desktop:To Do.txt” to ¬
folder “Macintosh HD:Users:kel:Documents”
end ignoring
end tell

gl,

Hmm. Same response. I was sure that would work - I’d read about it but completely failed to think of it. Drat.

I also tried using an empty “tell” block. Same response!

The only way to avoid the 5 to 10 second delay is to have only internal commands within the handler - changes to variables, dialog displays, etc.

Is this a rule with drag and drop in applescript studio? No calls to external objects during the handler? That’d really curtail its usefulness! :frowning:

-Joe

I’m so sorry. I was completely wrong.

One thing I somehow didn’t copy to the code I posted is a call to an internal handler I wrote - basically a subroutine. That’s what’s doing it. If I comment that out, then all is well. But I need to call it or the drag and drop is worthless.

And I call this function all over the place in my code with no problems. I guess I’ll have to look into it to see what’s causing the problem.

If I figure it out, I’ll post.

Okay, here’s the offending code:


				tell application "Finder"
					set fileList to name of every file in folder currentFolder as list
				end tell

I copied the entire subroutine to the drop handler, then eliminated pieces, block by block, until I isolated this piece of code. If this is commented out, then it all runs just swell - but I can’t have a list of the files in the folder where the drag-and-drop operation placed the file(s) that were dropped. I need that list.

I suppose I could just insert the file name from the drop operation into the list manually, and then do an alphabetical sort on it, but it seems reasonable to run the above code in a drop handler.

I tried to put the “ignore responses” code around the “set fileList . . .” statement, but I ended up getting an empty list, of course. Silly me.

By the way, the folder in question has only eight files in it - nine after the drop operation - so that shouldn’t be a problem. I’ve run the above code on folders with two hundred or more files in them and its run very quickly.

Any thoughts, or suggested avenues I could pursue to figure out a way to let me run this command without getting that 5-10 second delay?

Thanks again,

-Joe

So, in the final analysis, it seems that any ‘"tell application “Finder”’ block, if executed during a drop operation, will cause a delay. Other tell blocks may also cause this effect, but I haven’t tested that.

My solution in this case was to work around it by using a shell script to do the copying, then add the file name to the list variable by concatination, then sort the list alphabetically.

Hopefully the rest of my drop operations won’t truly need tell blocks.

Thank you for your help once again, Kel!