Script to Auto-Copy files when USB is mounted has a strange quirk

Hi, my AppleScript below exhibits an unwanted behavior when the “Duplicate” function is being executed.
This script automatically copies movie files from a USB drive to my computer when inserted. If there are only 1 - 3 movies it works fine, however, if there are several movies and copying lasts more than 2 minutes the script continues on to the next steps in the script.

For example: There are 9 movies to be copied, 4 to the “Movies” folder and 5 to the “Kids Movies” folder > I plug in the USB and the script starts copying the first 4 movies - the standard file copy dialog box appears > After 2 minutes another copy dialog box appears showing the other 5 movies copying into the Kids Movies folder > after 2 more minutes my “USB Auto Eject” dialog box appears, 60 secs later it disappears as it should (the movies are still being copied)> then my USB Ejected dialog box appears and disappears after 110 secs. At this point the USB drive is supposed to be ejected, it does not eject, the movies are still being copied. When the copying completes the file copy dialog box disappears. All the movies were copied correctly and the USB drive was not ejected. And, of course, I do not want the USB drive to eject until copying is complete.

So, my question is how to modify this script so that it pauses when copying, only as long as necessary, until the file copy process completes on each “Duplicate” function?

Thanks in advance for any help with this.

property Volumes : {"Entertainment Center", "Entertainment Center Files", "OS X Base System", "USB 750"} -- add as needed
property videoExtensions : {"avi", "mov", "mpg", "mp4", "wmv", "mkv"}

set movieVolume to alias (POSIX file "/Volumes/Entertainment Center Files/Entertainment/Movies")
set kidsmovieVolume to alias (POSIX file "/Volumes/Entertainment Center Files/Entertainment/Kids Movies")

tell application "System Events"
	set rootVolumes to disk item (POSIX file "/Volumes" as text)
	set allVolumes to name of every disk item of rootVolumes
	set numofallVolumes to count of allVolumes
	try
		set thisVolume to item 3 of allVolumes
	on error errorMessage number errorNumber
		set _error to errorMessage
		set _errorNum to errorNumber
		if errorNumber is -1728 then
			display dialog "There are no USB Drives Mounted." buttons {"OK"} default button 1 with title "Did You Insert a USB Drive? . . . ." giving up after 10
			if button returned of result is "OK" or gave up of result then error number -128
		end if
	end try
	
	tell application "Finder"
		activate
		delay 2
		if thisVolume is in the Volumes then
			set runScript to 1
		else
			try
				if exists alias (POSIX file ("/Volumes/" & thisVolume & "/movies") as text) then set runScript to 2
			on error errorMessage number errorNumber
				set _error to errorMessage
				set _errorNum to errorNumber
				if errorNumber is -1700 then
					display dialog "No Movies Folder on This USB Drive --- There Must be a *Movies Folder* or a *Kids Movies Folder* for Copy Function to Activate . . . . . USB Drive Will Eject in 60 Secs" buttons {"OK", "No"} default button 1 with title "Movie Copy Error !!" giving up after 60
					if button returned of result is "No" then
						error number -128
					else
						if button returned of result is "OK" or gave up of result then
							eject thisVolume
							display dialog "U S B    D r i v e    E j e c t e d    -    O K   t o   R e m o v e" buttons {"Not Necessary to Click this Button"} default button 1 giving up after 10
							return
						end if
					end if
				end if
			end try
		end if
		
		
		if runScript = 1 then error number -128
		
		if runScript = 2 then
			try
				duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file ("/Volumes/" & thisVolume & "/movies") as text) to movieVolume
				
			on error errorMessage number errorNumber
				set _error to errorMessage
				set _errorNum to errorNumber
				if errorNumber is -15267 then
					display dialog "This Movie Already Exists in the Movies Folder" buttons {"OK", "No"} default button 1 with title "Movie Copy Error !!" giving up after 10
					if button returned of result is "No" then
						error number -128
					else
						if button returned of result is "OK" or gave up of result then
							eject thisVolume
							display dialog "U S B    D r i v e    E j e c t e d    -    O K   t o   R e m o v e" buttons {"Not Necessary to Click this Button"} default button 1 giving up after 10
							return
						end if
					end if
				end if
			end try
			
			try
				duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file ("/Volumes/" & thisVolume & "/kids movies") as text) to kidsmovieVolume
				
			on error errorMessage number errorNumber
				set _error to errorMessage
				set _errorNum to errorNumber
				if errorNumber is -15267 then
					display dialog "This Kids Movie Already Exists in the Kids Movies Folder" buttons {"OK", "No"} default button 1 with title "Movie Copy Error !!" giving up after 10
					if button returned of result is "No" then
						error number -128
					else
						if button returned of result is "OK" or gave up of result then
							eject thisVolume
							display dialog "U S B    D r i v e    E j e c t e d    -    O K   t o   R e m o v e" buttons {"Not Necessary to Click this Button"} default button 1 giving up after 10
							return
						end if
					end if
				end if
			end try
			
			display dialog "USB Drive will Auto Eject in 60 Seconds or Click OK. . . . . . ." buttons {"OK", "No"} default button 1 with title "Copy Function Completed - Eject?" giving up after 60
			if button returned of result is "No" then
				error number -128
			else
				if button returned of result is "OK" or gave up of result then eject thisVolume
				display dialog "U S B    D r i v e    E j e c t e d    -    O K   t o   R e m o v e" buttons {"Not Necessary to Click this Button"} default button 1 giving up after 110
				
			end if
		end if
	end tell
