AppleScript Numbers: Iterate through range of cells - applescript

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

Related

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

Excel - More efficient way to remove specific borders

How can I make the below code more efficient, at the moment it takes roughly 30 seconds to complete
The code will clear all cells within a selection which have a white background & then clear all diagonal borders within a selection.
With only the clear cells with white background code it finishes instantly but as soon as I added the remove borders code it became slow.
Sub ADULTClearOnly()
Set sh2 = ThisWorkbook.Worksheets("ADULT Sign On Sheet")
sh2.Select
Cells.Range("B1:F756").Select
For Each Cell In Selection
If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
Cell.Value = ""
End If
Next
Cells.Range("G1:AO757").Select
For Each Cell In Selection
If Cell.Borders(xlDiagonalUp).LineStyle = xlDiagonalUp Then
Cell.Borders(xlDiagonalUp).LineStyle = xlNone
End If
Next
End Sub
There are several issues in your code:
You don't need to, and should not Select to do these actions
Your logic is flawed: xlDiagonalUp is not a LineStyle
Because you are setting any Diagonal Up borders to none, you don't need to iterate the range, do it in one step
So, replace
Cells.Range("G1:AO757").Select
For Each Cell In Selection
If Cell.Borders(xlDiagonalUp).LineStyle = xlDiagonalUp Then
Cell.Borders(xlDiagonalUp).LineStyle = xlNone
End If
Next
with
sh2.Range("G1:AO757").Borders(xlDiagonalUp).LineStyle = xlNone
Similarly,
sh2.Select
Cells.Range("B1:F756").Select
For Each Cell In Selection
If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
Cell.Value = ""
End If
Next
can be reduced to
For Each Cell In sh2.Range("B1:F756")
If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
Cell.ClearContents
End If
Next

Change view of specific split view

I'm looking to have my excel sheet with a split in it (vertically) ensuring a set of controls stay on the left of the screen for easy access. Currently Im using this code to select and move to a cell, based on a list of headings I have in the A column.
Option Explicit
Dim trimProcess() As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
If Not Intersect(ActiveCell, Range("A6:A1000")) Is Nothing Then
If ActiveCell.Value <> "" Then
For i = 0 To UBound(trimProcess)
If ActiveCell.Value = trimProcess(i) Then
Cells(4, 4 * (i + 1)).Select
Cells(4, 4 * (i + 1)).Activate
End If
Next
End If
End If
End Sub
This works fine for what I need, but it only works in the active split view IE if I click a cell in A in the left split, it moves the left split view. I want it so that the changes only the right view, but cant find the code to do so. Is this possible?

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:

Accessing cell properties in iWork Numbers

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

Resources