Applescript crashes when sent to the background

I have a problem with an applescript app which works great so long as you don’t bring any other window to the foreground while it is in its main processing loop.

it is basically a tableView which I can drag/drop files onto and then I loop through and process them according to their name, relocate them on the filesystem and insert a record into a filemaker database. I can’t figure out why I have to keep my app active while it’s looping through all the files.

And neither can anyone here, without your source. :wink:

Ha. I had a secret hope that this would have been a common enough issue that someone would say “you must be doing such-and-such”.

The full code is quite long but I’ll see if I can pull out just the loop that seems to cause the trouble…

on clicked theObject
	if the name of theObject is "importButton" then
		set theFMDatabase to contents of default entry "filemakerDB" of user defaults as string
		set uses threaded animation of progress indicator "busy_Indicator" of window "main" to true
		set uses threaded animation of progress indicator "importProgress_Indicator" of window "progressPanel" to true
		start progress indicator "busy_Indicator" of window "main"
		start progress indicator "importProgress_Indicator" of window "progressPanel"
		set contents of progress indicator "importProgress_Indicator" of window "progressPanel" to 0
		display panel window "progressPanel" attached to window "main"
		set contents of text field "importMessage_TextField" of window "progressPanel" to theFMDatabase
		set theTable to table view ¬
			"files" of scroll view ¬
			"files" of view id 3 of tab view ¬
			"importList_TabView" of window "main"
		-- set selected_items to selected rows of table view id 5 of scroll view id 4 of view id 3 of tab view id 2 of window id 1
		set selected_items to selected rows of theTable
		-- set theDataSource to data source of table view id 5 of scroll view id 4 of view id 3 of tab view id 2 of window id 1
		set theDataSource to data source of theTable
		--Turn off the updating of the views
		set update views of theDataSource to false
		
		if (count of selected_items) ≥ 1 then
			set rootPath to contents of default entry "structureRoot" of user defaults
			repeat with theRow from (length of selected_items) to 1 by -1
				set theFilePath to contents of data cell "file_path" of data row (item theRow of selected_items) of theDataSource
				set theNewName to contents of data cell "file_name" of data row (item theRow of selected_items) of theDataSource
				
				if validFileName(theNewName as string) then
					set contents of progress indicator "importProgress_Indicator" of window "progressPanel" to (1 - (theRow / (length of selected_items))) * 100
					
					set movDir to "NIL"
					set theNewPath to rootPath & "/" & filePathFor(theNewName, movDir)
					try
						do shell script "mkdir -p " & theNewPath
					end try
					set newLocation to (theNewPath & "/" & theNewName) as string
					
					try
						addAssetToDB(theNewName, theNewPath, theFilePath)
					on error
						stop progress indicator "busy_Indicator" of window "main"
						display dialog ("Could not Add to Database: " & theNewPath & "/" & theNewName) buttons {"OK"} default button {"OK"}
					end try
					
					try
						moveFile(theFilePath, newLocation)
					on error
						stop progress indicator "busy_Indicator" of window "main"
						display dialog "ERROR on file transfer: " & theNewPath & "/" & theNewName
					end try
					
					delete data row (item theRow of selected_items) in theDataSource
				end if
			end repeat
		end if
		set selected rows of theTable to {}
		--Turn on the updating of the views
		set update views of theDataSource to true
		stop progress indicator "busy_Indicator" of window "main"
		stop progress indicator "importProgress_Indicator" of window "progressPanel"
		close panel window "progressPanel"
		
	end if
	
end clicked

on addAssetToDB(fileName, theNewFilePath, theOldFilePath)
	set theFullPath to (theNewFilePath & "/" & fileName)
	set theOldFullPath to theOldFilePath

	set theFileInfo to do shell script "ls -l " & theOldFilePath		
	try
		set theModifyDate to getMetaData("kMDItemContentModificationDate", theOldFilePath) as string
	on error
		set theModifyDate to "unknown" as string
	end try
		
	set FMDatabase to contents of default entry "filemakerDB" of user defaults
	tell application "FileMaker Pro"
		tell database FMDatabase
			create new record in table "ELEMENTS"
			set theRecord to create new record in table "storage_element"
			set cellValue of cell "versionDateTime" of theRecord to theModifyDate as string
		end tell
	end tell
		
end addAssetToDB

on getMetaData(dataTag, file_path)
	set metaData to {}
	set rawData to (do shell script "mdls " & file_path & " | sed 's| *= |=|' | grep -e\"^kMD\"")
	set theLines to paragraphs of rawData
	set {olddelims, text item delimiters} to {text item delimiters, "="}
	repeat with thisLine in theLines
		set metaData to metaData & {{(text item 1 of thisLine), (text items 2 thru -1 of thisLine as string)}}
	end repeat
	set text item delimiters to olddelims
	
	return valueFromKey(dataTag, metaData)
end getMetaData