Use a reserved word as a record label

I decided to rewrite a frequently-used script. This turned out to be a bad idea and I’ve gone back to the original version. However, it raised a question about the use of a reserved word in a record, and I wondered if there’s a way to use Numbers as a label in the following script:

tell application "System Events" to set activeApp to name of first process whose frontmost is true

set windowData to "{Finder:{375, 33, 1170, 700}, FSNotes:{560, 33, 800, 680}, Mail:{480, 33, 960, 855}, Numbers:{1092,23,828,1057}, Preview:{460, 33, 1000, 1037}, Safari:{160, 23, 1600, 1057}, Soulver:{1410, 33, 500, 450}}"

try
	set {x, y, w, h} to (run script activeApp & " of " & windowData)
on error
	set {x, y, w, h} to {510, 33, 900, 750}
end try

tell application "System Events" to tell process activeApp to tell window 1
	set position to {x, y}
	set size to {w, h}
end tell

I thought enclosing Numbers in pipes in the record would do the job but it didn’t. The easy workaround was rewrite the script to use Numbers_App instead of Numbers in the script, but I’m still curious. I also wanted to use Script Editor as a label but that didn’t work because of the space.


set windowData to {Finder:{375, 33, 1170, 700}, FSNotes:{560, 33, 800, 680}, Mail:{480, 33, 960, 855}, |Numbers|:{1092, 23, 828, 1057}, Preview:{460, 33, 1000, 1037}, Safari:{160, 23, 1600, 1057}, Soulver:{1410, 33, 500, 450}}
|Numbers| of windowData

behave flawlessly

Were you trying to extract the property from the string defined in your code ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 31 mars 2020 20:56:05

Thanks Yvan. The following better demonstrates the issue. It errors with “Numbers” in the first line but works OK if “Numbers” is replaced with “Finder” (or any of the other labels).

set activeApp to "Numbers"

set windowData to "{Finder:{375, 33, 1170, 700}, FSNotes:{560, 33, 800, 680}, Mail:{480, 33, 960, 855}, |Numbers|:{1092,23,828,1057}, Preview:{460, 33, 1000, 1037}, Safari:{160, 23, 1600, 1057}, Soulver:{1410, 33, 500, 450}}"

set {x, y, w, h} to (run script activeApp & " of " & windowData) --> "Can’t get item 1 of {}." number -1728 from item 1 of {}

Would work better if you ask for the true key which isn’t “Numbers” but “|Numbers|” :rolleyes:

set activeApp to "|Numbers|"

set windowData to "{Finder:{375, 33, 1170, 700}, FSNotes:{560, 33, 800, 680}, Mail:{480, 33, 960, 855}, |Numbers|:{1092,23,828,1057}, Preview:{460, 33, 1000, 1037}, Safari:{160, 23, 1600, 1057}, Soulver:{1410, 33, 500, 450}}"

set {x, y, w, h} to (run script activeApp & " of " & windowData) --> "Can’t get item 1 of {}." number -1728 from item 1 of {}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 31 mars 2020 22:34:22

Thanks Yvan.

This works as expected:


tell application "System Events" to set activeApp to name of first process whose frontmost is true

set windowData to "{|Script Editor|:{375, 33, 1170, 700}, Finder:{375, 33, 1170, 700}, FSNotes:{560, 33, 800, 680}, Mail:{480, 33, 960, 855}, |Numbers|:{1092,23,828,1057}, Preview:{460, 33, 1000, 1037}, Safari:{160, 23, 1600, 1057}, Soulver:{1410, 33, 500, 450}}"

try
	set {x, y, w, h} to (run script activeApp & " of " & windowData)
on error
	try
		set {x, y, w, h} to (run script "|" & activeApp & "|" & " of " & windowData)
	on error
		set {x, y, w, h} to {510, 33, 900, 750}
	end try
end try

tell application "System Events" to tell process activeApp to tell window 1
	set position to {x, y}
	set size to {w, h}
end tell

Thanks KniazidisR–that is perfect. :slight_smile:

Where I got stuck was the differing treatment of record labels with and without pipes and you did that with an additional level of error correction, which never would have occurred to me.