how to know the action than when user switch to another document
There isn't a JS Action to detect this: https://developer.sketch.com/reference/action/
If you make a native plugin then you can likely hook into the native notifications and find a useful one to hook into. Ive tried making it work in JS in the past but I don't think its working anymore. https://sketchplugins.com/d/1388-how-to-get-a-list-of-nsnotifications/3
thank you. i will try it .