Access User Data Value or Name

I have some code;

tell application “OmniGraffle”
activate
tell canvas of front window
repeat with obj in graphics
set ObjName to id of obj
display dialog "This is the dialog " & ObjName
end repeat
end tell
end tell

This returns the ID of each of my graphics, but what I really want to return is the value of a data item list in the keypairs. I’ve trued numerous ways but with no sucess. Below is an example of what I want to achieve (but doesn’t work).

tell application “OmniGraffle”
activate
tell canvas of front window
repeat with obj in graphics
set ObjName to user data value of obj
display dialog "This is the dialog " & ObjName
end repeat
end tell
end tell

Any help is appreciated. Thanks in advance.

I’ve managed to get a little further, I think the issue is now iterating over the returned JSON looking string. Error and code below;

error “Can’t make properties of {{type:“YES”}, {type:“testg”}, {type:“mysql”}, {type:“linux”}} into type Unicode text.” number -1700 from properties of {{type:“YES”}, {type:“testg”}, {type:“mysql”}, {type:“linux”}} to Unicode text

tell application "OmniGraffle"
    activate
    tell canvas of front window
        repeat with obj in graphics
            set test to user data in graphics
            
            repeat with value in (properties of test) as list
                
                display dialog "This is the dialog " & value
            end repeat
            
            set ObjName to id of obj
            display dialog "This is the dialog " & ObjName
        end repeat
    end tell
end tell

317 days later, some sample code in AppleScript, and in Apple’s newer JavaScript for Automation.

JavaScript records are easier to work with - you can ask them what keys they have.

( AppleScript records don’t know and can’t tell you –
the scripter just has to know in advance what keys will be there ).

CODE SNIPPETS reading and writing the user data key-value pairs of a shape

AppleScript version

on run
  set oShape to firstSelectedGraphic()
  
  tell application "OmniGraffle"
    
    -- WRITE SOME name/key PAIRS TO THE SELECTED SHAPE
    
    set value of (user data item "Name" of oShape) to "Cicero"
    set value of (user data item "Workplace" of oShape) to "Rome"
    set value of (user data item "Role" of oShape) to "Lawyer"
    
    
    -- A BAKED-IN WEAKNESS OF APPLESCRIPT IS THAT THE CODE
    -- CAN'T ASK OR CHECK WHAT KEYS A RECORD HAS ... 
    
    -- (WE JUST HAVE TO KNOW THAT THIS RECORD HAS THE KEY 'letter' ...)
    
    -- 1. We can read all the user data of the shape into a record
    --    and then pick out particular values by keyname, OR
    set recData to user data of oShape
    set valueThruRecData to |Name| of recData
    
    -- 2.  we can read the value of particular user data items
    --     BUT, in Applescript we have to **know** the keynames
    --     we can ask applescript what user data keys this shape has ...
    set valueDirectly to value of user data item "Name" of oShape
    
    return {valueThruRecData, valueDirectly}
  end tell
end run


on firstSelectedGraphic()
  tell application "OmniGraffle"
    set oSeln to null
    
    set lstWins to windows whose its document is not null
    if length of lstWins > 0 then
      tell (item 1 of lstWins)
        set lstSeln to (its selection as list)
        if length of lstSeln > 0 then
          set oSeln to item 1 of lstSeln
        end if
      end tell
    end if
    
    return oSeln
  end tell
end firstSelectedGraphic

JavaScript for Automation version (various names - JSA, JXA, or just osascript JS)

function run() {
  var og = Application("OmniGraffle");

  var shp = firstSelectedGraphic(),
    lstPairs = [
      ['Name', 'Cicero'],
      ['Workplace', 'Rome'],
      ['Role', 'Lawyer']
    ],
    nameValuePair = shp.userDataItems;

  lstPairs.forEach(
    function (pair) {
      nameValuePair[
        pair[0]
      ].value = pair[1];
    }
  )

  var objUserData = shp.userData();

  return Object.keys(objUserData) // ask for list of keys in the object
    .map(function (key) {
      return key + ' -> ' + objUserData[key];
    });
    
    

  function firstSelectedGraphic() {
    var ws = og.windows.whose({
        _not: [{
          document: null
      }]
      }),
      w = ws.length ? ws.at(0) : undefined,
      slns = w ? w.selection() : [];

    return slns.length > 0 ? slns[0] : null;
  }
};