AppleScript - how to close "Your computer was restarted because of a problem" popup? - macos

How to close the "Your computer was restarted because of a problem" popup in AppleScript?
We use a VM to execute e2e tests and they often fail because of this popup. I would like to programmatically close it.
I tried listing the windows based on https://stackoverflow.com/a/26196136, but the popup is not included in the list

tell application "System Events"
tell application process "Diagnostics Reporter"
if (exists window 1) then click button "Ignore" of window 1
end tell
end tell

Related

How to quit Xcode with confirmation dialog box using apple script

I'm trying to do a script to quit Xcode with currently building application. My current script only cover to quit Xcode but I'm unable to handle the confirmation dialog box that pop-ups on the Xcode. On the confirmation box, there are two button options: Cancel or Stop Tasks. I wanted to click on Stop Tasks button. Please help.
Here's my current AppleScript so far:
set apps to {"Simulator","Xcode"}
repeat with thisApp in apps
tell application thisApp to quit
end repeat
tell application "System Events"
tell process "Xcode"
click button "Stop Tasks" of sheet 1 of window 1
end tell
end tell
This is my current applescript that didn't work.
I'm able to fix my issue regarding terminating Xcode with confirmation dialog box prompt. Here's my the applescript I've come up:
try
with timeout of 10 seconds
quit application "Simulator"
quit application "Xcode"
end timeout
on error number e
if e is -1712 then
activate application "Xcode"
tell application "System Events"
tell application process "Xcode"
keystroke return
end tell
end tell
end if
end try

OS X Programmatically Click Menu Item of Dock Menu

This is my dock menu:
I would like to programmatically click that button "Show Most Recent Window". Can this programmatically be done using Cocoa or CoreFoundation?
I know the PID the dock item is associated with.
There are many ways to achieve this, although generally you can easily set an AppleScript or oascript that can handle this. Basically it involves using AXRaise, which essentially calls on the function to raise the frontmost window of the application specified.
Code:
set mostrecentWindow to "mostrecentWindow"
set theApplication to "Safari"
tell application "System Events"
if exists process theApplication then
--raise frontmost window
tell process theApplication
set frontmost to true
perform action "AXRaise" of (windows whose title is mostrecentWindow)
end tell
else if not (exists process theApplication) then
--display alert
display alert "Warning: process " & theApplication & " is not running"
end if
end tell
The above example checks whether or not the process Safari is running, and if it is then raise it's most recent (or frontmost) window to the foreground; otherwise show a warning that the process is not running.
It sounds like a task that can be done using GUI Scripting using AppleScript.

How to close a floating window using AppleScript?

One of my application processes occasionally creates a floating window that I'd like to programmatically auto-close.
I have no problem checking for the existence of this window through its name, but then I am unable to close it with neither close window "windowname" nor tell window "windowname" to close.
E.g.:
tell application "System Events" to tell process "processname"
if exists window "windowname" then
close window "windowname"
end if
end tell
This result in:
error "System Events got an error: window "windowname" of process "processname" doesn’t understand the “close” message."
How can I close this window, then?
I can close a Finder window like this. Maybe you can do the same.
tell application "System Events"
tell process "Finder"
click button 1 of window 1
end tell
end tell
If "Command-W" closes the floating window, which it usually does, this will work:
tell application "System Events"
if exists window "<window name>" of process "<application name>" then
keystroke "w" using command down
end if
end tell
You can also use Automator instead of AppleScript proper to do this. Record yourself clicking the close button.
When I do this, Automator records the action as, I kid you not, Click the "<fill in title>" button.

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/

How to reload a Safari Extension from the command line?

I need to "Reload" a Safari extension from the command line (and also build the package later).
How can this be done?
Why?
I'd like to build in one step - my code is in CoffeeScript and thus I'm compiling it anyway.
What have I tried?
Apart from googling hopelessly I tried using this AppleScript:
tell application "System Events"
tell process "Safari"
click the button "Reload" of window "Extension Builder"
end tell
end tell
Which errors out with:
System Events got an error: Can’t get button "Reload" of window "Extension Builder" of process "Safari".
And this variation:
tell application "System Events"
tell process "Safari"
tell button "Reload" of window "Extension Builder" to perform action
end tell
end tell
Which doesn't give an error but also doesn't actually do anything.
It may be that the button isn't "named" as you expect. Check out UI Browser in order to view an application's interface hierarchy for use with GUI scripting.
The "Reload" button is accessible with:
click button "Reload" of UI element "ReloadUninstall[NAME OF EXTENSION HERE]" \
of UI element 1 of scroll area 1 of window "Extension Builder"
Extension Browser's view hierarchy seems to have changed since the accepted answer was written. This script works for me:
tell application "System Events"
tell process "Safari"
click button "Reload" of UI element 0 of window "Extension Builder"
end tell
end tell
Note that Safari and the Extension Builder window need to be open for this to work

Resources