Accessing cell properties in iWork Numbers - applescript

I'm trying a access the complete reference for a cell in Applescript. So far I've managed to get the cell reference and the table reference using a script like:
tell application "Numbers"
tell document 1
repeat with i from 1 to count of sheets
tell sheet i
repeat with j from 1 to count of tables
tell table j
try
set currentCell to the first cell of the selection range
return name of currentCell
end try
end tell
end repeat
end tell
end repeat
end tell
end tell
I can't seem to get the same structure to work for getting the sheet or the document reference. I have tried accessing the properties of the cell and I get something like:
{column:column "A" of table "Table 1" of sheet "Sheet 1" of document "Untitled" of
application "Numbers", alignment:center, value:0.0, background color:{59111, 59111,
59111}, text color:{0, 0, 0}, font size:10.0, vertical alignment:top, name:"A1",
class:cell, font name:"HelveticaNeue", format:automatic, row:row "1" of table "Table
1" of sheet "Sheet 1" of document "Untitled" of application "Numbers", text
wrap:true}
The column property of a cell therefore seems to include the complete reference but if I access the reference directly through the column property.
Can anyone give me some guidance as to how I get the sheet and document using applescript.
Cheers
Ian

Found the solution, fairly simple as long as the code is in the right order:
tell application "Numbers"
tell document 1
repeat with i from 1 to count of sheets
tell sheet i
repeat with j from 1 to count of tables
try
tell table j
set currentCell to the first cell of the selection range
set therow to row of currentCell
set sheetNumber to i
end tell
end try
end repeat
end tell
end repeat
return name of sheet sheetNumber
end tell
end tell
Can use similar code to check for the document number

Related

AppleScript Numbers: Iterate through range of cells

