AppleScript code that clicks a button in NSAlert - macos

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/

Related

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

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

AppleScript, why does first "display alert" work but then cause AppleScript icon to bounce, waiting for me to click it until I get another dialog?

NOTE: I do not have Admin privileges, and cannot use things that require assistance or accessibility or whatever it is!
I'm basically trying to write an execution of a script that sets up my macOS Desktop by opening apps, positioning them etc
Part of that is allowing me to click continue on a dialogue after doing certain things I have to do manually as a result of the limitation (positioning a few apps for example).
I can successfully have the first alert pop up, and when I press OK, it executes the task, but then the 2nd alert is hidden behind AppleScript and will not pop up until I click AppleScript. Anyone know why?
Heres what I have, and please keep in mind I do not have Admin privileges, which means I can't use System Events:
tell application "Script Editor"
set miniaturized of window 1 to true
end tell
display alert "App is opening. Make sure you log-out of Apple Connect!"
delay 3
tell application "Safari"
activate
make new document
set the bounds of the front window to {1077, 23, 1581, 464}
tell window 1
set URL of tab 1 to "http://appwebsite.com"
end tell
end tell
display alert "Set up App, sign into Apple then click OK to open all other Safari windows"
delay 1
tell application "Test Task App" to activate
Place an activate command before the display alert command so Script Editor has focus and the display alert command will not be hidden.
Example:
activate
display alert "Set up App, sign into Apple then click OK to open all other Safari windows"
You do not need it before the first one as Script Editor has focus with it runs the first display alert command, but once you activate Safari, the display alert command is behind Safari. So if you use an activate command before the display alert command, it brings it forward in front of, in this case, Safari.

Accessing an Applescript from 2 different menus produces different results. Why?

I've got an application that has an embedded Script menu for running Applescripts. However, if you try to run scripts with certain functions (mostly UI related) they won't work unless you run them from the System script menu (in menu bar).
For example, if in theApplication you say :
tell application "System Events" to tell application process "theApp" to get all windows
it will return an empty list if run from the program's script menu, but 2 if run from the system script menu. I've also tried:
tell application "System Events"
tell application process "theApp"
set allElements to UI elements
display dialog (count of allElements)
end tell
end tell
-- returns 2 when run from System script menu but 0 when run from within theApp.
Also if you run from the Script Editor it will work fine. GUI scripting is enabled for the application in System Preferences, so I'm curious as to why this is happening, and any workarounds for it (other than run the script from the System Script menu)? The Dictionary shows Standard Suite so it should have access to windows...
Any ideas?
I believe you haven't provisioned the app in question to use assistive devices.
You should really open System Preferences, then click the button for "Security", then unlock, and click the button for Assitive Devices, (the blue icon with a white man), then drag your app onto it (reveal your app from its Dock menu), and permit it to control your machine, remember to lock the pane afterwards.
I can not guaranttee this to work, but it is certainly worth a try.

Access context menu items by name using AppleScript

I'm trying to use AppleScript to click on context menu items in Logic Pro, preferably by simply providing the name of the menu item. It seems like this should be possible because I'm able to set up keyboard shortcuts for these context menu items using system preferences and providing the name of the command.
For instance, if you right click on the main editing window in Logic, a menu pops up with an option called "Add Audio File..." If I create a system preferences keyboard shortcut for Logic and give it this menu item name, it's able to execute just fine. I'd like to recreate this with a script. I'm familiar with accessing normal menu items using the hierarchy like so:
tell process "Logic Pro"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "Save"
but as far as I know, there's no way to access the context menu (right click menu) that I want like this. It seems there should be a way to simply access a non-menu-bar menu item by name since system preferences is obviously able to do so.
Logic pro is not scriptable so my suggestion would be that you set a keyboard shortcut in the system preferences then use system events to use said shortcuts.
for example to enter find mode (assuming there is a find mode since i don't own Logic Pro)
tell application "Logic Pro" to activate
tell application "System Events"
tell application process "Logic Pro"
keystroke "f" using command down
end tell
end tell
I don’t think you need to use the context menus. “Add Audio File…” is available in other parts of the Logic Pro X user interface. If you open the Project Audio window, there is an “Audio File” menu button with an “Add Audio File…” button in it. So this AppleScript will activate the “Add Audio File…” command:
tell application "System Events"
tell application "Logic Pro X" to activate
tell process "Logic Pro X"
tell menu bar 1
click menu item "Open Project Audio" of menu "Window"
end tell
delay 1
tell window 1
click menu button "Audio File" of group 1 of group 1
click menu item "Add Audio File..." of menu 1 of menu button "Audio File" of group 1 of group 1
end tell
end tell
end tell
One thing to keep in mind if distributing a GUI script is that the above script will only work in Logic Pro X running on a Mac set to US English (and maybe other kinds of English) because the names of the menus change if the system is set to another language. What you can do is replace the names in the above script with numbers, which is a totally experimental process, as far as I know. You have to try different numbers and see which continues to work.
So you may be able to replace:
menu button "Audio File" of group 1 of group 1
… in the above script with:
menu button 1 of group 1 of group 1
… and get the same functionality, and the script would work on any Mac. Or you may need to use “menu button 2.” Same goes for the other named items in the above script.
Also keep in mind that the user you distribute this script to has to give System Events permission to control their Mac in the Security pane of System Preferences or this script won’t work. That can be a giant obstacle to distributing GUI scripts. And if you save your script as an Application, you have to digitally sign it or it won’t run on other people’s computers, and that can be complicated.

How can I automate application installations that have dialog boxes that need input?

I am trying to install the downloaded application programatically, hence I am using apple script for the same since am working on Mac.
Here I've downloaded the Adobe AIR Installer app from adobe site which is in downloads folder and am trying to install the same with the below applescript code. Here when the Adobe AIR setup dialog box opens, I need to click on "I Agree" button to install the app.
With the below code am not able to click on the "I Agree" button, even i dont have Xcode Accessibility Inspecter do inspect the element. Pls guide me the script to handle the "I agree" button.
try
tell application "Finder"
activate
open application file "Adobe AIR Installer.app" of folder "Applications" of startup disk
set this_image to open this_file
delay 5
tell application "System Events"
click button "I Agree" of window "Adobe AIR Setup"
end tell
end tell
end try
You could try GUI scripting the commands with delays in-between clicks, but it's a little bit of work. Since the Adobe installer doesn't support clicking from applescript, you will need to download ExtraSuite and follow along with this previous MacScripter post.
To get pixel coordinates, start up the installer without AppleScript and type '⌘ + shift + 4' to get the crosshairs with pixel location (usually used for taking screenshots). Write down the coordinates for the first button and then hit 'esc' and move to the next button.
This is a pretty painful way to get the job done but there aren't many other alternatives that I know of.
It looks like the Adobe installers don't use Cocoa and aren't scriptable. You might have better luck with Sikuli. It's based on matching bitmaps and doesn't depend on any underlying framework.

Resources