Accessing Numbers cell values from Swift using Scriptingbridge

I'm writing some routines to move data in and out of Numbers tables using Swift and Scriptingbridge. Most of it is working OK but I am struggling to read cell values which is kind of the point of the exercise. I can read and write other properties, like the cell's row/column, as expected but if I use:

var x = cell.value

I get a reply:

Scripting test[9307:928082] -[SBObject copyWithZone:]: unrecognized selector sent to instance 0x600000d15f50

If I try:

        cell?.value = "21"

I get the error:

Cannot assign to property: 'cell' is immutable'

I randomly tried:

        cell?.setValue!("21")

and it worked, the cell updated in the table. I tried:

cell?.getValue!()

which didn't throw an error but always returns nil. I tried:

        var cellObject = cell as! SBObject
        var x = cellObject.property(withCode: 0x4E4D4376)

(where 0x4E4D4376 is the AEKeyword for a Numbers cell value) which returns:

<SBObject @0x600000d176c0: value of <class 'NmCl'> 1 of cellRange of <class 'NmTb'> 0 of <class 'NmSh'> 1 of <class 'docu'> 0 of application "Numbers" (3184)>

which looks like the correct reference but there doesn't seem to be a way to get the actual value from that.

I can read and write all the other properties I've tried and used methods so something's Not Quite Right here. As it stands I can write to cells using a workaround but not read, does anyone know of any workarounds?

Thanks in advance!
        


I'm writing some routines to move data in and out of Numbers tables
using Swift and Scriptingbridge.

Have you already got this working from AppleScript? If so, can you post an example of that?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Should have added, yes Applescript is working fine but I want to do the processing in Swift:

tell selection range
set {j1, j2} to {column 1's address, column -1's address}
set myResult to rows's cells j1 thru j2's value -- 2d-array for the range
--rows's cell j1's value -- 1d-array for 1st column in the range
set myCount to 1
set myRow to {}
set myArray to {}
repeat with myData in myResult
set the end of myRow to item 1 of myData
set myCount to myCount + 1
if (myCount) is greater than 10 then
set myCount to 1
set the end of myArray to myRow
set myRow to {}
end if
end repeat
get myArray
end tell

then to write:
set value of cell columnNumber of row rowNumber to myValue


ScriptingBridge is a tricky bit of tech because it tries to bridge the Apple event and Cocoa object models. My experience is that it doesn’t do a great job of this, and that it’s better to tackle problems like this in one of two alternative ways:
  • Send your own Apple events (A)

  • Embed the AppleScript within your app and run it using NSAppleScript (B)

In this case B would be better than A. Remember that you can use executeAppleEvent(_:error:) to pass parameters to, and receive results from, the AppleScript.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Accessing Numbers cell values from Swift using Scriptingbridge
 
 
Q