Skip to content

Instantly share code, notes, and snippets.

Last active October 26, 2017 21:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheagcraig/4fa2a11b7f1738e11c79 to your computer and use it in GitHub Desktop.
Save sheagcraig/4fa2a11b7f1738e11c79 to your computer and use it in GitHub Desktop.
Munki/Outset/PyObjC Self Service Set Apple Mail as Default Handler for mailto
# Put this at /usr/local/share/luggage/luggage.local to reap the rewards.
l_usr_local_outset: l_usr_local
@sudo mkdir -p ${WORK_D}/usr/local/outset/{firstboot-packages,firstboot-scripts,everyboot-scripts,login-every,login-once,on-demand}
@sudo chown -R root:wheel ${WORK_D}/usr/local/outset
@sudo chmod -R 755 ${WORK_D}/usr/local/outset
pack-outset-firstboot-packages-%: % l_usr_local_outset
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/outset/firstboot-packages
pack-outset-firstboot-scripts-%: % l_usr_local_outset
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/outset/firstboot-scripts
pack-outset-everyboot-scripts-%: % l_usr_local_outset
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/outset/everyboot-scripts
pack-outset-login-every-%: % l_usr_local_outset
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/outset/login-every
pack-outset-login-once-%: % l_usr_local_outset
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/outset/login-once
pack-outset-on-demand-%: % l_usr_local_outset
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/outset/on-demand
include /usr/local/share/luggage/luggage.make
pack-usr-local-sas-sas.png \
pack-script-postinstall \ \
pack-usr-local-sas-%: % l_usr_local_sas
@sudo ${INSTALL} -m 755 -g wheel -o root "${<}" ${WORK_D}/usr/local/sas
l_usr_local_sas: l_usr_local
@sudo mkdir -p ${WORK_D}/usr/local/sas
@sudo chown -R root:wheel ${WORK_D}/usr/local/sas
@sudo chmod -R 755 ${WORK_D}/usr/local/sas
touch /private/tmp/.com.github.outset.ondemand.launchd
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
<string>Self Service</string>
<string>Due to a bug in OS X, the default mail handler cannot be set by the user after it has been set once without programmatic intervention. To restore Apple Mail as the default mail reader, run this item, and then following the prompting instructions, quit all of your open applications prior to continuing. This change requires a logout.</string>
<string>Set Default Mail Handler to Apple Mail</string>
# -*- coding: utf-8 -*-
# Copyright (C) 2016 Shea G Craig
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
Help user set default mail client despite OS X bug.
Sets the default mail reader to Apple Mail (because we configure our
clients to use Outlook) using the LaunchServices framework, and then
immediately log out.
Displays a dialog instructing user to close open applications that may
block logout from occuring, rather than execute a Volcanic Instant Death
This uses code from my auto_logout project for the applescripting and
alert dialog presentation.
This was written to be part of an OnDemand item in Munki's Managed
Software Center, using outset's on-demand feature to run as the current
console user (as it would otherwise run as root and not work).
import os
import subprocess
import sys
# pylint: disable=no-name-in-module
from AppKit import (NSImage, NSAlert, NSTimer, NSRunLoop, NSApplication,
NSSound, NSModalPanelRunLoopMode, NSApp,
NSRunAbortedResponse, NSAlertFirstButtonReturn)
# pylint: enable=no-name-in-module
from LaunchServices import LSSetDefaultHandlerForURLScheme
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
# Sound played when alert is presented. See README.
ALERT_SOUND = "Submarine"
# Icon used in the alerts. If not present, the Python rocket is used
# instead.
ICON = "/usr/local/sas/sas.png"
# Methods are named according to PyObjC/Cocoa style.
# pylint: disable=invalid-name
class Alert(NSAlert):
"""Subclasses NSAlert to include a timeout."""
def init(self): # pylint: disable=super-on-old-class
"""Add an instance variable for our timer."""
self = super(Alert, self).init()
self.timer = None
self.alert_sound = None
return self
def setIconWithContentsOfFile_(self, path):
"""Convenience method for adding an icon.
path: String path to a valid NSImage filetype (png)
icon = NSImage.alloc().initWithContentsOfFile_(path)
self.setIcon_(icon) # pylint: disable=no-member
def setAlertSound_(self, name):
"""Set the sound to play when alert is presented.
name: String name of a system sound. See the README.
self.alert_sound = name
def setTimeToGiveUp_(self, time):
"""Configure alert to give up after time seconds."""
# Cocoa objects must use class func alloc().init(), so pylint
# doesn't see our init().
# pylint: disable=attribute-defined-outside-init
self.timer = \
time, self, "_killWindow", None, False)
# pylint: enable=attribute-defined-outside-init
def present(self):
"""Present the Alert, giving up after configured time..
Returns: Int result code, based on PyObjC enums. See NSAlert
Class reference, but result should be one of:
User clicked the cancel button:
NSAlertFirstButtonReturn = 1000
Alert timed out:
NSRunAbortedResponse = -1001
if self.timer:
self.timer, NSModalPanelRunLoopMode)
# Start a Cocoa application by getting the shared app object.
# Make the python app the active app so alert is noticed.
app = NSApplication.sharedApplication()
if self.alert_sound:
sound = NSSound.soundNamed_(self.alert_sound).play()
result = self.runModal() # pylint: disable=no-member
print result
return result
# pylint: disable=no-self-use
def _killWindow(self):
"""Abort the modal window as managed by NSApp."""
# pylint: enable=no-self-use
# pylint: enable=no-init
# pylint: enable=invalid-name
def build_alert():
"""Build an alert for auto-logout notifications."""
alert = Alert.alloc().init() # pylint: disable=no-member
"Setting the default mail reader requires an immediate logout "
"due to a bug in OS X.")
alert.setInformativeText_("Please quit all applications and hit 'Okay'. "
"Your computer will then logout.")
return alert
def set_mail_reader(bundle_id):
"""Use LaunchServices to set mailto handler.
There is a bug in OS X that allows you to set this only once.
Afterwards, if you set it again, it will revert to the previous
setting within about 10 seconds. Until this is fixed, logging out
really quickly seems to work around it.
bundle_id (String): Bundle Identifier for the app to handle
mail. Caps do not seem to matter.
Integer return code (0 is a success) as per
return LSSetDefaultHandlerForURLScheme("mailto", bundle_id)
def really_log_out():
"""Log out without the prompt. Will still ask about open apps."""
run_applescript('tell application "loginwindow" to «event aevtrlgo»')
def run_applescript(script):
"""Run an applescript"""
process = subprocess.Popen(['osascript', '-'], stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
result, err = process.communicate(script)
if err:
raise Exception(err)
return process.returncode
def build_abort_alert():
"""Build an alert for letting user know it failed."""
alert = Alert.alloc().init() # pylint: disable=no-member
"Failed to set default mail handler")
alert.setInformativeText_("Please contact the Helpdesk.")
return alert
def main():
alert = build_alert()
if alert.present() != NSAlertFirstButtonReturn:
print "User Cancelled"
if set_mail_reader("") == 0:
abort_alert = build_abort_alert()
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment