[SOLVED] Package to add/remove dock icons in macOS

Questions about WAPT Packaging / Requests and help regarding Wapt packages.
Forum Rules
Community Forum Rules
* English support on www.reddit.com/r/wapt
* French community support is available on this forum
* Please prefix the topic title with [RESOLVED] if it is resolved.
* Please do not edit a topic that is tagged [RESOLVED]. Open a new topic referencing the old one.
* Specify the installed WAPT version, full version, and build number (2.2.1.11957 / 2.2.2.12337 / etc.) as well as the Enterprise/Discovery edition.
* Versions 1.8.2 and earlier are no longer supported. The only questions accepted regarding version 1.8.2 are related to upgrading to a supported version (2.1, 2.2, etc.).
* Specify the server OS (Linux/Windows) and version (Debian Buster/Bullseye - CentOS 7 - Windows Server 2012/2016/2019).
* Specify the OS of the administration/package creation machine and the machine with the problematic agent, if applicable (Windows 7/10/11/Debian 11/etc.).
* Avoid asking multiple questions when opening a topic, otherwise it may be ignored. If there are multiple topics, open separate topics, preferably one after the other and not all at the same time (i.e., do not spam the forum).
* Include code snippets, screenshots, and other images directly in the post. Links to Pastebin, Bitly, and other third-party sites will be systematically removed.
* As with any community forum, support is provided voluntarily by members. If you require commercial support, you can contact Tranquil IT's sales department at 02.40.97.57.55
Answer
bastien30
Messages: 38
Registration: March 8, 2024 - 3:21 PM

November 8, 2024 - 5:00 PM

Good morning,

Here is the package I made to add and remove applications in the dock of the MacOS workstations we deploy.

It is easily modifiable by editing the tables to_remove[] And to_add[] at your convenience.
It only manages applications that are located in /Applications/ (but it's not hard to change if needed).

You can also resume only the functions remove_dock_shortcut() And add_dock_shortcut() and use them in your packages.

It's not super clean but it does the job; if you have a better one, I'm all ears :D

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *

# Source: https://stackoverflow.com/questions/19918547/how-to-remove-an-application-icon-from-dock-from-mac-osx-mavericks

def install():
    print(r'Installation does nothing, dock will be configured per user at first session-setup.') 
    
def uninstall():
    print(r'Uninstallation does nothing, launch "defaults delete com.apple.dock; killall Dock" in user shell to reset the dock to defaults settings.')

def session_setup():
    to_remove = ["Safari", "Messages", "Mail", "Plans", "Photos", "FaceTime", "Contacts", "Freeform", "TV", "Musique", "Keynote", "Numbers", "Pages", "App Store"]
    for app in to_remove:
        remove_dock_shortcut(name=app)
    
    to_add = ["Thunderbird", "3CX Desktop App", "Spark", "WAPT/WAPT Self-service"]
    for app in to_add:
        add_dock_shortcut(name=app)

    # Reload dock after all modifications    
    run("osascript -e 'tell Application \"Dock\"' -e 'quit' -e 'end tell'")

def remove_dock_shortcut(name=None, reload=False):
    """Remove shortcut from MacOS dock
    Args:
        name : application name (it has to be in /Application/)
        reload (boolean) : if True, reload dock before return
    Returns:
        True if dock was updated, else False
    """
    if name:
        all_shortcuts = run(r'defaults read com.apple.dock persistent-apps | grep file-label\"').split("\n")
        i = 0
        for shortcut in all_shortcuts:
            if "=" in shortcut:
                current = shortcut.split("=")[1].split(";")[0].strip().strip('"')
                if current == name:
                    run(r'/usr/libexec/PlistBuddy -c "Delete persistent-apps:%s" ~/Library/Preferences/com.apple.dock.plist' % i)
                    if reload:
                        run("osascript -e 'tell Application \"Dock\"' -e 'quit' -e 'end tell'")
                    return True
            i += 1
    return False

def add_dock_shortcut(name=None, reload=False):
    """Add shortcut to MacOS dock
    Args:
        name : application name (it has to be in /Application/)
        reload (boolean) : if True, reload dock before return
    Returns:
        True if dock was updated, else False
    """
    if name:
        run(r'defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/%s.app/</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"' % name)
        if reload:
            run("osascript -e 'tell Application \"Dock\"' -e 'quit' -e 'end tell'")
        return True
    return False
Answer