Add Dictionary in Array with defaults

I want to modify a LaunchAgent in code. The agent needs a dictionary in an array. I can do a dictionary or an array. But how do I add the dictionary to the array?

This is the result I want:

Something structured more or less like this?

<dict>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>1</integer>
            <key>Minute</key>
            <integer>5</integer>
       </dict>
        <dict>
            <key>Hour</key>
            <integer>7</integer>
            <key>Minute</key>
            <integer>3</integer>
       </dict>
    </array>
</dict>

Hmmm I see it in man as:

 StartCalendarInterval <dictionary of integers or array of dictionary of
     integers>
     This optional key causes the job to be started every calendar interval as
     specified. Missing arguments are considered to be wildcard. The semantics
     are much like crontab(5).  Unlike cron which skips job invocations when
     the computer is asleep, launchd will start the job the next time the com-
     puter wakes up.  If multiple intervals transpire before the computer is
     woken, those events will be coalesced into one event upon wake from
     sleep.

           Minute <integer>
           The minute on which this job will be run.

           Hour <integer>
           The hour on which this job will be run.

           Day <integer>
           The day on which this job will be run.

           Weekday <integer>
           The weekday on which this job will be run (0 and 7 are Sunday).

           Month <integer>
           The month on which this job will be run.

For one StartCalendarInterval entry it should be just:

<dict>
    <key>StartCalendarInterval</key>
    <dict>
          <key>Hour</key>
          <integer>1</integer>
          <key>Minute</key>
          <integer>5</integer>
    </dict>
</dict>

Cough:

Some users need the entry, some users don’t. And I need different intervals.

Cough:

I gave you all the info you need for any case.

Hu? I can see the plist myself. But how do I use the defaults command to edit the plist so that I get the dictionary into the array? For sure I’m not going to add the data to the file without API or command.

Why not? That’s how I do it myself.

MBS has a class for parsing plist
https://www.monkeybreadsoftware.net/class-cfdictionarymbs.shtml

Start with CFDictionaryMBS.dictionaryWithContentsOfFile(file as folderitem)

1 Like

Ah, good idea. I only knew the CFPreferencesMBS for the preferences.

The defaults command is problematic in recent macOS versions. Suggest you use plutil insetad. See Editing Property Lists with plutil – Scripting OS X

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.