Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!uw-beaver!nntp.cs.ubc.ca!psgrain!iafrica.com!uct.ac.za!quagga.ru.ac.za!howland.erols.net!EU.net!newsfeed.internetmci.com!csn!nntp-xfer-1.csn.net!csn!nntp-xfer-2.csn.net!stortek!news
From: Yueming Xu <yxu@bronze.stortek.com>
Subject: Apple Events with VisualWorks
Content-Type: text/plain; charset=gb2312
Message-ID: <323F332F.495@bronze.stortek.com>
Sender: news@stortek.com
Content-Transfer-Encoding: 7bit
Organization: Field Automation
Mime-Version: 1.0
Date: Tue, 17 Sep 1996 23:24:31 GMT
X-Mailer: Mozilla 2.02 (X11; I; SunOS 5.5 sun4m)
Lines: 143

This question is for Macintosh.  I tried to launch a word processor with
a file name as the parameter, and hope the word processor to open the
file when it is launched.  I guess there is a way to accomplish this
using Apple Events.  So, I filed in ParcPlace's Apple event stuff, and
customized the VW object engine.  I have got the simple example in
AppleEventDispatcher run, but failed to extend this example to solve our
problem.  The enclosed class method is what I have got so far.  It does
NOT work!

I really do not understand AppleEvent programming.  Is there anyone who
have done something like this?  Any information would be very
appreciated.  Thanks.

-- 
 ___________________________________________________________________
(__ _____________________________________________________________ __)
   |                                                             |
   | Yueming Xu                   Phone:  (303) 673-4426         |
   | WWFO Automation              Fax:    (303) 661-5191         |
   | Storage Technology, Inc.     Pager:        851-9267         |
   | Louisville, CO 80028-4361    E-mail: yxu@bronze.stortek.com |
 __|_____________________________________________________________|__
(___________________________________________________________________)

'From VisualWorks(R), Release 2.5.1 of September 26, 1995 on September
17, 1996 at 4:24:59 am'!



!AppleEventDispatcher class methodsFor: 'examples'!

openDocNamed: fullPathString
	"Create and send a Macintosh AppleEvent to Microsoft Word,
	telling it to open the document specified in aPathString."

	"AppleEventDispatcher openDocNamed: 'Macintosh HD:Desktop Folder:stp
dev:stpadm.ini'"

	| ae signPtr appAddress oserr theEvent reply docSpecPtr fileAliasPtr
listElemPtr fileListPtr |

	ae := AppleEventsInterface new.
	theEvent := ae AppleEvent gcMalloc.
	reply := ae AppleEvent gcMalloc.
	appAddress := ae AEAddressDesc gcMalloc.
	signPtr := ae DescType gcMalloc contents: ae kMSWordSignature value.

	"Find the Finder's address"
	oserr := ae
				AECreateDesc: ae typeApplSignature value
				dataPtr: signPtr
				dataSize: 4
				result: appAddress.
	oserr = 0 ifFalse: [^oserr].

	"Create an AppleEvent"
	oserr := ae
				AECreateAppleEvent: ae kCoreEventClass value
				eventID: ae kAEOpenDocuments value
				target: appAddress
				returnID: ae kAutoGenerateReturnID value
				transactionID: ae kAnyTransactionID
				result: theEvent.
	ae AEDisposeDesc: appAddress.

	"Make an FSSpec for the target document."
self halt.
	docSpecPtr := ae FSSpec gcMalloc.
	oserr := ae
				FSMakeFSSpec: 0
				dirID: 0
				fileName: fullPathString asByteString gcCopyToHeap
				specPtr: docSpecPtr.
	oserr = 0 ifFalse: [^oserr].

	"Create alias for the file."
	fileAliasPtr := ae AliasHandle gcMalloc.
	oserr := ae NewAlias: 0 target: docSpecPtr alias: fileAliasPtr.
	oserr = 0 ifFalse: [^oserr].

	"Create  an alias list."
	fileListPtr := ae AEDescList gcMalloc.
	oserr := ae
				AECreateList: ae nil value
				factoredSize: 0
				isRecord: 0
				result: fileListPtr.
	oserr = 0 ifFalse: [^oserr].

	"Create the file descriptor."
	listElemPtr := ae AEDesc gcMalloc.
	oserr := ae
				AECreateDesc: ae typeAlias value
				dataPtr: fileAliasPtr
				dataSize: fileAliasPtr size
				result: listElemPtr.
	oserr = 0 ifFalse: [^oserr].

	"Add the file descriptor to the alias list."
	oserr := ae
				AEPutDesc: fileListPtr
				index: 0
				desc: listElemPtr.
	oserr = 0 ifFalse: [^oserr].
	ae AEDisposeDesc: listElemPtr.

	"Add the alias list to the event."
	oserr := ae
				AEPutParamDesc: theEvent
				keyword: ae keyDirectObject value
				desc: fileListPtr.
	oserr = 0 ifFalse: [^oserr].
	ae AEDisposeDesc: fileListPtr.

	"Send the AppleEvent"
	oserr := ae
				AESend: theEvent
				reply: reply
				sendMode: 	ae kAENoReply value +
							ae kAEAlwaysInteract value +
							ae kAECanSwitchLayer value
				sendPriority: ae kAENormalPriority value
				timeOutInTicks: ae kAEDefaultTimeout
				idleProc: 0
				filterProc: 0.
	oserr = 0 ifFalse: [^oserr].

	"Dispose of the AppleEvents we created."
	ae AEDisposeDesc: theEvent.
	ae AEDisposeDesc: reply.

	^oserr.! !

'From VisualWorks(R), Release 2.5.1 of September 26, 1995 on September
17, 1996 at 4:25:51 am'!



!AppleEventsInterface methodsFor: 'defines'!

kMSWordSignature
	<C: #define kMSWordSignature 'MSWD'
	>! !
