Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Failed attempt to define a range using AppleScript

When setting an iWork range name using row numbers:


tell application "Numbers"
	activate
	tell document 1
		tell sheet 1
			tell table "test"
				set rangename to "3:4"
				set selection range to range rangename
			end tell
		end tell
	end tell
end tell

works fine

BUT changing the statements inside the tell blocks to:

				set a to "3"
				set b to "4"
				set rangename to "\"" & a & ":" & b & "\""
				log rangename
				set selection range to range rangename


—> (*"3:4"*)


set selection range of table "test" of sheet 1 of document 1 to range "\"3:4\"" of table "test" of sheet 1 of document 1

--> error "The range value cannot be retrieved." number -10000 from range "\"3:4\"" of table "test" of sheet 1 of document 1


My question is "Why does the Event Handler fail?" The log message suggests that the range name has been constructed correctly. 


As shown in iworkautomation.com/numbers/  there is another way to construct  rangename  :


set rangename to ((name of first cell of row a) & ":" & (name of last cell of row b))


but  I’m wondering about the reason the previous attempt failed as I suspect it might illuminate a gap in my understanding  of  AppleScript.





MacBook Pro 15″, macOS 10.14

Posted on Sep 12, 2022 10:34 PM

Reply
Question marked as Best reply

Posted on Sep 14, 2022 5:40 AM

To prevent confusion, I would wanted to clarify that


set rangename to "\"" & a & ":" & b & "\""


is equivalent to


set rangename to ""3:4""


In "\"" the \ has the effect of "escaping" the extra quote so AppleScript will accept it.


As I posted above, your original script added an extra set of quotes (not a \ or some other character).


This "escaping" business can sometimes get a little confusing. It's needed for a " character because that character is typically used to surround a string in AppleScript.


range expects a following string.

The concatenation in this:


set rangename to a & ":" & b as text


automatically coerces to a string if a and b are numbers, so you don't need to worry about surrounding it with "" .


SG



Similar questions

8 replies
Question marked as Best reply

Sep 14, 2022 5:40 AM in response to SGIII

To prevent confusion, I would wanted to clarify that


set rangename to "\"" & a & ":" & b & "\""


is equivalent to


set rangename to ""3:4""


In "\"" the \ has the effect of "escaping" the extra quote so AppleScript will accept it.


As I posted above, your original script added an extra set of quotes (not a \ or some other character).


This "escaping" business can sometimes get a little confusing. It's needed for a " character because that character is typically used to surround a string in AppleScript.


range expects a following string.

The concatenation in this:


set rangename to a & ":" & b as text


automatically coerces to a string if a and b are numbers, so you don't need to worry about surrounding it with "" .


SG



Sep 13, 2022 7:51 PM in response to jbh2

jbh2 wrote:

Yes, that works ok, but I'm not understanding why the range name constructed from "3" and "4" fails.


It doesn't fail. Try this:



tell application "Numbers"
	tell document 1
		tell sheet 1
			tell table "test"
				set a to "3"
				set b to "4"
				set rangename to a & ":" & b as text
				set selection range to range rangename
			end tell
		end tell
	end tell
end tell



In your original script you added an extra set of quotes. That is what caused the fail.


SG

Sep 15, 2022 3:56 AM in response to jbh2

Yes, handling of quotes can be tricky in AppleScript. And the "escaping" with a \ can look pretty weird, tripping up even the most experienced. Plus the automatic "coercion" to string on concatenation is not something that seems to be widely documented. Handy, though.


Thanks for the green tick!


SG



Failed attempt to define a range using AppleScript

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