Close file using NSWorkspace

前端 未结 1 385
时光说笑
时光说笑 2021-01-23 14:24

My requirement is to open a file like pdf/ppt etc and close it after 20 minutes. I am able to open the files appropriately thanks to NSWorkspace. But how can I close the files ?

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 15:17

    NSWorkspace just asks the operating system to open the file, it doesn't maintain any connection to the file at all. The OS will open the file with whatever the default application is for that file type, unless you specify a particular application to use. Anything that happens with the file after that point is completely outside of your application's control because the file is being managed by another process.

    You would need to use either AppleScript/Apple Events or the Accessibility Framework to control whatever application currently has the file open and ask it to close the file, but there is no guarantee that it will do so.

    The only way to guarantee that you will be able to close the file is to open it in your app. Obviously, this is impossible if you need to open a variety of diverse and possibly proprietary file types.

    0 讨论(0)
提交回复
热议问题