Syntax Error: AppleScript for Numbers

when I execute AppleSript below, Syntax Error occurred.


on run {input, parameters}


tell application "Numbers"


activate


tell the first table of the active sheet of document 1


repeat with i from 1 to the count of cells of the selection range


set the value of cell i of the selection range to (random number from 1 to 100)


end repeat


end tell


end tell


end run



I'm a totally beginner for iWork Automation and appreciate any comment or advice.

MacBook Pro 13″, macOS 11.3

Posted on May 15, 2021 5:04 PM

Reply
Question marked as Top-ranking reply

Posted on May 15, 2021 11:01 PM

Edited my original post with a better revision.


What is happening is when you set the value of the first cell, it changes the selection range to just that one cell. When you try to do cell 2, there is no cell 2.


A change to your code so it would work would be like this:


tell application "Numbers"	
	activate
	tell the first table of the active sheet of document 1
		set myRange to selection range
		repeat with i from 1 to the count of cells of myRange
			set the value of cell i of myRange to (random number from 1 to 100)
		end repeat
	end tell
end tell


Note that you are assuming the selection range will be in the first table of the sheet. Another way to do it would be this:


tell application "Numbers" to tell front document
	tell active sheet
		tell (first table whose class of selection range is range)
			repeat with c in (get selection range)'s cells
				set value of c to (random number from 1 to 100)
			end repeat
		end tell
	end tell
end tell



2 replies
Question marked as Top-ranking reply

May 15, 2021 11:01 PM in response to toga86

Edited my original post with a better revision.


What is happening is when you set the value of the first cell, it changes the selection range to just that one cell. When you try to do cell 2, there is no cell 2.


A change to your code so it would work would be like this:


tell application "Numbers"	
	activate
	tell the first table of the active sheet of document 1
		set myRange to selection range
		repeat with i from 1 to the count of cells of myRange
			set the value of cell i of myRange to (random number from 1 to 100)
		end repeat
	end tell
end tell


Note that you are assuming the selection range will be in the first table of the sheet. Another way to do it would be this:


tell application "Numbers" to tell front document
	tell active sheet
		tell (first table whose class of selection range is range)
			repeat with c in (get selection range)'s cells
				set value of c to (random number from 1 to 100)
			end repeat
		end tell
	end tell
end tell



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Syntax Error: AppleScript for Numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.