idle routine disables ability to click buttons

I have an applescript studio project that counts the files in a watch folder and displays a progress bar while there are files in the folder. A completely different process that runs as root removes the files from the folder as each task is completed. As the tasks are completed the progress bar updates. In order to get the progress bars to work I put the code in the on idle handler, see below.

Now, I am trying to add a button to the application which will kill the process running as root. However, I believe that the idle handler is not allowing the on clicked handler to execute. Has anyone dealt with this or have any ideas how I could work around this?


-- teset.applescript
-- test

on idle theObject
	set watchfolder to "myhd:watch:"
	set the startingCount to countWatchfolder(watchfolder)
	set content of text field "currentcount" of window "main" to startingCount as number
	
	set content of text field "startingCount" of window "main" to startingCount
	set maximum value of progress indicator "bar" of window "main" to startingCount
	set content of progress indicator "bar" of window "main" to 0
	set indeterminate of progress indicator "bar" of window "main" to false
	
	start progress indicator "bar" of window "main"
	start progress indicator "spinner" of window "main"
	
	if startingCount is greater than 0 then
		set visible of window "main" to true
	else
		quit
	end if
	
	
	
	
	set currentCount to countWatchfolder(watchfolder)
	
	
	repeat until currentCount = 0
		set currentCount to countWatchfolder(watchfolder) as number
		set currentFile to getFileName(watchfolder)
		set content of text field "currentcount" of window "main" to currentCount
		set content of text field "fileName" of window "main" to currentFile
		set processedCount to (startingCount - currentCount)
		set content of progress indicator "bar" of window "main" to processedCount
		
	end repeat
	quit
end idle

on clicked theObject
	if the name of theObject = "abortRequest" then
		set theReply to display dialog "Are you sure you want to stop this request?" buttons {"No", "Yes"} default button 1 attached to window "main"
	end if
end clicked

on dialog ended theObject with reply theReply
	if button returned of theReply is equal to "Yes" then
		killprocess()
	end if
end dialog ended





on countWatchfolder(watchfolder)
	set theFolderItems to list folder watchfolder without invisibles
	set theCount to (count of theFolderItems) as number
	return theCount as number
end countWatchfolder

on getFileName(watchfolder)
	set master_file_list to list folder alias watchfolder without invisibles
	set this_item to item 1 of master_file_list
	set this_item_name_list to every character of this_item
	set this_item_name to items 6 through -5 of this_item_name_list as string --display dialog this_item_name_list
	return this_item_name
end getFileName


on killprocess()
	do shell script "sudo killall taskApp" user name "admin" password "" with administrator privileges
end killprocess

I’ve used the on idle-handler a lot of times, and I never had problems with it. Is your idle handler linked to the Application or the File’s Owner in Interface Builder? I always connect it to the File’s Owner. And maybe splitting the one script into two scripts can help solve your problem, though I hardly doubt that’s the problem.

Thanks for the reply.

The idle-handler is linked to the File’s owner, though when I look at the “Application” in interface builder the idle-handler has a - on the check box, (not fully checked), as well.

The idle handler appears to work correctly. The problem, I think, is the clicked handler. Because the idle handler is running the “repeat until currentCount=0” routine, it can’t receive the event. I am only guessing, I am not sure.

How would you suggest splitting the script?

Oooow, didn’t see your repeat block. Putting a repeat block in your on idle-handler is not quite the meaning of it. It’s best you link your main window to the same script with the Awake From Nib handler. Then make all necessary variables global and in the Awake From Nib setup all variables.

Replace the on idle with something like this (not tested):

global watchFolder
global startingCount

on awake from nib theObject
	set watchFolder to "myHD:Watching"
	set startingCount to countWatchFolder(watchFolder)
	
	tell window "Main" to tell progress indicator "bar"
		set maximum value to startingCount
		set contents to startingCount
		set indeterminate to false
	end tell
	
	tell window "Main"
		start progress indicator "bar"
		start progress indicator "spinner"
	end tell
	
	--call method "makeKeyAndOrderFront:" of window "Main"
end awake from nib

on idle theObject
	if (countAllItemsOfFolder(watchFolder)) is not 0 then
		set currentCount to countAllItemsOfFolder(watchFolder)
		set currentFile to getFileName(watchFolder)
		
		tell window "Main"
			set contents of progress indicator "Bar" to (startingCount - currentCount)
			set contents of text field "currentcount" to currentCount
			set contents of text field "filename" to currentFile
		end tell
	else
		tell me to quit
	end if
	
	return 1
end idle


(* ===== HANDLERS ===== *)
on countAllItemsOfFolder(aFolder)
	set theFolderItems to list folder aFolder without invisibles
	return (count of theFolderItems) as integer
end countAllItemsOfFolder

on getFileName(watchFolder)
	set master_file_list to list folder alias watchFolder without invisibles
	set this_item to item 1 of master_file_list
	set this_item_name_list to every character of this_item
	set this_item_name to items 6 through -5 of this_item_name_list as string --display dialog this_item_name_list
	return this_item_name
end getFileName

Hope it helps,
ief2