1

I'm trying to close an open window in Preview by using the terminal. Everything that I have found on the internet suggests using AppleScript instead, but I cannot do such since the plist option does not exist for me (to enable osascript for preview as indicated here on Super User). I am running OS X 10.8.2.

I should note here that I don't really care for AppleScript, and would much prefer a bash solution.

To reiterate, I have a .pdf open in Preview, and I want to close the specific window accessing and displaying the file rather than terminating the entire program. I think this is possible, since there is a way with the OS to kill the window without bash.

I would also like to know how the OS interfaces with the shell when it closes a subprocess (window) of an application.

I can find the open .pdf, process, socket, node, et c. with lsof, but I am hesitant to go further with such information since AFAIK, I can kill the entire process (overkill) or close the port/socket (weird kill) with that information.

Thanks for helping me with this problem. It's really bothering me that my google abilities are failing me.

edit: added in Super User link for osascript enabling to add context.

2
  • man ps then man kill no need to look on the internet. Commented Mar 7, 2013 at 4:20
  • 1
    Yes, but, what signal would I use with kill to close a subunit of an application without terminating the whole application? The only signals I could find listed on the manual are either too blood-thirsty or too ineffective. What I've googled so far has been unhelpful as well. Commented Mar 7, 2013 at 5:06

1 Answer 1

1

The approach that you're taking won't work -- windows within an application are not separate to the degree that you can remotely force one to close without affecting the application as a whole.

Only solution I can think of does involve AppleScript. I'm not sure what you mean by "the plist option does not exist for me".

tell application "System Events" click first button of (first window of process "Preview" whose name is "example.png") end tell 

This is a bit more convoluted than usual because Preview.app is sandboxed, and thus not scriptable via normal methods -- I had to use UI scripting instead. (The "first button" that I'm clicking is the close button on the window.)

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I meant this by plist enabling. Your solution does indeed work, and your workaround is clever, but is there really no way to close a window in a sandboxed app without "clicking" the "first button"?
Not that I'm aware of. As far as I can tell, the application is simply invisible to normal AppleScript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.