tell.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Lib » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Lib » test » tell.py
# (Slightly less) primitive operations for sending Apple Events to applications.
# This could be the basis of a Script Editor like application.

from AE import *
from AppleEvents import *
import aetools
import types

class TalkTo:
  def __init__(self, signature):
    """Create a communication channel with a particular application.
    
    For now, the application must be given by its 4-character signature
    (because I don't know yet how to do other target types).
    """
    if type(signature) != types.StringType or len(signature) != 4:
      raise TypeError, "signature should be 4-char string"
    self.target = AECreateDesc(typeApplSignature, signature)
    self.send_flags = kAEWaitReply
    self.send_priority = kAENormalPriority
    self.send_timeout = kAEDefaultTimeout
  def newevent(self, code, subcode, parameters = {}, attributes = {}):
    event = AECreateAppleEvent(code, subcode, self.target,
                          kAutoGenerateReturnID, kAnyTransactionID)
    aetools.packevent(event, parameters, attributes)
    return event
  def sendevent(self, event):
    reply = event.AESend(self.send_flags, self.send_priority,
                              self.send_timeout)
    parameters, attributes = aetools.unpackevent(reply)
    return reply, parameters, attributes
    
  def send(self, code, subcode, parameters = {}, attributes = {}):
    return self.sendevent(self.newevent(code, subcode, parameters, attributes))
  
  def activate(self):
    # Send undocumented but easily reverse engineered 'activate' command
    self.send('misc', 'actv')


# This object is equivalent to "selection" in AppleScript
# (in the core suite, if that makes a difference):
get_selection = aetools.Property('sele', None)

# Test program.  You can make it do what you want by passing parameters.
# The default gets the selection from Quill (Scriptable Text Editor).

def test(app = 'quil', suite = 'core', id = 'getd', arg = get_selection):
  t = TalkTo(app)
  t.activate()
  if arg:
    dict = {'----': arg}
  else:
    dict = {}
  reply, parameters, attributes = t.send(suite, id, dict)
  print reply, parameters
  if parameters.has_key('----'): print "returns:", str(parameters['----'])
  

test()
# So we can see it:
import sys
sys.exit(1)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.