Determine the application or process that sent a scripting event

Does anyone out there know if it is possible to obtain some indication of “who” sent a particular event to a scriptable application? I’ve scanned documentation and topics related to NSScriptCommand and the cluster of objects related to a script event cycle, and used Xcode’s debugging facility to trace through a custom command’s execution but have not located any clues, nor have I found any in the application notification and event handling properties and methods. (Although I have not dug deep into that area).

My goal is for this scriptable “service” application able to manage data by application using it’s services, so each script/app only can access the elements that IT has created. I can do that in the service app provided I can get some kind of unique “signature” from each referencing application, preferably its process id.

It sounds like you need to return unique IDs when you create the objects, and then require their use when the referencing app wants to access the objects again.

I’ve wondered about this myself in the past.

If I understand (and can recall) it correctly, the Apple Event Manager acts as the bridge between sender and receiver, and presumably keeps a private reference to both in each transaction. Unfortunately, what goes on inside the AEM is opaque to both sender and receiver.

Short of an API to look inside the AEM, Shane’s approach is likely the best bet.

I wouldn’t be able to implement commands like

tell application "theService" to get myElements

theService wouldn’t be able to filter out only the owner’s "myElement"s

My workbench app already returns a uniqueID already, so that capability is conquered, but I would like to have a more complete suite; it is sooooo close to being a capability with just some kind of signature in the interprocess communication (Event Info). Apple’s Sandbox facilities must have some way of knowing else I don’t see how it could permit or deny scripting control between apps. Of course that could be deep down in scripting support, but I though I would ask.

Thanks

Have you looked at using with transaction? That might be the go, although it was badly broken before 10.10, when the release notes suggest they finally fixed end transaction.

I will research the transaction facility. It was a passing thought when I was scanning documentation, but I didn’t follow the trail at the time, hoping for a more elegant solution :slight_smile:

Yes, the keyOriginalAddressAttr tells you the application that originated the event:

NSAppleEventDescriptor* target = [theAppleEvent attributeDescriptorForKeyword:keyOriginalAddressAttr];

Note that you might get a PSN, Bundle ID or typeApplSignature (probably not in contemporary versions of the Mac OS).

1 Like

Just saw this Mark. I will follow up on it tomorrow, thanks :relieved:

The code from Mark gave me a search key "attributeDescriptorForKeyword " that hit just what I was looking for. Yes, it does pay to ask. I had scanned Stack Overflow with variations of “NSScriptCommand” and “apple events” but didn’t spot what I wanted at the time.

From Stack Overflow.

I inserted the suggested code in my test command and the resulting “info” dictionary had all kinds of “goodies” including the “pid” of the app sending the command.

More testing is needed to see about consistency and possible side-effects, but it looks good so far.:relaxed: