How to handle pdoc appleEvent

I want to respond to a ‘pdoc’ AppleEvent (print the associated document) but I’m not clear on how to handle the third parameter (“eventID”) received by the HandleAppleEvent handler.

I’m guessing that it is a Ptr to some kind of file descriptor but I’m stuck there. (I haven’t been able to find any Xojo specific examples.)

Any help would be appreciated. Thanks.

I can’t test right now, but, from memory, you have the signature:
EventClass: aevt
EventID: pdoc

And then the parameter is either a file to print (using TheEvent.FolderItemParam("----")) or a list of files to print (using TheEvent.DescListParam("----")).
Does this work for you?

(what do you mean by “third parameter”?)

Thomas Tempelmann has some libraries to add AppleScript support for an app. See Thomas Tempelmann | Making your Xojo OS X application scriptable (AppleScript)

Hi & thanks. The event signature is:

Application.HandleAppleEvent (Event as AppleEvent, EventClass as String, EventID as String) As Boolean

The parameter “EventID” is what I meant by the third parameter. I need to convert that into some kind of file reference that my Xojo code can use.

Thanks Beatrix; Sounds good — I’ll have a look at it.

Ah, you’re talking about the Xojo event; fine.
Here are the three parameters’ explanation:
• Event As AppleEvent: this is the object representing the actual AppleEvent. This is where, for example, you can get the file the user wants to print:
FileToPrint=Event.FolderItemParam(“----”)
(like this, it’ll work for only one item at a time)
This is where all data pertaining to the event can be found, once you’ve figured out which event is asked (from the other 2 parameters).
• EventClass as string: the class of the AppleEvent. Consider this as being a group of defined events (see later). Common values are: “core”, “aevt”, “misc”,…
• EventID as string: the ID of the event.

EventClass and EventID are both a 4 characters string to identify the event.
The EventClass is represents a group containing EventIDs.
To illustrate this, here are some common AppleEvents you have on MacOS:

AppleScript verb (as entered in a script): get
EventClass: core
EventID: getd

AppleScript verb: set
EventClass: core
EventID: setd

AppleScript verb: open
EventClass: aevt
EventID: odoc

AppleScript verb: close
EventClass: core
EventID: clos

AppleScript verb: print
EventClass: aevt
EventID: pdoc

AppleScript verb: quit
EventClass: aevt
EventID: quit

AppleScript verb: reveal (targeting the Finder to show a file)
EventClass: misc
EventID: mvis

AppleScript verb: activate (an application)
EventClass: misc
EventID: actv

AppleScript verb: duplicate
EventClass: core
EventID: delo

As you can see, the “aevt” class (suite) encloses a variety of EventID values, each representing a specific event, as does each other class.
Note that, for example, an event with class=aevt and ID=quit is meant to quit an app, but an event with class=core (or anything else) and ID=quit would not be defined system-wide and only your app would know its meaning (if you choose to define it).
Each event then defines its set of properties. For instance, “open” (class=aevt, ID=odoc) has a parameter to know which file(s) must be open, while “move” (class=core, ID=move) typically would need to know which item(s) to move and where to move them (i.e. 2 parameters, and a third optional (properties to set along the way)). The direct parameter (usually the main one) is accessed using the “----” special string.

In summary, here’s one way you can handle a “print file(s)” event in Xojo (in the HandleAppleEvent event):

if eventClass="aevt" and eventID="pdoc" then '"pdoc" in the "aevt" suite is for the "print" command
  dim aed As AppleEventDescList 'A list of parameters (valid if there are several items to print)
  dim f As FolderItem
  
  aed=theEvent.DescListParam("----") 'Attempt to get a list of items
  if aed=nil then 'When nil, it means a single item has been passed (it's like an array in Xojo: for a single item, you use a straight variable)
    f=theEvent.FolderItemParam("----") 'The single file to print
    
    PrintDocument f 'Call the method to print the passed folderitem
  else 'aed is not nil: we have several files to process
    for i as Integer=1 to aed.Count 'Index for AppleEvent functions are usually 1 based
      f=aed.FolderItemItem(i)
      
      PrintDocument f 'Print each file
    next
  end if
  Return True 'Tell the caller application that the event was understood and handled.
end if

(juste note I’ve written this right now in Xojo for this example, but I didn’t actually ran it to test for errors)

HTH.

Wow, Arnaud! I was offline for a while and I come back to such a great, detailed answer! Thank you so much for the time you spent!

Best.

…/dc

1 Like

You’re welcome!
Glad it helped.