SillyBallsSaver

SillyBallsSaver

Example showing screen saver in Python

Based on “Silly Balls.saver” by Eric Peyton <epeyton@epicware.com>

See: http://www.epicware.com/macosxsavers.html
https://developer.apple.com/library/mac/#documentation/UserExperience/Reference/ScreenSaver/Classes/ScreenSaverView_Class/Reference/Reference.html

The source of this application demonstrates - Writing a ScreenSaver in PyObjC - Building plug-in bundles in PyObjC

Jason Toffaletti <catalyst@mac.com>

Sources

SillyBalls.py

"""
Example showing screen saver in PyObjC

Based on "Silly Balls.saver" by Eric Peyton <epeyton@epicware.com>
    http://www.epicware.com/macosxsavers.html
"""
import objc

from AppKit import NSBezierPath, NSColor
from ScreenSaver import ScreenSaverView
from random import random, randrange

class SillyBalls (ScreenSaverView):

    def animateOneFrame(self):
        # choose a random point.
        (x, y), (fw, fh) = self.frame()
        x, y = randrange(0.0, fw), randrange(0.0, fw)
        ballSize = randrange(10.0, 90.0)

        path = NSBezierPath.bezierPathWithOvalInRect_(((x, y), (ballSize, ballSize)))

        # make a random color.
        randomColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(random(), random(), random(), random())

        # set it.
        randomColor.set()

        # draw a new ball.
        path.fill()

objc.removeAutoreleasePool()

setup.py

"""
Script for building the example.

Usage:
    python3 setup.py py2app
"""
from setuptools import setup

plist = dict(
    NSPrincipalClass="SillyBalls",
)

setup(
    setup_requires=["py2app"],
    plugin=["SillyBalls.py"],
    data_files=["English.lproj"],
    options=dict(py2app=dict(
        extension=".saver",
        plist=plist,
    )),
    setup_requires=[
        "py2app",
        "pyobjc-framework-Cocoa",
        "pyobjc-framework-ScreenSaver",
    ]
)

Resources