Speed up script?

Hello all,

I have a simple script for moving files. It passes variables to a shell script that does a cp command (we are working around a Finder bug). Anyway, the script works as written but some folks are complaining that if a lot of files are dropped on the droplet, it takes about 10-15 seconds per file to copy. So…is there a good way to speed up a script like this? It’s a simple script so how much tighter can it get?

Here is the script (needs to be saved as a droplet):


global theLocation

-- This following line is required to make it a droplet. 

on open theDropped
	
	-- This sets the Copy To location
	
	tell application "Finder"
		set theLocation to choose folder with prompt "Which folder do you want me to copy the file/files to?"
	end tell
	
	
	-- This begins the run loop for when multiple files are dropped on the droplet. 
	
	repeat with oneDrop in theDropped
		
		set iPath to quoted form of POSIX path of oneDrop
		tell application "System Events" to set thename to the name of oneDrop
		
		tell application "Finder"
			
			-- This section looks for existing files in the location where you want to copy the file and warns is a duplicate exists. This may be partly where the slowdown happens. 
			
			set checklocation to (name of every file in folder theLocation)


			
			if checklocation contains thename then
				try
					display dialog "The file " & thename & space & "already exists in this location, if you continue it will be overwritten."
				on error
					error number -128
					
				end try
			end if
			
		end tell
		
		
		set thefolder to the quoted form of the POSIX path of theLocation
		
		-- The following line does the copy via the command line. 
		
		do shell script "cp " & iPath & space & thefolder & (quoted form of thename)
		
	end repeat -- go back for another (assuming a one-at-a-time process)
	return
end open



Anyway, what would be a slicker script for this?

Cheers.

First of all, try to avoid tell blocks as much as possible. It can really annoy people. What if they had files with similar names and they forgot which file they wanted to copy? A quick look in the file would show them. So they start browsing for the file but they get blocked by the display dialog.

I don’t understand your do shell script "cp . You’d get something like this: cp ‘∞/Desktop/aFile’ ‘~/Documents/’‘aFile’. Not the two between “Documents” and “aFile”? To copy files it suffice to give only the destination folder.

The global declaration is also something I don’t quite get. You only have one handler so there’s no need to declare them global.

Script:

-- This following line is required to make it a droplet. 
on open theDropped
	-- This sets the Copy To location
	set theLocation to choose folder with prompt "Which folder do you want me to copy the file/files to?"
	
	
	-- This begins the run loop for when multiple files are dropped on the droplet.
	repeat with oneDrop in theDropped
		set iPath to quoted form of POSIX path of oneDrop
		set thename to the name of (info for oneDrop)
		
		
		-- This section looks for existing files in the location where you want to copy the file and warns is a duplicate exists. This may be partly where the slowdown happens. 
		set checklocation to (list folder theLocation)
		if checklocation contains thename then display dialog "The file " & thename & space & "already exists in this location, if you continue it will be overwritten."
		
		-- construct destination
		set thefolder to the quoted form of the POSIX path of theLocation
		
		-- The following line does the copy via the command line. 
		do shell script "cp " & iPath & space & thefolder
		
	end repeat -- go back for another (assuming a one-at-a-time process)
end open



Hope it helps,
ief2

Cool. I will try it out. Thanks.

Hi,

list folder is quite expensive. It’s better to do it once before the repeat loop
and add the new names to the list.
The line to coerce the folder reference to a POSIX path is only required once


-- This following line is required to make it a droplet. 
on open theDropped
	-- This sets the Copy To location
	set theLocation to choose folder with prompt "Which folder do you want me to copy the file/files to?"
	set checklocation to (list folder theLocation)
	-- construct destination
	set thefolder to the quoted form of the POSIX path of theLocation
	
	-- This begins the run loop for when multiple files are dropped on the droplet.
	repeat with oneDrop in theDropped
		set thename to the name of (info for oneDrop)
		if checklocation contains thename then display dialog "The file " & thename & space & "already exists in this location, if you continue it will be overwritten."
		set end of checklocation to thename
		
		-- The following line does the copy via the command line. 
		do shell script "cp " & quoted form of POSIX path of oneDrop & space & thefolder
		
	end repeat -- go back for another (assuming a one-at-a-time process)
end open

That did a pretty good job. The listing of the files inside the loop was the worse time suck.

Thanks.

Anything that you can replace with shell scripts will be faster, maybe you could do the routine where you check for existing files with same name in a shell script as well.

Manuel