This is impossible. First, there is no rule trigger for the "open" use case, which is a generic read access that can also not be distinguished between a simple browser download, a CMIS or public ReST API call, or being opened e.g. in MS Office via WebDAV. Similarly, there is no rule trigger for the "close" use case - unless there are actual changes being made, closing a file in a client application may not perform any call back to Alfresco to let it know the file is being closed. And even in the case changes are made, the "save" operation cannot be distinguished between e.g. q quick save after which editing continues, and a "final" save.
If you are dealing with MS Office based files, you would be better off to use Alfresco Office Services to mount a WebDAV-like network drive - in this instance, MS Office will think it is working with a SharePoint server and automatically issue LOCK/UNLOCK commands to prevent co-modification.