Applescript activate closed window - macos

how can I activate an application and make it open the "standard" window as if I would have clicked on the dock icon with applescript?
E.g. I am in iTunes, close the window with command-w open another application and then I click on the iTunes dock icon and iTunes becomes the frontmost application and opens up it's "standard" iTunes window.
When I want to simulate that with applescript I type:
tell application iTunes to activate
What happens then is, that iTunes becomes the frontmost application, but the "standard" window (in that case the iTunes window) isn't being opened.
Does anyone know about a way to open the "standard" window with a general approach for any application?
Thanks b00tsy

after a long time I found out that what I expect to happen with
tell application "anyApplication" activate
actually happens with
tell application "anyApplication" reopen

There is no true, single way to show a "standard" window. It is up to the developer of each application to decide how a window is to be created and shown. For example, here is iTunes and Microsoft Word 2008...
tell application "iTunes"
activate
set theBrowser to browser window 1
set visible of theBrowser to true
end tell
tell application "Microsoft Word"
set newDocument to make new document
end tell
In iTunes, there are three "standard" windows—browser, EQ, and playlist—and browser and EQ always exist. But in Microsoft Word, however, we have to make a new document, which is as close to a "standard" as it gets. Most applications will work the same way Word does, but it isn't a guarantee and you will have to look at the Dictionary for a given application to see how it handles windows (although iTunes does have a window class to throw everyone off, but that's Applescript for ya...).
One more thing to note...in most cases, once a window is closed, the only way to reopen it is to open the file that populated the window you just created or simply create a new one. iTunes is rare in its implementation.

Related

Opening a new Safari window with Alfred AppleScript opens 2 windows

When I run this script from Alfred (using the text "nsafari", and Safari is quit (not in the dock), two Safari windows will pop up. When I run it from the Script Editor, it will sometimes open up two windows, but sometimes not. (This also happens with my new Safari window script as well).
if application "Safari" is running then
tell application "Safari"
make new document
activate
end tell
else
tell application "Safari" to activate
end if
Why does it open two windows only from Afred? And how do I make it only create one?
I don't know why, but after two years, the problem I reported above does not seem to happen any more...
(and yes this is not a very satisfying answer)

How to open, close and close the dialogs of another application using cocoa or applescript?

I want to open another separate application, open the projects/documents of that application in a iterative way and then close the application. I also want to close all the modal and non modals dialogs which popped up during the opening of the document. I want to close all the dialogs including the crash dialog in case the application fails/ crashes.
What will be the best way using cocoa or applescript to achieve this and from where i can get more detailed information?
If the app has a scripting interface, of course the best way is to do that.
You generally don't want to iterate in AppleScript, but rather to operate on all of the results of a query.
For example, for almost any application that implements the "standard suite", you can just:
tell app "TextEdit" to close windows
This is much simpler (and faster, and more likely to be implemented correctly in the target app) than:
tell app "TextEdit"
repeat with theWindow in windows
close theWindow
end repeat
end tell
Of course this may pop up save/abandon changes dialogs, and it may skip over or include dialogs and inspectors, and so on, depending on the application's user model.
More importantly, it won't work if the app doesn't support scripting (and the standard suite).
Also, it won't help at all with closing a crash report—that window is owned by CrashReporter, not the original application (which is a good thing, because you can't talk to the original application anymore, now that it's crashed…).
The alternative is the UI Scripting features in System Events. This will only work if assistive access is enabled. It can also be a bit fiddly to figure out which windows are the ones you want to deal with, and which controls are the ones you want.
For example:
tell app "System Events"
click button 1 of windows of application process "TextEdit"
end tell
This works by finding every window (no matter what kind) owned by the TextEdit process, and simulating a click on the first button in that window (the red close button).
If you google "AppleScript UI Scripting" you should find lots of different guides. The first hit I found was http://www.makeuseof.com/tag/applescripts-ui-scripting-mac/ and it looks like a decent place to start.

AppleScript code that clicks a button in NSAlert

I have an mac application that once opened (in awakeFromNib) shows the user an NSAlert with two buttons, one with "Option1", the other with "Option2". I would like to automate the following flow:
Open the application from the Applications folder.
Choose "Option2" by clicking the button in the NSAlert
So far I've managed to do only the first part:
tell application "Finder"
activate
make new Finder window to startup disk
open application file "My Application.app" of folder "Applications" of startup disk
close Finder window 1
end tell
Can anybody help me with the code? I understand that I can use the System Events commands to catch this click event, but can't manage to find any help online for clicking a button in an NSAlert, and not in a regular Window.
You might try this...
tell application "My Application" to activate
delay 5
tell application "System Events"
tell process "My Application"
click button "option 2" of window 1
end
end
How many windows do you have open? It's probably getting confused.
If you need to the program UI Browser can generally find the proper terms for GUI Scripting. I'm not sure it's worth buying just for this but you can run the program in demo mode for a month and use it to find the proper terms.
http://pfiddlesoft.com/uibrowser/

Is there a way to get a permanent identifier for any given window

I'm trying to write a script that can control different windows from different programs (set position and size). I've got things just about working but I'm having problem accurately identifying windows. Right now I've got:
tell application "System Events"
tell application "Mail"
set windowName to name of window 1
end tell
end tell
This is a pretty simplified version of what I have working now. I'm grabbing the window information for many different applications and storing them as properties which are being called upon by another script later:
tell application "System Events"
tell application "Mail"
set position of window windowName to valueX
end tell
end tell
This works as long as the name of the window doesn't change. In many other applications I have no problems because window titles don't change (iCal, iChat, etc). In Mail the window title changes depending on how many e-mails are in your e-mail box. If an e-mail comes in between the first part of the script and the last then the script fails.
I can't really refer to the window by it's index number because those change as the order of the windows change (front to back). I thought maybe the window ID would work, but I need my script to work even if an application has been quit and restarted and the ID number changes if the application is relaunched. Am I SOL, or is there something I hadn't thought of?
Not SOL, but you'll have to do a little more work. There's only a few types of windows. You have the main browser window and you have email messages, whether it be a draft they are composing or an email message they're reading. Maybe you're concerned with the preferences window too. So you have to store the type of window. If it's a browser then you also will have to store the currently selected message. If it's an email window then you store the message id too. For browsers you just open a new browser window and restore the selection. For email messages you just open the messages. You'll have to check Mail's dictionary for other types of windows, but the idea will be the same.

Making a another applications's window frontmost and focused

I've been trying to get any given window from another Application to become front most and focused.
Here are the two methods I have tried:
AppleScript
tell application "Safari"
set index of window "Downloads" to 1
activate
end tell
Why it doesn't work: While it seems to change the z-Order, it doesn't change the focus! The previous frontmost window, even if obfuscated, retains focus.
CoreGraphicServices
CGSWindowOrder(cid, wid, kCGSOrderAbove, nil)
Why it doesn't work: Without being the Universal Owner, you can't change the order of a window owned by another process. In this case, making myself the Universal Owner is not an option -- I don't want to depose the Dock.
Recap
I need to make a window frontmost and focused from one process to another. AppleScript fails to focus, and CGS isn't an option since I don't own the window.
Any solutions/ideas?
Try using the Accessibility API to send an AXPress action to the AXWindow. Obviously, the user will need to have access for assistive devices turned on.

Resources