I have an AppleScript that I wrote for Numbers a few years ago that used to work. Since then, I've upgraded my OS (probably more than once), which I'm sure also upgraded Numbers and other aspects of the system. My current environment is:
MacOS 12.2.1 (Monterey)
Numbers 11.2
The script appears to be failing (i.e., not doing what I want) when trying to iterate through the range of pre-selected cells:
...
tell application "Numbers"
set activeSheet to active sheet of front document
set scriptStart to current date
tell table 1 of activeSheet
set selRng to cells of selection range
set cellCount to count of selRng
repeat with currentCell in cells of the selRng
my processCell(currentCell)
end repeat
end tell
set scriptEnd to current date
beep
display dialog "DONE: " & my formatTime()
end tell
...
I know from debugging statements (not shown) in the loop that it never gets there and so repeat with currentCell in cells of the selRng is not the correct way to loop through the selRng (which I've also verified contains eight cells).
I'm going to continue trying out different things by searching the web for (good) examples, but if someone here can provide pointers it might save me a lot of time...
Like I said, this used to work...
UPDATE-1: As with my previous posting, I've seemed to solve the initial question, but then the problem just morphs into something else. The loop changed to:
repeat with currentCell in selRng
my processCell(currentCell)
end repeat
OR:
repeat with currentCell in selRng
set cellName to name of currentCell
my processCell(cellName)
end repeat
In the first case, I end up with currentCell being set to:
item 1 of {cell "D122" of table 1 of sheet 2 of document id "66DF865E-D1EC-4E4A-8A0E-E66213CF0E76", cell "E122" of table 1 of sheet 2 of document...}
Which doesn't work because it isn't a cell, it still looks like a range.
In the second case, I end up with a string "D122" which doesn't work for the called routine that expects an actual cell to be passed to it.
So now I either have to figure out how to just get the actual first cell in the range (first coding) or how to convert the string to an actual cell or cell reference (second coding)...
UPDATE-2:
If I change the repeat loop to:
repeat with i from 1 to count of selRng
set currentCell to cell i
my processCell(currentCell)
end repeat
currentCell is set to A1 - not what I want.
If I change it to:
repeat with i from 1 to count of selRng
set currentCell to cell i of selRng
my processCell(currentCell)
end repeat
I get an error "Can’t get cell 1 of {cell "K122" of table 1 of sheet 2 of document id "66DF865E-D1EC-4E4A-8A0E-E66213CF0E76" of application "Numbers"}."
So the first case gives me the wrong cell, and the second case appears to leave me where I was before, trying to extract the i'th cell element of the range. (Arrgh :-))
Well, I figured it out. The new code is:
tell application "Numbers"
set activeSheet to active sheet of front document
set scriptStart to current date
tell table 1 of activeSheet
set selRng to cells of selection range
repeat with i from 1 to count of selRng
set currentCell to item i of selRng
my processCell(currentCell)
end repeat
end tell
set scriptEnd to current date
beep
display dialog "DONE: " & my formatTime()
end tell
Note the use of item I of selRng instead of cell I of selRng

Applescript one window resizes, the other doesn't

I'm getting an unusual result when resizing windows via applescript.
I'm trying to set the position and size of Excel's windows but in one window's case it works and in another it doesn't.
Here's the code:
tell application "System Events"
get the name of every application process whose class of windows contains window
repeat with P in the result
get (every window of process (contents of P) whose value of attribute "AXMinimized" is false)
repeat with W in the result
set win_name to name of W
log P & ":" & win_name
if (win_name is equal to "VBA-Web - Blank") then
log "CASE1"
tell W
set {position, size} to {{0, 0}, {1100, 500}}
end tell
end if
if (win_name is equal to "Microsoft Visual Basic - VBA-Web - Blank.xlsm - [ThisWorkbook (Code)]") then
log "CASE2"
tell W
set {position, size} to {{0, 0}, {1120, 520}}
end tell
end if
end repeat
end repeat
end tell
In the code above, CASE1 works fine, but CASE2 does nothing.
I suspect the VBA editor window is somehow different... but how?
Any suggestions on what I can try to fix this?

Can I read/write the name of the Illustrator artboard in Applescript

Is it possible to get the name of an Illustrator artboard in Applescript?
This script works perfectly until I try to get the name of the artboard:
tell application "Adobe Illustrator"
tell document 1
set artboards_count to count of artboards
set c to 1
repeat while c <= artboards_count
log index of artboard c as text
log artboard rectangle of artboard c as text
log name of artboard c as text -- this line fails
set c to c + 1
end repeat
end tell
end tell
the line log name of artboard c as text fails - everything else works ok.
The message is:
Adobe Illustrator got an error: Can’t get name of artboard 1 of document 1. (-1728)
Any idea as to why?
Setting the name fails too, BTW. However if I do
tell application "Adobe Illustrator"
tell document 1
return properties of artboard 1
end tell
end tell
I get (carriage returns added for clarify):
artboard rectangle:0.0, 0.0, 841.889999999999, -595.280000000001,
ruler PAR:1.0, show center:false, show cross hairs:false,
show safe areas:false, ruler origin:0.0, 0.0, name:Artboard 1,
container:document 1, best type:reference, default type:reference,
class:artboard, index:1
from which one would think the property nameshould be there.
The name property is read-only so there is no way to change it once the artboard exists. And even though you can get the properties of an artboard, you can’t convert it to a string if you simply want to target a particular artboard. But there is a way to do it nonetheless that I discovered by accident. Suppose you have several artboards and want to target the artboard named "Squash this". Here's how to do that:
Tell application "Adobe Illustrator"
tell current document
set artCount to number of artboards
repeat with i from 1 to artCount
set artProp to get properties of artboard i
try
set propString to artProp as string --this will fail
on error
set errorDisp to text of result --this captures the text of the error
set errorDispText to errorDisp as string --changes the text to a ¬
searchable string
end try
if errorDispText contains "quash" then
display notification "errorDispText" --oddly enough, this displays just ¬
the artboard name
exit repeat
end if
end repeat
end tell
end tell
The best solution could be coercion; take the record returned from the Artboard properties and coerce the record into a list. So, your name property from the record becomes the 7th item in the list. The code below will get Artboard dimensions, name and index, and place them into a list for later use in your script.
Hope this helps!
tell application "Adobe Illustrator"
set artboardDetails to {}
tell front document
set allArtboards to every artboard
repeat with i from 1 to count of allArtboards
set thisArtboard to item i of allArtboards
set thisArtboardProps to properties of thisArtboard
set thisArtboardDimensions to artboard rectangle of thisArtboardProps
set thisArtboardIndex to index of thisArtboardProps
--WORKAROUND
set thisArtboardPropsCoerce to thisArtboardProps as list
set thisArtboardName to item 7 of thisArtboardPropsCoerce
set the end of artboardDetails to {thisArtboardName, thisArtboardIndex, thisArtboardDimensions}
end repeat
end tell
end tell
(*
--WORKAROUND
The following is a workaround for error returned from:
set anArtboardName to the name of anArtboard -- this line will not return a result, just errors out
BUT WE NEED TO WATCH OUT FOR THE COERCED LIST!
So, we have to coerce the properties record of artboard to a list first
WEIRD I KNOW!
The coercion then changes the record of 11 items to a list of 12 items
set anArtboardProps to properties of anArtboard as list
set anArtboardName to item 7 of anArtboardProps
NOW LETS MATCH UP THE COERCION ITEMS
The first lines are from the record properties, and the second lines are from the coerced to list versions.
01. artboard rectangle:{0.0, 768.0, 1366.0, 0.0},
01. {0.0, 768.0, 1366.0, 0.0},
02. ruler PAR:1.0,
02. 1.0,
03. show center:false,
03. false,
04. show cross hairs:false,
04. false,
05. show safe areas:false,
05. false,
06. ruler origin:{0.0, 0.0},
06. {0.0, 0.0},
07. name:"Artboard 1",
07. "Artboard 1",
08. container:document 1,
08. document 1,
09. best type:reference,
09. reference,
10. default type:reference,
10. reference,
11. index:1
11. artboard,
12. NULL - nothing in the record
12. 1 - so this line is the index of the artboard
*)
Here is my workaround. As the «class bAl9» vanishes every time you compile, you have to copy/paste every time, but it works. And yes, «class bAl9» will turn to "name" and will not compile correctly the next time. Thanks Adobe !
tell application "Adobe Illustrator"
tell document 1
repeat with x in (every artboard)
log index of x as text
log artboard rectangle of x as text
log «class bAl9» of x as text -- artboard's name property = «class bAl9»
end repeat
end tell
end tell
The name property of artboards is read/write, it's easy to rename artboards this way once you get the trick.
Edit : For the loop in a list, I always use the repeat with x in l statement. It's smart and fast.
Stu's answer was the best that I found. Coercing the record to a list is brilliant – I didn't even know you could do that. My code was:
repeat with i from 1 to the (count of AllArtBoards)
set ThisArtBoard to item i of AllArtBoards
set ThisArtBoardName to item 7 of ((properties of ThisArtBoard) as list)
set the end of AllArtBoardNames to ThisArtBoardName
end repeat

Set value of an AXDateTimeArea

I want to set the value of an AXDateTimeArea GUI element via Applescript. Attached is a screenshot of the Accessibility Inspector showing the element.
This is the code that I tried. No errors are thrown, but the value of the element doesn't change.
set value of attribute "AXValue" of tElement to "2012-06-21 13:45:18 +0000"
Additionally I tried the following lines but no success nor an error thrown.
set value of tElement to "2012-06-21 13:45:18 +0000"
set value of tElement to (current date)
set value of attribute "AXValue" of tElement to (current date)
This is always within a loop over the content of the sheet/window:
tell application "System Events"
tell process "myprocess"
set tElements to entire contents of (get sheet 1 of window 1)
repeat with tElement in tElements
end repeat
end tell
end tell
This is a screenshot:

Get a row in applescript by AXStaticText

value of static text of row 1 of (table 1 of some scroll area)
--> {"Wi-Fi, Connected"}
select (row of (table 1 of some scroll area)
whose (value of static text) begins with "Wi-Fi")
error "Can’t get attribute \"AXValue\" of static text."
number -1728 from «class attr» "AXValue" of «class sttx»
While I can get the value, why I can't use it as a filter in 'whose' clause?
Thanks.
value of static text seems to return a list like {"Wi-Fi, Connected"}. Try using a repeat loop that coerces the list to text:
tell application "System Events" to tell process "System Preferences"
repeat with r in rows of table 1 of scroll area 1 of window 1
if (value of static text of r as text) starts with "Wi-Fi" then return r
end repeat
end tell

Resources