Showing posts with label mac. Show all posts
Showing posts with label mac. Show all posts

Friday, August 17, 2007

StuntPlayground

I've been porting StuntPlaygound to MacOSX.  It uses the Ogre3D engine.  The source was kindly provided at http://walaber.com/.  The main challenges have been getting to run on the latest version of Ogre ("Eihort", 1.4.x), which uses CEGUI v0.5 for UI elements and OIS for keyboard and mouse input.
So far, I have the arena spinning around and the logo comes up:
More updates as I progress.

Wednesday, August 15, 2007

Using NetWorkSpaces with Python

In order to try out NetWorkSpaces for Python, I tackled a problem I've had for some time.  Due to IT reasons, its easier to have my Outlook client run on a PC nearby, and view that PC through VNC.  But, sometimes I miss meeting notices if I'm not around to hear the reminder sound through the PC's speakers.

First I needed create Macros for Outlook to run each time I received an email or a reminder.  There are subroutines called 'Application_NewMail' and 'Application_Reminder' that are called, so I grabbed some information from the subject in each of those routines, entitled each with either 'Email' or 'Reminder', and sent the info to a Python script:



Public Function SendText1(title, txt)
Dim prog As String
Dim t As String
Dim WSHShell

Set WSHShell = CreateObject("wscript.Shell")
t = " " & Trim(txt)
prog = "c:\Python25\Python.exe c:\Python25\doNotify.py " & title & " """ & t & """"
WSHShell.Run prog, 1, 1
Set WSHShell = Nothing
End Function


Private Sub Application_NewMail()
Dim oFolder
Dim oNewItem
Dim s
Dim n

Set oFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set oNewItem = oFolder.Items.GetLast
Rem For Each oNewItem In oFolder.Items.Restrict("[Unread] = 0")
s = oNewItem.SenderName & ": "
s = s & Trim(oNewItem.Subject)
n = "Email"
Call SendText1(n, s)
Rem Next
End Sub

Private Sub Application_Reminder(ByVal Item As Object)
Dim s As String
Dim sBody As String
Dim n

s = Item.Subject & "(" & Item.Location & ") " & FormatDateTime(Item.Start, vbShortTime)
n = "Reminder"
Call SendText1(n, s)
End Sub


Notice in 'SendText1' I run a Python script called 'doNotify.py' with two parameters, coming in as args[1] and args[2]:



# doNotify.py
from nws.client import NetWorkSpace
import sys

def main(args):
ws = NetWorkSpace('kh_outlook', serverHost='bvt-dc00133', useUse=True)
ws.store('notify', (args[1],args[2]))

if __name__=="__main__":
main(sys.argv)



The script grabs the NetWorkSpace 'kh_outlook', stores to the name 'notify' a tuple for the two arguments, and then exits.

On my Mac, I run a 'server' that hangs around, watching the NetWorkSpace of 'kh_outlook', fetching 'notify', and passing the two strings from the tuple to Growl, using growlnotify:



# outlook_server.py
import os
from nws.client import NetWorkSpace

print "Starting server..."
ws = NetWorkSpace('kh_outlook', serverHost='bvt-dc00133', persistent=True)
try:
while 1:
message = ws.fetch('notify')
cl = "growlnotify -s -a mail.app -m \"" + message[1] + "\" -t " + message[0]
os.system(cl)
except KeyboardInterrupt:
print 'shutting down gracefully.'

print 'Exiting.'



I get Growl messages for each email or reminder, titled ('-t' option) with either 'Email' or 'Reminder' respectively, and I set those messages sticky with the '-s' option so I won't miss them if I'm away from my Mac.

Monday, August 13, 2007

Python Intro

Gave a short talk at the Portland Cocoaheads meeting last Wednesday, Aug. 8th. I basically wanted to highlight the bits of Python that I wish I had known before I dove in. Here's the code I used to copy and paste into an interactive Python session:



#!/usr/local/bin/python

# indentation
a = 1
if (a == 1):
print a
else:
print "a != 1"

# if statement
a = 3
if a > 0:
print 'greater'
elif a == 0:
print 'equal'
else:
print 'less'

# while loops
a = 3
while a > 0:
print "a = %d"%a
a -= 1

#functions
def hello(who):
print "Hello, " + who

hello("world")

# strings
def hello(who):
return "Hello, " + who
s = hello("world")
print "s = %s"%s
print s[0:5]
print len(s[0:5])
print s.upper()

# lists
l = [ 'a', 'b', 'c', 1, 2, 3 ]
print l
# third element
print l[2]
# third, fourth, and fifth elements
print l[2:4]
# last element
print l[-1]
# append
l.append('snake')
print l

# for loops
for i in l:
print i

# dictionary
d = {'alpha': 1, 'beta':2, 'gamma':3}
d['delta'] = 4
print d
print d['gamma']
del d['gamma']
d.keys()
if d.has_key('gamma'):
print 'has gamma'
else:
print 'does not have gamma'
for k,v in d.iteritems():
print "k = ",k
print "v = ",v

# sets
s1 = set()
for x in 'aabbbccccdddddefffffff':
s1.add(x)
print 'set s1 = ',s1

# dir() function
# dir(s)

# help function
import platform
#help(platform)
platform.processor()

# using pyobjc
#
from Foundation import *
d1 = NSMutableDictionary.dictionary()
print isinstance(d, dict)
print type(d)
print isinstance(d1, dict)
print type(d1)
print isinstance(d1, NSMutableDictionary)
d1.setObject_forKey_(42, 'key2')
d1['key1'] = 'hello'
print d
print d1

# addressbook example
import AddressBook
book = AddressBook.ABAddressBook.sharedAddressBook()
b = book.people()
print "Number of people in addressboo is ",len(b)
print b[1].valueForProperty_(AddressBook.kABLastNameProperty)
print type(b)

Editors: