<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Architecting Life &#187; AppleEvent</title>
	<atom:link href="http://xujiwei.com/blog/tags/appleevent/feed/" rel="self" type="application/rss+xml" />
	<link>http://xujiwei.com/blog</link>
	<description>Just do it</description>
	<lastBuildDate>Wed, 28 Dec 2011 02:06:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>在 REALbasic 中注册 AppleEvent</title>
		<link>http://xujiwei.com/blog/register-appleevent-in-realbasic/</link>
		<comments>http://xujiwei.com/blog/register-appleevent-in-realbasic/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 06:37:19 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Mac OS & iOS]]></category>
		<category><![CDATA[AppleEvent]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[REALbasic]]></category>

		<guid isPermaLink="false">http://www.xujiwei.com/blog/?p=228</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/register-appleevent-in-realbasic/" title="在 REALbasic 中注册 AppleEvent"></a>之前为了注册一个自定义协议，需要通过注册 AppleEvent 来实现，在 Objective-C 中，可以很方便的使用 NSAppleEventManager 来注册 AppleEvent 句柄，但是在 REALbaisc 中，是没有办法直接去调用 NSAppleEventManager 的，所以需要通过声明然后调用 C API 来实现相应的功能。 与 NSAppleEventManager 中功能相对应的 C API 有 AEInstallEventHandler, NewAEEventHandlerUPP 等，通过这些 API 我们也可以在 REALbasic 中来注册 AppleEvent 了，再配合 Info.plist 中的 URLScheme 声明，即可实现 URL 自定义协议处理句柄。 #if TargetCarbon soft &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/register-appleevent-in-realbasic/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/register-appleevent-in-realbasic/" title="在 REALbasic 中注册 AppleEvent"></a><p>之前为了注册一个自定义协议，需要通过注册 AppleEvent 来实现，在 Objective-C 中，可以很方便的使用 NSAppleEventManager 来注册 AppleEvent 句柄，但是在 REALbaisc 中，是没有办法直接去调用 NSAppleEventManager 的，所以需要通过声明然后调用 C API 来实现相应的功能。</p>

<p>与 NSAppleEventManager 中功能相对应的 C API 有 AEInstallEventHandler, NewAEEventHandlerUPP 等，通过这些 API 我们也可以在 REALbasic 中来注册 AppleEvent 了，再配合 Info.plist 中的 URLScheme 声明，即可实现 URL 自定义协议处理句柄。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">#if TargetCarbon
    soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> AEInstallEventHandler Lib CarbonLib ( _
    theAEEventClass <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    theAEEventID <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    handler <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    handlerRefcon <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    isSysHandler <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
    Soft <span style="color: #151B8D; font-weight: bold;">Declare</span> <span style="color: #E56717; font-weight: bold;">Function</span> NewAEEventHandlerUPP Lib CarbonLib (userRoutine <span style="color: #151B8D; font-weight: bold;">as</span> Ptr) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">Static</span> CallbackUPP <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 0
    <span style="color: #8D38C9; font-weight: bold;">If</span> CallbackUPP = 0 <span style="color: #8D38C9; font-weight: bold;">then</span>
      <span style="color: #151B8D; font-weight: bold;">dim</span> m <span style="color: #151B8D; font-weight: bold;">as</span> MemoryBlock =  AddressOf ForwardCarbonAEEventToObject
      <span style="color: #8D38C9; font-weight: bold;">If</span> m <span style="color: #8D38C9; font-weight: bold;">is</span> nil <span style="color: #8D38C9; font-weight: bold;">then</span>
        Return
      <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
      CallbackUPP = NewAEEventHandlerUPP(m)
    <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">dim</span> v <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Variant</span> = me
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">dim</span> OSError <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = AEInstallEventHandler( _
    OSTypeToUInt(kInternetEventClass), _
    OSTypeToUInt(kAEGetURL), _
    CallbackUPP, _
    v.Hash, <span style="color: #00C2FF; font-weight: bold;">false</span>)
&nbsp;
    msgbox str(OSError)
#endif</pre></div></div>


<p>先使用 NewAEEventHandlerUPP 来生成一个 AppleEvent 回调函数的句柄，然后调用 AEInstallEventHandler 来注册一个共享函数 ForwardCarbonAEEventToObject 为 AppleEvent 事件处理句柄。</p>

<p>AEInstallEventHandler 所需的 AEEventClass 和 AEEventID 都是一个 4 字节的整型，但是通常我们在调用的时候，是用的一个 4 字符的字符串，因此需要一个函数来将 4 字符转换为 4 字节的整形。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">// code from ToolbarSearchField by The ZAZ Studios
// http://www.thezaz.com/opensource/realbasic/macosx/searchfield/
<span style="color: #151B8D; font-weight: bold;">static</span> m <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #E56717; font-weight: bold;">new</span> MemoryBlock(4)
m.LittleEndian = <span style="color: #00C2FF; font-weight: bold;">false</span>
m.StringValue(0, 4) = s
return m.UInt32Value(0)</pre></div></div>


<p>在 ForwardCartonAEEventToObject 里，参数 theEvent 和 replyEvent 都量个整形，为了从这两个参数里拿到数据，还需要使用 AEGetParamPtr 来从 AppleEvent 中拿到数据。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> AEGetParamPtr lib CarbonLib ( _
    theAppleEvent <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, theAEKeyword <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    desiredType <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, <span style="color: #151B8D; font-weight: bold;">byref</span> actualType <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    dataPtr <span style="color: #151B8D; font-weight: bold;">as</span> Ptr, maximumSize <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    <span style="color: #151B8D; font-weight: bold;">byref</span> actualSize <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span></pre></div></div>


<p>当然还有一系列的 AEGetDataDesc、AEGetDescSize 等函数可以，具体可以查 Xocde 随带的库文档。</p>

<p>关于注册自定义协议，可以参考<a href="http://stackoverflow.com/questions/49510/how-do-you-set-your-cocoa-application-as-the-default-web-browser">这篇文章</a>。</p>

<p>通过 Core Foundation 中的一些 C API，在 REALbasic 也可以完成一些平台相关的工作，虽然麻烦了些：）</p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/register-appleevent-in-realbasic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

