How to start external application when grasshopper starts?

Hey guys,

I’m in the middle of a grasshopper hackathon that last until tomorrow evening and I am having troubles finding a way to:

When grasshopper starts I want to launch an application.

Basically this is most likely a super simple thing, just need to subscribe to some event or something, I’m a professional coder so feel free to go quite detailed:)

All the help is greatly appreciated:)

Thanks

Edit: I’m using rhino 5 and grasshopper 0.9.0.0.7.6

If Grasshopper is loading a GHA of yours, then you can put all the code you want inside a Grasshopper.Kernel.GH_AssemblyPriority derived class. This class will be loaded first if it exists. Of course any component derived class will also be loaded on startup, so you can put your app init code there as well, but then you have to figure out how to not start an external all the next time your component gets instantiated.

Super David,

worked as a charm, super simple, thanks

Hey,

Now I have extra quations about this:

Question1:
Is there a function or somehting I can override that is runs after the canvas is loaded so that I cal subscribe to objectsAdded event?

Qustion2:
What about an event that triggers when grasshopper is closing? I would like to un-subscribe this event there. Or is it needed? Will grasshopper do that automatically for me when it closes?

     GH_Document grasshopperDocument = Grasshopper.Instances.ActiveCanvas.Document;
     grasshopperDocument.ObjectsAdded -= addSourceChangeHandlerToNewlyAddedObjects;
     grasshopperDocument.ObjectsAdded += addSourceChangeHandlerToNewlyAddedObjects;

It’s not needed. GH only closes when Rhino closes and the entire AppDomain is shut down. Everything will be unloaded and unregistered automatically. So unless you want to do something there’s no need.

It doesn’t make sense to do this before any documents are loaded, so you could postpone registering the events until the first time Grasshopper.Instances.DocumentServer.DocumentAdded event is raised. However this does not necessarily guarantee that the window and canvas are already loaded.

In recentish versions of GH for Rhino6 there’s a static event on GH_DocumentEditor called AggregateShortcutMenuItems, which is raised only once, after the menu of the editor has been populated. It was added to allow plugins to more easily insert their own items into the menu, but you can use it as a cue for the availability of the main canvas.

Hey David,

Great. Now I have:

  public override GH_LoadingInstruction PriorityLoad()
  {
     Grasshopper.Instances.DocumentServer.DocumentAdded += DocumentAdded;

     return GH_LoadingInstruction.Proceed;
  }

  void DocumentAdded(GH_DocumentServer documentServer, GH_Document document)
  {
     GH_Document grasshopperDocument = document;
     grasshopperDocument.ObjectsAdded -= addSourceChangeHandlerToNewlyAddedObjects;
     grasshopperDocument.ObjectsAdded += addSourceChangeHandlerToNewlyAddedObjects;
  }

This seems to do the trick, i tested and works fine, but now you scared me with:

Can you explain a bit better?
I am stuck with grasshopper 5, thats what the users are using, so higher versions are not an option.

The idea here is that after grasshopper starts I register to this event and I track what objects our users are inserting to their projects

Don’t forget to unregister the DocumentAdded event, you don’t want to get called every time a document is loaded.

It would technically be possible to load a document before the UI has been invoked. I think none of the standard commands allow for this, but it’s probably possible through code.

David,

Where do you suggest i do this?

Inside the handler, when it gets called the first time.

void DocumentAdded(GH_DocumentServer documentServer, GH_Document document)
{
  Grasshopper.Instances.DocumentServer.DocumentAdded -= DocumentAdded;
   GH_Document grasshopperDocument = document;
  grasshopperDocument.ObjectsAdded -= addSourceChangeHandlerToNewlyAddedObjects;
  grasshopperDocument.ObjectsAdded += addSourceChangeHandlerToNewlyAddedObjects;
}

?

Hang on. If you’re listening to ObjectAdded events on the GH_Documents directly then this is a weird approach. I thought you were handling the ObjectAdded events that the canvas relays from whatever document it is currently showing. Because there is only ever one canvas so you don’t have to worry about oversubscribing or unsubscribing from events, whereas there can be lots of documents coming into and going out of existence all the time.