Applescript won't complete

Perhaps this is too trivial for such an advanced forum, but I hope you will indulge a newbie scripter.
I have a nagging problem with a very simple script for numbers. It starts with an on run and ends with an end run. At the beginning i have a say “start” and at the end I have a say “end”. Between the code works it looks up some cells on the third page of a numbers chart and puts them in the right cells for each row on the first page(sheet).

BUT when I try to close the script it says it is still running, and the only way to close it is a force quit. So what do I need to do to have the script quit normally?

Thanks for your indulgence and help.

As we aren’t sooth sayers, you have to post your code here if you want some help about it.

Yvan KOENIG (VALLAURIS, France) mercredi 29 avril 2015 15:14:58

sorry.

(*
AppleScript for Numbers - a numbers document has been opened. Now gets stock names from column 1 of row 2 to row 31 of the document’s first sheet, first table containing stock symbols in the column . Use each symbol on this first sheet to loop through the symbols on the third sheet; find the symbol and move the dividend data from cells on third sheet to cells on the first sheet
*)
property theApp : “Numbers”

on run
local trueDoc, trueSheet, trueTable, trueDoc1, trueSheet1, trueTable1, rowsCount1
say “starting”

tell application "Numbers"
	activate
	
	set trueDoc to name of front document
	set trueSheet to name of first sheet of document trueDoc
	set trueTable to name of first table of sheet trueSheet of document trueDoc
	set trueSheet1 to name of third sheet of document trueDoc
	set trueTable1 to name of first table of sheet trueSheet1 of document trueDoc
	
end tell

(* major loop through stock on first sheet, for each stock find data on sheet 3 and copy 3 cells to 
	   first sheet *)
repeat with y from 2 to 31
	tell application "Numbers" to tell document trueDoc to tell sheet trueSheet to tell table trueTable
		tell row (y)
			set stockSym to get value of cell (1)
		end tell
	end tell
	
	(* now find it on last table *)
	tell application "Numbers" to tell document trueDoc to tell sheet trueSheet1 to tell table trueTable1
		set rowsCount1 to row count
	end tell
	repeat with x from 2 to rowsCount1
		tell application "Numbers" to tell document trueDoc to tell sheet trueSheet1 to tell table trueTable1
			tell row (x)
				set parms to get value of cell (1)
				set setEX to get value of cell (7)
				set annDiv to get value of cell (4)
				set nextPay to get value of cell (9)
				if parms = stockSym then
					tell application "Numbers" to tell document trueDoc to tell sheet trueSheet to tell table trueTable
						tell row (y)
							set value of cell (10) to annDiv
							set value of cell (16) to setEX
							set value of cell (17) to nextPay
						end tell
					end tell
				end if
			end tell
		end tell
	end repeat
end repeat

say "dividend information done"

end run

(1) I ran the script with no change on a document whose tables had 41 rows.

It did the job in 24 seconds with no problem.

How many rows have your own tables ?

(2) To check that my memory was right, I ran the script a second time but I quickly clicked the Stop button - the black square between the red circle and the black triangle.
The script stopped politely saying error “Cancelled by user.” number -128

So I am really puzzled by what you describe.

Yvan KOENIG (VALLAURIS, France) mercredi 29 avril 2015 18:39:20

Wow that is odd. My first page has 31 rows. The third page has 40. Maybe I will have to re-enter the script to make sure some unprintable character isn’t there? But to put into the reply I did a copy and paste so I would have thought you would run into the same error that said I couldn’t close the script as it was still running.

MANY THANKS for taking the time to run a test. (Coding mysteries drive me crazy.)

newbiejoan.

Are you saying the script hangs without reaching the ‘say “dividend information done”’ line, or that it finishes but you can’t close the script document in Script Editor afterwards?

yes, It does complete the say " dividend done" so I think it is completed but when I go to close the script file I get error message that it can’t be closed because it is still running. But I do hear the say.

One last oddity. The script worked fine until recently. I don’t know if there have been script editor updates, but I have the latest Yosemite, and there have been recent updates to it.

Hi newbiejoan.

