Search code examples
xcodecocoaapplescriptapplescript-objc

Parse NSAppleEventDescriptor in AppleScriptObjC


I am trying to write an AppleScript application for the Automator to manipulate Contact groups. I am receiving a contact item as input to the script but I'm stuck at how to parse it. Here's the source I've written so far:

script Labels_for_contact_groups
    property parent : class "AMBundleAction"

    on runWithInput_fromAction_error_(input, anAction, errorRef)
        -- Get parameters
        set labelName to valueForKey_("trainingLabelName") of parameters() of me
        set labelValue to valueForKey_("trainingLabelValue") of parameters() of me

        -- Iterate over the contact groups
        -- what should I do here?
        log input

        return input
    end runWithInput_fromAction_error_
end script

I get the following in the logs:

2016-10-12 11:35:28.061 Automator[2167:174553] <NSAppleEventDescriptor: [ 'obj '{ 'want':'azf5', 'form':'ID  ', 'seld':'utxt'("50E8C441-3DF0-4CD7-8E36-E175B37D2CCB:ABGroup"), 'from':[0x0,10d10d "Contacts"] } ]>

What I need to do is extract the information of all contacts in the specified group. How do I proceed?

Edit: I added the following lines

tell application "Contacts" to set thePeople to people of input
repeat with i from 1 to number of items in thePeople
        set thisPersonCurrent to item i of thePeople
        log thisPersonCurrent
end repeat

Now I get the following error:

[Labels_for_contact_groups runWithInput:fromAction:error:]: Can’t get every «class azf4» of «class ocid» id «data optr0000000080A9220080600000». (error -1728)

What am I doing wrong?


Solution

  • What I was trying to achieve did not work through the Automator. I realised there are numerous gaps in the API. What finally worked was a standalone AppleScript with the following line:

    tell application "Contacts"
      set theGroupNames to name of groups
      set text_returnedCurrent to choose from list theGroupNames with prompt "Select Group" without multiple selections allowed
      set the_peopleCurrent to people of group (text_returnedCurrent as text)
      ...