end tell

Model: MacMini
Browser: Safari 603.2.4
Operating System: macOS 10.12

Your script is just awful. It has repeatable operations and there is no subroutines at all. I have no time to understand it. First put it in order. Yes, the problem is that the execution of the script overtakes the duplicating. If you ordered things, everything would become clear by itself, and so … look for yourself.

Here’s how to check if a certain code line can do it in 2 seconds (which is default time in AppleScript):


set timeoutSeconds to 2.0
set uiScript to "set thisVolume to item 3 of allVolumes"
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
activate
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doWithTimeout

If certain code line needs more than 2 seconds, then give more seconds. For example:

with timeout 10 seconds
 -- your code line
end timeout

Use Automator excellent subroutine above with code lines, were you duplicate files and replace
line set timeoutSeconds to 2.0 with for example set timeoutSeconds to 200.0 (to find how much time code line needs):

set timeoutSeconds to 200.0
set uiScript to "duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file (\"/Volumes/\" & thisVolume & "/movies\") as text) to movieVolume"
my doWithTimeout(uiScript, timeoutSeconds)

NOTE: As you use “Finder” for this duplicating code line, replace “System Events” in subroutine of Automator with “Finder”, too. And remove tell application “Finder”/“System Events” were no need in your run handler

ORRRRR… JUST SET REALLY BIG TIME:


with timeout of 3600 seconds
duplicate (items whose name extension is in the videoExtensions) in alias (POSIX file ("/Volumes/" & thisVolume & "/kids movies") as text) to kidsmovieVolume
end with

Do not worry, this is only the maximum time, your code line will be executed faster than 1 hour

Thanks KniazidisR for your reply. In my own defense, I am a novice with AppleScript so I do not know how the script should be ordered. This is my first script so unfortunately, I don’t know what I don’t know.

If I understand correctly, your suggestion is to use your “uiScript” to pause the script for a specified number of seconds, however, the number of movies to be copied will change and therefore the number of seconds would be different. If I copied just one movie, for example, I would not need the “uiScript” timeout, or if I copied 7 movies I would need perhaps 450 seconds timeout. Is there a way to make it AUTOMATICALLY variable? Or, a way to have the script detect the file copy dialog box and pause until the box disappears?

Thank you for your suggestions.