Mac OS plist creation triggers notification

385 Views Asked by At

Currently I am trying to setup up some automation on my Mac using launchagent plist files and various other mechanisms provided by macOS.

How I programmatically create a plist file:

import plistlib

data = {
    'Label': 'some label',
    'ProgramArguments': [path/to/shell_script],
    'RunAtLoad': True,
    'KeepAlive': False
}

with open(my_plist_file_path, 'wb') as plist_file:
    plistlib.dump(data, plist_file)

So this little snippet successfully creates a plist file in ~/Library/LaunchAgents/ where all other user specific plist files are located... BUT:

Every time I do so, I get the mac os system notification saying:

Background Items Added: ...

And my question is simply: why?

How can i programmatically create a plist without triggering this notification?

Version: Mac OS Ventura 13.5

In ~/Library/LaunchAgents/ I can see many other plist files which have been created by e.g. Google Chrome / Homebrew but these plist files never triggered the notification described above.

2

There are 2 best solutions below

2
Philippe On
This is a known issue. There are several threads about this here on these forums. In one of them a well respected forum contributor who is also an Apple developer tried to explain some of the issues here, but I cannot find this post right now. Basically Apple added a new "Background App" feature to macOS Settings (just ignore this are of Settings as it is not what you think it is and may cause more problems) which is currently broken...plus there is no real information about this feature so app developers have no way of understanding how to code their apps to work properly with this new macOS feature/option. Also, don't try deleting any files as has been suggested by some people one as the previously mentioned contributor & developer has mentioned it will just cause even more problems.

https://discussions.apple.com/thread/254476743

You can reset background task management database, to get around this.

sfltool resetbtm
# then reboot

https://support.apple.com/en-gb/guide/deployment/depdca572563/web

You can call directly from python code :

import os, plistlib

data = {
    'Label': 'some label',
    'ProgramArguments': [path/to/shell_script],
    'RunAtLoad': True,
    'KeepAlive': False
}

os.system('sudo sfltool resetbtm')

with open(my_plist_file_path, 'wb') as plist_file:
    plistlib.dump(data, plist_file)
0
orbanbalage On

With AppleScript:

tell application "System Events"
    -- create an empty property list dictionary item
    set the parent_dictionary to make new property list item with properties {kind:record}
    
    -- create new property list file using the empty dictionary list item as contents
    set the plistfile_path to "~/Documents/example.plist"
    
    set pl to ¬
        make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
end tell

I did test the ~/Library/LaunchAgents/ location and it works and I did not get any system notifications. I'm on Ventura 13.6

Script source: Applescript2plist.scpt

Saving information in plist through Applescript

(In example: current track played in iTunes)

Note: I don't have iTunes, but the script worked with Spotify without motidication (you get a prompt to select the music app).