I’m afraid I too have no idea why your script document won’t close after it’s run. Not with that error message anyway. Script Editor does have a few problems at the moment, thanks to various stupid ideas in its own implementation and in the system, but I can’t think of anything that would make it think a script was still running after the run was completed. You could try explicitly saving the script before running it, to make sure it wasn’t in an “Edited” state during the run.

Another possible (but unproven) factor is that, as written, your script has to work quite hard. The ‘x’ repeat performs 30 * (rowsCount1 - 1) * 4 individual cell reads from the third sheet of the Numbers document. The ‘y’ repeat gets the third sheet’s ‘row count’ 30 times, as well as performing that many individual cell reads. Perhaps an opportunity for a log jam either in Numbers itself or in the Apple events flying back and forth between Script Editor and it. (There’s also an undesirable listing of ‘tells’ to tables in different sheets.) But I don’t see how the last ‘say’ line would be executed in that case ” unless there’s an ‘ignoring application responses’ statement in the script which you haven’t disclosed.

Still, I’ve taken the liberty of revamping the script to make it more efficient. I don’t know if this’ll cure the problem, but it may be a little faster (assuming my version works properly ” I haven’t been able to test it thoroughly without your data).

(* 
AppleScript for Numbers -  a numbers document has been opened. Now gets stock names from column 1 of row 2 to row 31 of the  document's first sheet, first table containing stock symbols in the column . Use each symbol on this first sheet to loop through the symbols on the third sheet; find the symbol and move the dividend data from cells on third sheet to cells on the first sheet 
*)
property theApp : "Numbers"

on run
	-- Doing everything inside an ordinary handler makes all the variables local (and therefore non-persistent) by default.
	main()
end run

on main()
	say "starting"
	
	tell application "Numbers"
		activate
		
		set trueDoc to name of front document
		set trueSheet to name of first sheet of document trueDoc
		set trueTable to name of first table of sheet trueSheet of document trueDoc
		set trueSheet1 to name of third sheet of document trueDoc
		set trueTable1 to name of first table of sheet trueSheet1 of document trueDoc
		
	end tell
	
	-- Read all the values from the relevant columns into AppleScript lists beforehand.
	-- Getting them in bulk is faster than getting them from Numbers individually.
	-- Repeatedly getting them from the lists will be faster than repeatedly asking Numbers for them.
	tell application "Numbers"
		tell document trueDoc
			tell table trueTable of sheet trueSheet
				set stockSymList to value of cells 1 thru 31 of column 1
			end tell
			tell table trueTable1 of sheet trueSheet1
				set parmsList to value of cells of column 1
				set setEXList to value of cells of column 7
				set annDivList to value of cells of column 4
				set nextPayList to value of cells of column 9
			end tell
		end tell
	end tell
	
	-- Get sheet 3's row count (or the number of values in parmsList) once only.
	set rowsCount1 to (count parmsList)
	
	(* major loop through stock from first sheet, for each stock find data from sheet 3 and copy 3 cells to first sheet *)
	repeat with y from 2 to 31 -- List index = sheet 1 row number.
		-- Get a stock symbol from the list representing column 1 in the first sheet.
		set stockSym to item y of stockSymList
		
		(* now find it in parmsList (values from column 1 of table 1 of last sheet) *)
		repeat with x from 2 to rowsCount1 -- List index = sheet 3 row number.
			set parms to item x of parmsList
			if (parms = stockSym) then
				-- Only get this "row"'s other three values if the stock symbol matches.
				set setEX to item x of setEXList
				set annDiv to item x of annDivList
				set nextPay to item x of nextPayList
				
				-- Set the relevant cells in the first sheet.
				tell application "Numbers"
					tell document trueDoc
						tell row y of table trueTable of sheet trueSheet
							set value of cell (10) to annDiv
							set value of cell (16) to setEX
							set value of cell (17) to nextPay
						end tell
					end tell
				end tell
				-- Exit the inner repeat, since we've now matched this particular symbol.
				exit repeat
			end if
		end repeat
	end repeat
	
	say "dividend information done"
end main

Wow. Thanks for all the help. It did run faster, and no more error message. PLUS ,and this is a big plus , it gave me a lesson on apple scripting. I don’t have a lot of application for apple script, but my husband was wanting some data and I wanted to learn apple script so this is my second script. Not sure when I will think up another use.