Pagina 1 di 1

[RISOLTO] Pacchetto per aggiungere/rimuovere le icone del dock in macOS

Pubblicato: 8 novembre 2024 - 17:00
di bastien30
Buongiorno,

Ecco il pacchetto che ho creato per aggiungere e rimuovere applicazioni nel dock delle workstation MacOS che distribuiamo.

È facilmente modificabile modificando le tabelle da rimuovere[] E da_aggiungere[] a tuo piacimento.
Gestisce solo le applicazioni che si trovano in /Applicazioni/ (ma non è difficile modificarle se necessario).

È anche possibile riprendere solo le funzioni rimuovi_scorciatoia_dock() E aggiungi_scorciatoia_dock() e usali nei tuoi pacchetti.

Non è pulitissimo, ma fa il suo lavoro; se ne hai uno migliore, sono tutto orecchie :D

Codice: Seleziona tutto

# -*- 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