Thursday, July 9, 2020

#1 2020-03-31 12:28:49 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 535

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:

Applescript:

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.

Last edited by peavine (2020-04-03 09:22:59 am)


2018 Mac mini - macOS Catalina

Offline

 

#2 2020-03-31 12:56:13 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4546

Re: Use a reserved word as a record label

Applescript:


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

Offline

 

#3 2020-03-31 02:16:37 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 535

Re: Use a reserved word as a record label

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).

Applescript:

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 {}

Last edited by peavine (2020-03-31 02:24:10 pm)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-03-31 02:34:28 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4546

Re: Use a reserved word as a record label

Would work better if you ask for the true key which isn't "Numbers" but "|Numbers|" roll

Applescript:

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

Offline

 

#5 2020-03-31 03:23:33 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 535

Re: Use a reserved word as a record label

Thanks Yvan.


2018 Mac mini - macOS Catalina

Offline

 

#6 2020-04-01 02:53:06 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Use a reserved word as a record label

peavine wrote:

I also wanted to use Script Editor as a label but that didn't work because of the space.


This works as expected:

Applescript:


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

Last edited by KniazidisR (2020-04-01 04:30:03 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-04-01 07:43:31 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 535

Re: Use a reserved word as a record label

Thanks KniazidisR--that is perfect. 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.

Last edited by peavine (2020-04-02 11:32:59 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)