Accessing More Frameworks with Python

This post is based on a column I wrote for MacTech magazine in 2012. MacTech used to make older columns available online, but they haven’t done that for the past several years for some reason.
I’m planning to go through my older columns and dust off and republish some that I think are still relevant or useful.

Recently, we built a command-line tool using Python and the PyObjC bridge to control display mirroring.
PyObjC supports a lot of OS X frameworks “out-of-the-box”, and accessing them from Python can be as simple as:

include CoreFoundation

But what if the problem you want to solve requires a framework that isn’t included with the PyObjC bindings? In turns out that you can create your own bindings. In this post we’ll explore this aspect of working with Python and OS X frameworks.

OUR SAMPLE PROBLEM

In my organization, we sometimes have a need to set displays to a certain ColorSync profile. The ColorSync profile to use for a given display is a per-user preference, so if you need to set it for all users of a machine, you can’t just manually set it while logged in as one user and call it good.

If you are managing display profiles for a group of machines, or a conference room machine that has network logins, you need a way to manage display profiles for all users. Using MCX or doing some defaults scripting might come to mind. Let’s look at that possibility.

Continue reading “Accessing More Frameworks with Python”

Accessing More Frameworks with Python

Command-line tools via Python and Cocoa

This post is based on a column I wrote for MacTech magazine in 2012. MacTech used to make older columns available online, but they haven’t done that for the past several years for some reason.
I’m planning to go through my older columns and dust off and republish some that I think are still relevant or useful.

Cocoa-Python, also referred to as PyObjC, is a set of Python modules and glue code that allow Python programmers to access many of Apple’s Cocoa frameworks. This allows you to do many things from Python scripting that might otherwise require compiling code in C/Objective-C. To access the Cocoa frameworks, you import them by name, just as you might import a regular Python module.

A quick example: the CoreFoundation framework contains methods to work with user preferences, a bit like the /usr/bin/defaults tool. We can use the CFPreferencesCopyAppValue function in Python simply by importing CoreFoundation, and then calling it like we would a function from a “regular” Python module:

#!/usr/bin/python

import CoreFoundation

print CoreFoundation.CFPreferencesCopyAppValue(
          "HomePage", "com.apple.Safari")

If you run the above code, it will print the current home page you have set in Safari. We’ve successfully used an OS X framework from Python!

Continue reading “Command-line tools via Python and Cocoa”

Command-line tools via Python and Cocoa

You Oughta Check Out AutoPkg: Links

If you attended my presentation on AutoPkg today, thanks! Here are the links:

AutoPkg:

http://autopkg.github.io/autopkg

https://github.com/autopkg/autopkg

https://github.com/autopkg/autopkg/releases

AutoPkg recipe repos:

http://github.com/autopkg

JSSImporter:

https://github.com/arubdesu/jss-autopkg-addon

AbsoluteManage Processor:

https://github.com/tburgin/autopkg/blob/master/Code/autopkglib/AbsoluteManageExport.py

AutoPkg Change Notifications script:

http://seankaiser.com/blog/2013/12/16/autopkg-change-notifications/

MacSysAdmin 2013 session:

http://docs.macsysadmin.se/2013/video/Day2Session4.mp4

Steve Yuroff’s AutoPkg and Jenkins notes:

http://swytechnotes.wordpress.com/2013/10/21/autopkg-and-jenkins-under-one-admin-account/

AutoPkg Wiki:

https://github.com/autopkg/autopkg/wiki

You Oughta Check Out AutoPkg: Links

Post-PSU Mac Admins Pre-Conference Workshop

If you attended the workshop today Matt and I led on “Python for Systems Administrators”, thank you! Here are links to some of the additional information and documentation mentioned today:

Course materials:

http://gregneagle.github.io/psumac2014_python/

Cocoa documentation links

Foundation:

https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Miscellaneous/Foundation_Functions/Reference/reference.html

CFPreferences:

https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFPreferencesUtils/Reference/reference.html

Plists and Foundation:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

More documentation:

http://www.python.org/

http://www.diveintopython.net

https://www.coursera.org/course/interactivepython

Post-PSU Mac Admins Pre-Conference Workshop

Xcode 5 Cocoa-Python Templates

Xcode

A couple of years ago I made some Xcode 4 project and file templates available to help in creating new projects that use Cocoa-Python, also known as PyObjC.

These templates kind-of, sort-of worked with Xcode 5, but due to changes in how Apple is supporting Python development, required some manual tweaking.

(See https://developer.apple.com/library/mac/technotes/tn2328/_index.html for “Changes To Embedding Python Using Xcode 5.0″)

I’ve updated the templates so they should “just work” with Xcode 5:

https://github.com/gregneagle/Xcode5CocoaPythonTemplates

Enjoy.

Xcode 5 Cocoa-Python Templates