How can i make it aware of the selection being changed, so that it will trigger itself whenever it spots a change?
A quick look at the API docs reveals that we have no selection_in_pattern_observable
Usually, the ability for a script to get notified when a value has changes is indicated by the presence of an accompanying variable with the _observable suffix.
The alternative is a bit messy, but entirely do-able. It would basically involve hooking your script onto an idle notifier, and use this to check (several times per second) if the selection has changed:
-- Invoked periodically in the background, more often when the work load
-- is low. less often when Renoises work load is high.
-- The exact interval is not defined and can not be relied on, but will be
-- around 10 times per sec.
-- You can do stuff in the background without blocking the application here.
-- Be gentle and don't do CPU heavy stuff in your notifier!
Note that even if the code above says "don't do CPU intensive stuff", lua is pretty damn efficient :-)
Checking for a changed selection would not add much overhead at all - just make sure your code is relatively efficient, and you will be fine.
Finally, I'd suggest that you add this (that the selection becomes observable) to our API wishlist thread.