Questions about loops

Hey folks,

Bringing a new question to the table today, I am trying to figure out something that is driving me a bit bonkers and my Google-Fu is failing me at this point so I am hoping someone much more knowledgable than me can point me in a right direction.

I am trying to use AppleScript to read through a table in Numbers (Single column with 10 rows), I can get it to point to the table and can get it to enter some data but I cannot get it to fill in all the data correctly, ideally what I am looking for it to do is to read through the table and see if it contains any data (by default it will be blank) then to prompt to ask for data to be entered (No need to enter this previously if possible to save entering in data when not needed) then to iterate through the list and enter the data into the column.

Here’s what I currently have being used:

tell table Table 1 of sheet Sheet 1
								
	repeat with rowNumber from 1 to 10
									
		try
			set cellValue to value of cell ("A" & rowNumber)
			if cellValue is " " then exit repeat
		on error
			exit repeat
		end try
	end repeat
								
	set Q1 to text returned of (display dialog "Q1" default answer "")
	set Q2 to text returned of (display dialog "Q2" default answer "")
	set Q3 to text returned of (display dialog "Q3" default answer "")
	set Q4 to text returned of (display dialog "Q4" default answer "")
	set Q5 to text returned of (display dialog "Q5" default answer "")
	set Q6 to text returned of (display dialog "Q6" default answer "")
	set Q7 to text returned of (display dialog "Q7" default answer "")
	set Q8 to text returned of (display dialog "Q8" default answer "")
	set Q9 to text returned of (display dialog "Q9" default answer "")
	set Q10 to text returned of (display dialog "Q10" default answer "")
	set valueList to {Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10} -- Table variables for input
								
	repeat with i from 1 to 10
		set value of cell ("A" & rowNumber) to (item i of valueList)
	end repeat
		end tell

For one I am unsure how to get it to stop the repeat if it detects data in the list, then to continue on, so any help from those wiser and more knowledgable is appreciated

Not sure that I fully understand your objective but try this:

Basically, it cycles through the cells in your column. If the cell contains a space then it is skipped — but the user is notified; if it does not, then the user is asked to input the new value for the cell. When completed, the script provides the list of values for the cells.

use scripting additions
tell application "Numbers"
	set d1 to document 1
	set s1 to sheet 1 of d1
	set t1 to table 1 of s1
	
	tell t1
		set valueList to {}
		repeat with rn from 1 to 10
			if value of cell ("A" & rn) is space then
				display dialog "Row " & rn & " is a space" with title "End of the line"
				set end of valueList to space
			else
				set qq to text returned of (display dialog "Q" & rn default answer "")
				set end of valueList to qq
				set value of cell ("A" & rn) to item rn of valueList
			end if
		end repeat
	end tell
end tell
valueList
--> {"1", "2", "3", "4", " ", "6", "7", " ", "9", "10"}

Hey Mockman,

As ever you are a font of knowledge.

The idea behind it is to check whether the table contains data, if it does then it can exit the loop and then continue on through the next segment of code, if it’s empty then prompt a series of questions (names, dates, times etc for specific cells) to build out a list then populate the cells in the table with the information that was provided.

So if any cell in the table contains data then skip everything that’s in your original post, but if every cell is empty, then cycle through them with the ‘q’ dialogues? Is that correct?

A couple of questions… is the data in the cell range all numbers or all text or a combination? Will the ‘empty’ cells be really empty? You use a ‘space’ in your original script. An empty cell should have a missing value. If they are truly empty, then you could do something like this:

tell application "Numbers"
	set d1 to document 1
	set s1 to sheet 1 of d1
	set t1 to table 1 of s1
	
	set cellValues to value of cells of t1
	repeat with v in cellValues
		if contents of v is not missing value then
			set addData to false
			display dialog "False"
			exit repeat
		else
			set addData to true
		end if
	end repeat
	
	if addData then
		-- put q-loop here
	end if
	
end tell

If there is a situation where a cell has some contents but you don’t want it to skip the adding of data, then you’d have to modify the test somewhat.

By the way… I didn’t elaborate in my first post but rather than trying to create a series of variables, just create a list of the values. That’s how it’s handled in my post.