how to activate window, not app?

I'm trying to write a very simple applescript to just create a new
Terminal window and set it to be frontmost (i.e. so it has focus)
without raising all the other Terminal I have open. The following
creates the new Terminal window but results in all the other Terminal
windows I have open behind my other windows being raised, which I'm
trying to avoid:

tell application "Terminal"
activate
do script ""
end tell


I stumbled on an old thread that seemed to be asking the same
question at http://groups.google.com/group/alt.comp.lang.applescript/browse_frm/thread/31a26 61b7ee29f06, but I don't see how the example given accomplishes this. Can anyone help?

Thanks!
Josh

[Note: Cross posted from http://groups.google.com/group/alt.comp.lang.applescript/browse_frm/thread/b1fee 62777be00e2, as I haven't had any responses on the newsgroup.]

MacBook5,1, Mac OS X (10.6.1)

Posted on Nov 2, 2009 1:37 PM

Reply
Question marked as Top-ranking reply

Posted on Nov 2, 2009 2:10 PM

--Does this help?


tell application "Terminal"
set visible of every window whose visible is true to false
do script ""
end tell


-- to recover the original visible window(s), add this (the 5 second delay gives you time to assess the first part of the script):


delay 5
tell application "Terminal"
set visible of every window whose visible is false to true
end tell


-- I think there can be a number of variations on this entire approach. I find that if I quit and then relaunch Terminal, the new Terminal window is not there (or not visible), but the original window(s) are once again visible.
20 replies

Nov 4, 2009 2:54 AM in response to jab630

BTW, my most recent suggestion (all of my suggestions in this thread, in fact) were worked out in 10.4.11. But I see that both you and Pierre L. are using 10.6.1. I have that, but have yet to install it on my Intel machine, so I can't do any testing with it. Although I don't know of anything specific,
conceivably there are issues that would make my suggestions incompatible. So let me know what happens, + or -.

Incidentally, the whole intent of the two-app approach is to once and for all take the focus away from the script editor.

Nov 5, 2009 4:38 PM in response to Pierre L.


"I was also trying to say that it seems impossible to me, using AppleScript (and even GUI scripting), to activate any application without automatically bringing all of its windows to the front."


Just curious. Did you try the "two-script" or "two-app" approach? One might argue that the "invisible" Terminal window(s) is indeed "activated, but would it make a difference?

(I still don't know whether the two scripts will execute in 10.6.1. If they don't, it's probably has something to do with getting the PID of the first script.)

Message was edited by: Noaks

Nov 5, 2009 4:46 PM in response to Noaks

I did try your scripts.

Three stickies, three Terminal windows and one TextEdit window were already opened when I launched Starter_.

07 Stickies #3
06 Stickies #2
05 Stickies #1
04 Terminal #3
03 Terminal #2
02 Terminal #1
01 TextEdit

The result? A new Terminal window was created at front, but the three other Terminal windows completely disappeared without any way to retrieve them. Later on, there was the alert “AppleEvent timed out.” Still later on, when I wanted to log out, there was the alert “The variable Starter_PID is not defined.”

Btw, would it be possible for you to improve the HTML code that you use when adding examples to your posts. Using Safari, I am forced to continously scroll back and forth in order to read your posts. Just a suggestion.

Nov 5, 2009 6:15 PM in response to Pierre L.

Thanks very much for the information.

Second things first:

"Btw, would it be possible for you to improve the HTML code that you when you add examples to your posts. Using Safari, I amforced to continously scroll back and forth in order to read your posts. Just a suggestion."

I'm aware of the problem but just haven't found the time to really do it right. If you have a suggestion, I'll give it a try -- it couldn't be much worse.

Now to first things:

Since I've demonstrated (to myself) that the two-app approach works in 10.4.11 and 10.5.7, the issues you're seeing almost certainly have to do with 10.6.x. My guess that such issues would relate to the PID retrieval seems to be correct, but without installing 10.6, I don't know how to approach modified code. (I had similar issues when I messed up the PID retrieval with the earlier os's.) Perhaps you know a much more direct way of getting the PID of "Starter_" that would work in 10.6 –– but it's almost certainly that part of the code in "Finisher_" that's causing the problem, as getting "Starter_" to quit efficiently is crucial.

As to not being able to retrieve the original Terminal window(s), I've consistently found that quitting and then re-activating Terminal does the trick, but maybe not with the PID-retrieval errors. Another approach that works for me is:

'tell app "Terminal" to set the visible of every window whose visible is false to true'. Of course, that would have to be at the end of the script, and the script would have to execute without other errors.

I'll eventually get to 10.6.x, but I'm not particularly enthused about doing that, given all the apparent issues people seem to be encountering.

Thanks again.

Edit: Now, there you go. I didn't use any HTML code in this response, but the no-wrap issue is still there! One more thing to add to my million-item list of things I don't understand.

Message was edited by: Noaks

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to activate window, not app?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.