Click menu item on Mac OSX Lion using AppleScript - applescript

I have a problem using a simple AppleScript on Mac OSX 10.7.3.
With the following simple AppleScript which I find everywhere OSX raises an error 'The action "Run AppleScript" encountered an error'
I open up the Automator, create a Service, drop in a "Run AppleScript" node and enter the following code which I assume is correct because as I said it is the way a lot of people are doing it without any complaints.
AppleScript:
tell application "Terminal" to activate
tell application "System Events"
tell process "Terminal"
click menu item "New Window" of menu "Shell" of menu bar 1
tell application "Terminal" to close the front window
end tell
end tell
EDIT: When running in Automator I also get an error description:
Run AppleScript failes -1 error
Access for assistive devices is disabled"

Is Enable access for assistive devices enabled? If so, have you tried to reenable it?

Well, I guess I'll answer the question anyways (and thanks for editing your question to give a bit more useful detail).
Go to the "Universal Access" pane in System Preferences and at the bottom of the "Seeing" tab, you'll see a "Enable access for assistive devices" checkbox.
Turn that on and I suspect Automator will work.

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.

How to grant Applescript permissions through Applescript?

If I give my Applescript to another person, they would have to manually allow Applescript control of their computer by going into System Preferences, clicking Security & Privacy, then clicking Privacy and then Accessibility, then finally add Applescript. Is there anyway I can make Applescript add its self so they don't have to? Is there another solution? Because without them doing this, Applescript can't click things.
Without applescript added so it can have control, I get the error, "Script Editor is not allowed assistive access."
I believe that your are trying to enable assistive access for devices. For instance, GUI scripting requires this to be enabled. You can't enable it directly, but you can point the user to the place they need to go. The following finds out if assistive access is enabled and brings up the System Preference pane where the option is.
tell application "System Events"
set UI_enabled to UI elements enabled
end tell
if UI_enabled is false then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "This script utilizes the built-in Graphic User Interface Scripting architecture of Mac OS x which is currently disabled." & return & return & "You can activate GUI Scripting by selecting the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 1 buttons {"Cancel"} default button 1
end tell
end if
That should be enough for your users to get the hint.
You can theoretically do it via the command line:
https://raymii.org/s/snippets/OS-X-Enable-Access-for-assistive-devices-via-command-line.html
For example, in Lion and Mountain Lion it's a simple as this:
touch /private/var/db/.AccessibilityAPIEnabled

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/

Resources