cancel "duplicate" command xcode-applescript

Hi
Im putting together an AS Studio app in Xcode, part of the script requires
that I enter a file name in a text field, then after a button is clicked a search is performed for the asked for file, when a match is found the file is automatically duplicated to another folder after a delay of 3 seconds, during that three seconds I would some how like to be able to cancel the duplicate command,

This is the duplicate piece.

if baseName contains “16” then
duplicate FileToOpen to “MacintoshHD:Users:Budgie:Desktop:HotFolders:M:” with replacing

I am trying to stop the duplicate process via:

if name of theObject is “stop” then
??

any ideas welcome

cheers (let me no if I need to supply more info) :slight_smile:
Budgie

ok, ive found this piece of code that will return a %, which from what I can gather is the amount of CPU usage
for the process that is happening at that time in my application, my idea is to take that information and use
it to cancel the duplicate process, but im uncertain how to implement this, or am I going way of the tracks
and missing something really simple to do with canceling the duplicate process while it’s in action?

do shell script "top -s 2 -l 2 | awk '/FREEZE/ {busy= $3}; END {print busy}'"

ive been researching and have found that I can kill a process by locating the PID, im not proficient in shell scripting so would appreciate some help

http://bbs.applescript.net/viewtopic.php?id=12870

how would I implement this to stop the duplicate process?, is the right, wrong way to go?

found that the research ive been doing is more for shell commands etc, but the duplicate command in this case is not in a shell, im thinking that it’s not a simple a task to stop a process after it has started, any suggestions on a work around to this issue??

else if t_name is “FIND” then

set _path to quoted form of POSIX path of "Macintosh HD:Users:Budgie:Desktop:untitled folder:"
	set _selecteditem to contents of text field "STDFILE" of window "main" as text
	set f to paragraphs of (do shell script "/usr/bin/find " & _path & " -type f -name '" & _selecteditem & "'")
	if f is not {""} then
		set FileToOpen to (POSIX file (item 1 of f)) as alias
		set contents of text field "SENTORIP" of window "main" to "Match found, sending to the rip in 3 seconds"
		set text color of text field "FILEFOUND" of window "main" to {0, 0, 65535}
		set contents of text field "FILEFOUND" of window "main" to FileToOpen
		----------------------------------
		delay 3
		----------------------------------
		tell application "Finder"
			set anItem to FileToOpen
			tell (info for anItem) to set {tName, tEx} to {name, name extension}
			set baseName to text 1 thru ((get offset of "." & tEx in tName) - 1) of tName
			----------------------------------
			----
			----------------------------------
			if baseName contains "16" then
				duplicate FileToOpen to "Macintosh HD:Users:Budgie:Desktop:HotFolders:M:" with replacing
				----------------------------------

end if
end tell
else
set text color of text field “FILEFOUND” of window “main” to {65535, 0, 0}
set contents of text field “FILEFOUND” of window “main” to “NO MATCH FOUND”
end if
else if name of theObject is “stop” then
??? stop duplicateing process

Why?

Why do you want to cancel the duplication? It sounds like the problem lies in your process flow, not in whether there is a way to do it. The reason that there is no “cancel” function is because there isn’t meant to be one. You, as a programmer, have the burden of doing such things in a clear, conforming, responsible manner. It is very unconventional to do a find and then jump right into duplicating the found item(s). Sounds like a disaster waiting to happen. What if the user types in “system” or “library”, and then spills their coffee? While they run off to fetch a towel, the Finder is duplicating half their hard drive. It sounds much more elegant and intuitive to do a search, display the results, and then offer the user a button to click to initiate the task. I might use a table view to list the found files, with a checkbox next to each that the user can check to select the files they wish to copy. You should always give the user the opportunity to cancel before they ever begin. Even if you could cancel the duplication halfway through, it would still be your responsibility to go in and delete all of the files that you duplicated before the process was cancelled, so the user could be certain that the ENTIRE operation was cancelled, not just what was left of it.

Also, using the delay command won’t work anyways, because the app will block until the delay is over and then jump right into execution of the rest of the script. Use an idle loop for this sort of thing instead.

I would advise against your current approach and work out a safer, more predictable solution.

Why?, hmmmm I am beginning to wonder that my self, initially it was as it is, to “cancel” the duplicate command if a problem was seen, but I am now starting to see that this is (A) not achievable, and (B) showing that it probably pointeless.

The disaster is very unlikely to happen as the script is case sensitive and set up to only work on a specific text format, from a specific hard drive (which will eventually be a external 500gb firewire drive), it is not directed to the main hard drive as it is at present, the text is like this Q1234-16, so if you typed in q1234-16, the script will not find and send anything to the rip, if their is a number, slash, “.” etc out of sequence it will not find and send anything to the rip, these numbers come from a list of numbers supplied via a job info card, the text is either manually entered of entered with a text recognition pen scanner, what is typed/scanned and what is searched for must match before the process can continue, hence the cancel idea is probably void, I did have the results displayed and the option given to click a button, but decided I wanted to automate the flow as much as possible, if their is no match found the script stops and says no match found. this is triggered by the “else” statement.

Thanks jobu I will look into different ways of doing my process flow, appreciate the come back.