Latest Updates: mac RSS

  • Snakes on Demand: How to write a Python Launchd Agent

    mike 9:23 pm on March 14, 2010 | 0 Permalink | Reply
    Tags: launchd, mac, macdev, multiprocessing, , python

    Suppose you want to write a Launchd agent in Python that communicates using UNIX domain sockets. There’s no sample code for that, but the information is out there to figure out how (especially because the launchd source code is available). Most search results will tell you that you need to read the Daemons and Agents Tech Report TN2083, but it’s pretty long and not a great tutorial. A better intro reference is Chris Hanson’s blog post “Launchd: better than sliced bread!”, but that doesn’t tell you everything, and doesn’t mention Python.

    I decided I’d share what I’ve learned recently to make getting started a little easier. I’ll give a short description here and post the resulting code to bitbucket at py-launchd.

    Note: This was written and tested on OS X 10.5.8 with the default /usr/bin/python, version 2.5.1. Since I wanted to use multiprocessing, I used the backport to 2.5 available at google code: python-multiprocessing . For convenience, it’s included in the repository, and so is its license.

    Goal

    What we’re trying to do is have some python code that gets called when launchd notices someone connecting to a UNIX domain socket. UNIX domain sockets are local-only, so this is ideal for agents that only serve local apps. We’re also using launchd “agents” not “daemons”, so we’re assuming that it’s OK to have one agent process for each user. If you’re managing access to something that needs to be unique system-wide, then this won’t work (but you can still use Launchd).

    launchd overview

    The launchd process will read a launchd plist you give it (at login, via launchctl or the 10.6 framework), and listen on the socket you tell it to. Once it sees a connection, it’ll start the agent program you specified in the plist, and that program can make some calls using the launchd C API to get a file descriptor for the socket that was connected. This is important – you don’t need to call bind() or listen() on the socket, because launchd already did. It’s just handing the open socket’s file descriptor straight to you and your code can just call accept() on it.

    Using launchd with Python

    You need to create a C/ObjC tool that can do the launchd check-in to get the open file descriptor, then pass that off to your Python code. This is pretty straightforward using the Python framework included in OS X and the Python C APIs.

    What I’ve done is create an agent loader that I called PyLaunchd that loads and runs server code in Agent.py. It expects Agent.py to be in the same directory.

    PyLaunchd is built separately and copied into the test app’s Resources folder with an XCode script phase.

    I have the app delegate copy PyLaunchd and Agent.py to ~/Library/Application Support/PyLaunchd/ on loading. It also customizes the launchd plist to set the path correctly for the current user, then writes that to ~/Library/LaunchAgents/, and loads it. (Actually it first unloads it, then reloads it. I’m not convinced this is the right way to do it). It uses system() to call launchctl, but I believe in OS X 10.6 there’s an API you can call to do it directly.

    Finally, the sample app is really simple – it just opens the socket using the multiprocessing Client class, and sends whatever you type. The example Agent I’ve included will ROT13 it and send it back.

    Please let me know if you have any comments, questions, or improvements.

     
  • App History

    mike 1:02 pm on November 25, 2009 | 0 Permalink
    Tags: mac,

    I just saw Brent’s post from a couple weeks ago about an App History App – I think that’d be really cool. I especially like the screen shot idea, since I love seeing posts from a developer showing what an app looked like along the progression. It’s too easy to forget to document that kind of thing, and I’d love it if we saw more of these behind-the-scenes posts.

     
  • Measuring PDFs with Skim

    mike 5:00 pm on November 6, 2009 | 0 Permalink
    Tags: , mac, skim,

    Skim Measurement

    I love it when I can guess how to do something new in a program. Skim just did this for me – I needed to figure out how big the rectangle of text in a paper I’m working on is, in inches. I looked for rulers, and then decided I’d just see if I get any size feedback when I make a rectangular selection. Turns out there’s a nice feedback status line in the bottom right – clicking toggles between measurement in pts and in inches.

    I like how this solves my problem without adding a lot of new UI for measurement – no rulers, no extra tool to measure length.

     
  • mike 12:08 pm on October 26, 2009 | 3 Permalink
    Tags: apps, awards, community, mac

    I’d like to see a community-run version of the Apple Design Awards.

    Anyone could nominate an app, and the developer community would vote on who wins, in just a few categories.

    I’m thinking the categories could be a little more user-centric than Apple’s. For instance, I don’t really care about best adoption of OS X technologies, but something like “best refinement of a common task” or “best default behavior” or “best scriptability” would be interesting, and something that’d be easier to use as a marketing badge…

    This is just off the top of my head, so there are probably others. “Least data lock-in”? “Easiest to learn?”, “Best plugin community”?

     
  • mike 12:49 pm on September 17, 2009 | 0 Permalink
    Tags: , mac, , text

    TextShapeView was another Cocoa Text system hack project I started a few years ago.

    I made a view that displays a zoomed out view of a string from a text view, with indentation, along with a rectangle showing where the text view is in the file.

    You can click around in the view to move the It also highlights the selection, and draws lines in blue if they contain the string in the NSFindPBoard, so you can see all the lines in the file that match when you use the Find panel.

    This is available under a Creative Commons license, and I’d ask that if you make any improvements, you send them back to me or join the ‘leverage’ sourceforge project, where the code is hosted. I wanted to eventually make this into a Cocoa text plugin like my I-Search plugin.

    Here is the SVN repository for the textshape code.

    There was also a Source code Tarball with a demo app, shown in the screenshot below:

     
  • mike 2:15 pm on August 20, 2009 | 0 Permalink
    Tags: location, mac, network, script

    Prompted by a thread on the ever-useful Sun internal mac-users alias, here’s a daemon that runs a script every time your network location changes:

    Mac OS X LocationChanger – TECH.inhelsinki.nl.

     
  • mike 11:54 am on August 18, 2009 | 0 Permalink
    Tags: , freeware, mac, mailsmith

    From @spamsieve, I heard that BareBones’ Mailsmith has a new home: mailsmith.org. It’s freeware and will be under development.

    It’s great to see new life for one of the few alternative desktop mail clients for OS X, and while there’s no info on their plans for it yet, there’s some great discussion of possible features on the google group mailsmith-talk.

    I’ll be curious to see if any of the design choices will be changing – for example, no IMAP support and no inline HTML display… I have to use IMAP, and despite common abuse, I do think HTML email is useful.

    edit: Looks like no Mailsmith IMAP: “Not any time soon…” – @siegel on twitter

     
  • mike 11:37 pm on August 6, 2009 | 2 Permalink
    Tags: apple, mac,

    From the HN thread on this post from Garry at posterous, about product minimalism:

    Teams at apple are TINY. Mail is like 3 guys. …

    Two things: The thread was about how high quality software is easier without politics you get in large teams, which I agree with, and I bet that a lot of the software we love was made by smaller teams than you’d think.

    And, just 3 engineers on Mail explains a lot.

     
  • New Release of EagleFiler

    mike 11:22 am on July 28, 2009 | 0 Permalink
    Tags: mac, , organization, productivity

    From C-Command Blog – EagleFiler 1.4.7: a bunch of nice refinements.

    I use EagleFiler to keep track of reference documents that I get in emails. Presentations, reports, etc – the kind of thing that would usually just get stored in the mail archive. Because I want a complete mail archive and a fast way to get to the important stuff, I keep the important stuff in EagleFiler so I can add tags and searches don’t get a lot of false hits.

     
  • Subversion Client Issues

    mike 7:17 am on July 13, 2009 | 7 Permalink
    Tags: gui, mac,

    I use subversion, and won’t switch to something newer for a while, so it’s worth looking at how to polish that old hammer. I’m used to using the command line svn, or emacs. But recently I decided to try out a few of the nice GUI clients that are around, including Versions, ZigVersion and Cornerstone.

    Of these, the only one that’s polished enough to lure me away from emacs and seems to support my favorite mode of working is Cornerstone, and it still gets it a little wrong.

    I like to write log comments while looking at (and scrolling through) the diffs for the files I’m committing. This means I want a text field for writing log comments on the same screen as the diffs, that isn’t modal, and doesn’t stop me from moving around between multiple diffs.

    As far as I could tell, I couldn’t get the comment field and the diff display shown together in Versions, and while I could in ZigVersion, that app had a subpar diff display and lacked polish overall, missing key shortcuts where I’d expect them, for instance. Cornerstone almost lets me do what I want, but it displays the comment field in a modal sheet, so I have to cancel to change which diff I’m looking at.

    This is easy in emacs, but I like a nicer diff GUI. Am I just missing something? This feels like a natural workflow, so it seems strange that no clients support it well.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel