Applescript delay so long when run click button command - applescript

i was write a simple code to click share button on Notes applicaion
set upArrow to ASCII character 30
activate application "Notes"
tell application "System Events"
tell process "Notes"
set frontmost to true
click button 2 of group 2 of splitter group of window 1
keystroke upArrow
keystroke return
end tell
end tell
the problem is
click button 2 of group 2 of splitter group 1 of window 1 of application process "Notes" --> missing value
it need 6s to complete run this and return error missing value
but if i dont add keystroke or any command follow that, this code work perfectly

I also get that delay and it returns missing value. But could you click a menu bar item instead?
activate application "Notes"
tell application "System Events" to tell process "Notes"
click menu item "Email" of menu 1 of menu item "Share" of menu "File" of menu bar 1
end tell

set focused of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
or
set value of attribute "AXFocused" of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
Source:
https://lists.apple.com/archives/Accessibility-dev/2006/Oct/msg00013.html

There is a thread that seems to describe the same bug that appears to be limited to specific software. It is on MacScripter.net: Script delays on clicking button (controlling Sys Prefs pane)
Note the most recent post that describes a delay in FileMaker. It seems like the only work-around when software exhibits this bug is to do something else to open the new window. If the only way to run the desired function is via a button, one (terrible) option is clicking at coordinates relative to the corner of the front window. Obviously this will easily fail if the content can move relative to the corner or if a new version of the software moves the button desired. Any other ideas?
UPDATE, 2023-02-13:
In case anybody is still dealing with this bug, here's some useful AppleScript to get the coordinates of the object you'd like to click. You can then feed those into some coordinate-clicking utility, like cliclick (binary), Python code, Keyboard Maestro, and so on.
tell application "System Events"
set someObject to <REFERENCE TO YOUR OBJECT>
-- e.g. op's: button 2 of group 2 of splitter group of window 1 of application process "Notes"
set {xCoord, yCoord} to position of someObject
set {xSize, ySize} to size of someObject
end tell
set objOffset to round (minNum({xSize, ySize}) / 2) rounding down
set xClick to xCoord + objOffset
set yClick to yCoord + objOffset

Related

how to set safari zoom level to 100% from the below shared image/steps with applescript or python script for macos desktop?

safari->preferences->websites->Page Zoom-> click on "when visiting other websites" and make this to 100% if option ranges in number from 50% to 200%
Need an applscript for this.
click on this to see the image where i am looking for the zoom setting to 100%
AppleScript 1: Set Safari default Page Zoom to 100%
tell application "System Events" to tell application process "Safari"
set frontmost to true
keystroke "," using command down -- Open Safari Preferences window
click button "Websites" of toolbar 1 of window 1
-- Need for seeing, selecting or clicking groups (or panes) under Websites window
tell group 1 of group 1 of window "Websites"
select row 5 of table 1 of scroll area 1 of group 1
-- group 1: left pane. row 5: Page Zoom
click pop up button of group 2
-- group 2: right pane. Revealed from the command "UI element" in AppleScript 2 below
click menu item "100%" of menu 1 of pop up button of group 2
end tell
keystroke "w" using {shift down, command down}
-- Or, keystroke "w" using command down: close Preferences window
end tell
MacOS 11.6.8 Big Sur
Explanation: see comments after -- on each lines.
Add a line "delay 1" between action command (Eg: keystroke, click and select) lines if your Mac needs.
You may script changes of Safari and other apps' Preferences in a similar way.
AppleScript 2: Find out all UI elements after some UI actions
To find out which UI elements (such as window, toolbar, button, group, scroll area, table, row, pop up button, menu, menu item) are available after some UI actions, put the command "UI element" under their parent UI element in Script Editor (NOT Automator). Eg:
tell application "System Events" to tell application process "Safari"
set frontmost to true
keystroke "," using command down
click button "Websites" of toolbar 1 of window 1 -- Need
UI element of group 2 of group 1 of group 1 of window "Websites"
end tell
Result: (below the script in Script Editor)
--> {static text "Control the page zoom level on the websites below:" ...,
group 1 ...,
pop up button 1 of group 2 of group 1 of group 1 of window "Websites" of application process "Safari" of application "System Events",
-- Useful in AppleScript 1 above
static text "When visiting other websites:" ...,
button "Remove" ...}
Attention: the action command 'click button "Websites" ...' is required to display the UI elements after it.
Note: Pls encourage new contributor and vote up if you find this answer useful. Thanks!

Click on a specific place on a window, click on a specific button and control if the window changes

Actually I have 3 questions about the same problem: controlling a window with applescript.
What should I do if I would press on button "Close Window" of application "Google Chrome"?
Is it possible to check if the window changes? For example, to see if appear a pop-up or something like that...
What about clicking on a specific place into a window? I mean, I know I can use
tell application "System Events"
click at {x,y}
end tell
but this command use the entire screen as reference system, and I want it works only on a specific window. For example, if at "{x,y}" i put "{1,1}", applescript will click on the first item on the menu bar. Is there a way I can say to "System Events" to click at "{1,1}", but on the window "Google Chrome"?
Here are three examples of how to close the front window of Google Chrome using AppleScript:
Note: The following assumes Google Chrome is running with at least one window open when you test each example AppleScript code in Script Editor.
Example one is the most straight forward way:
tell application "Google Chrome" to close front window
Example two directly clicks the close button:
tell application "System Events" to tell ¬
application process "Google Chrome" to ¬
click button 1 of front window
Example three calculates the center of the close button and clicks there:
activate application "Google Chrome"
delay 0.5
tell application "System Events" to tell ¬
application process "Google Chrome" to tell ¬
front window
set posB1 to (position of button 1)
set szB1 to (size of button 1)
set x to (item 1 of posB1) + (item 1 of szB1) / 2 as integer
set y to (item 2 of posB1) + (item 2 of szB1) / 2 as integer
end tell
tell application "System Events" to click at {x, y}
Note that in the first two examples, the front window of Google Chrome doesn't even need to be the frontmost window on the Desktop; however, with the third example it does, otherwise the click at {x, y} will not go to the intended target.
That said, example three really shouldn't be used when there it a straight forward way, as in example one, to get the job done. Example three was just a proof of concept to get the coordinates to click at. This method may be useful in some fringe cases, especially in an app that doesn't directly support AppleScript.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
In applescript GUI scripting you can simply refer to an element by name or index and tell it to click or to perform an action. For instance to click the close button on the first open window in Chrome you could use:
tell application "System Events"
tell process "Google Chrome"
tell window 1
tell button 1
click
end tell
end tell
end tell
end tell
You don't actually need to know its physical position to click one it; you just need to know that the first button in the window is the close button.
System Events always returns the position of any element in screen pixels, so if you want the position of an element in terms of its window, get the position of the element, get the position of the window, and do some addition or subtraction (e.g., if you want to click at {5,5} in a window whose position is {100, 125}, click at {105, 130})
AppleScript isn't really designed to monitor GUI changes, though if you want to be tricky and you know what change you're looking for you can do something like this:
tell application "System Events"
tell process "..."
tell window 1's pop up button 3
repeat until (exists menu 1)
delay 0.2
end repeat
-- menu 1 now exists, so the pop up button is open
end tell
end tell
end tell
...but note that this will hang the script until the menu is opened. A more elegant way to handle that is to write a script application with an idle handler, like so:
on run
-- whatever initialization is needed
end run
on idle
tell application "System Events"
try
tell process "..."
tell window 1's pop up button 3
if exists menu 1 then
-- menu 1 now exists
-- the pop up button is open
-- do what must be done
end if
end tell
end tell
on error errstr
display alert "Something went wrong" message "The script sent this error: " & errstr
end try
end tell
return 0.2
end idle
You can leave that running in the background watching for specific changes in the GUI (the 'try' statement is in case the app you're watching quits, the window closes, or something unexpected happens to the GUI).
If you haven't already, open the System Events scripting definition in Script Editor and look at the Processes Suite. That will show you all the things you can do with GUI scripting.

UI automation, appleScript Keyboard shortcuts for non-menued items Preview?

I am trying to create a keyboard shortcut action for Preview, namely Draw and Sketch. However, they are are the NON-menued items, which means can't get it done in System Preference I see someone done it from inspiration, it is possible, but when I try to follow alone, here is my code so far and please help me complete this. here are the action.
Here is the error I am facing now
after some researches, does the UI/accessibility inspector help?
activate application "Preview"
delay 0.4
set the menuItem to "Draw"
tell application "System Events"
try
tell application process "Preview" to click radio button menuItem of radio group 1 of splitter group 1 of window 1
on error
try
tell application process "Preview" to click radio button menuItem of radio group 1 of window 1
on error errorM
display dialog errorM
end try
end try
end tell
ideally, trying to make it to work when all bars are hidden,
but if not possible. can we make it to work vwith mark up bar is shown. as below.
This work for me using the latest version of macOS Mojave
tell application "Preview" to activate
repeat while application "Preview" is not running
delay 0.2
end repeat
tell application "System Events"
try
click menu item "Show Markup Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
try
click menu item "Show Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
repeat while not (exists of toolbar 1 of window 1 of application process "Preview")
delay 0.2
end repeat
set description2 to a reference to every checkbox of toolbar 1 of window 1 of application process "Preview"
set theCheckboxes to description of description2
if item 1 of theCheckboxes is "Draw" then
set checkBoxDraw to 1
else
set checkBoxDraw to 2
end if
if item 1 of theCheckboxes is "Sketch" then
set checkBoxSketch to 1
else
set checkBoxSketch to 2
end if
delay 1
-- Below, insert either checkBoxSketch for "Sketch" or checkBoxDraw for "Draw"
click checkbox checkBoxDraw of toolbar 1 of window 1 of application process "Preview"
end tell
-- without these next following lines, the toolbar "Draw" or "Sketch" do not appear to be selected
tell application "Preview" to tell window 1
set visible to false
set visible to true
end tell
delay 3

Applescript - Idein

I'm newbie to Appplescript. I need to automate certain actions on my computer related with my Bluetooth keyboards.
I want to be able to click on the remove or connect button of a keyboard in the following dialog window of the System Preferences Panel.
Dialog window
My code until this moment is as follows:
try
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Set Up Bluetooth Keyboard…" of window "Keyboard"
end tell
end tell
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
click button "remove" of "Home Keyboard"
end tell
end tell
end try
My problem is related with the remove button since is unidentified cell of an unidentified table. With unidentified, I mean without description. Maybe there is an easy solution, but I'm not able to find it. Furthermore, It could happen that more than one keyboard exists, so I need to identify the cell from the Keyboard name.
Do you know any hint related with this issue?
Thanks in advance
Here is a sample script I used to reconnect a specific mouse via Bluetooth :
tell application "System Events"
tell application "System Preferences"
activate
reveal anchor "MouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application process "System Preferences"
click button "Configuration of Bluetooth mouse…" of window 1 -- see note 1
delay 1
select (first row of table 1 of scroll area 1 of sheet 1 of front window whose value of item 1 of static text of UI element 1 contains "Mouse") -- see note 1
get value of item 1 of static text of UI element 1 of row 2 of table 1 of scroll area 1 of sheet 1 of front window
click button "Done" of sheet 1 of front window -- see note 1
end tell
tell application "System Preferences" to quit
end tell
Note 1 : Be careful about the 3 lines with comment 'see note 1' : the value of the string may be different for your local language. Please adjust these 3 values.
I think for keyboard, concept should be very similar. Because it is using GUI scripting, if Apple changes the layout of Bluetooth screen preferences, it must be adjusted. This script works from Yosemite to ElCaptain.I can't test it for next systems.

Selecting Pop Up Menu Buttons in AppleScript

I want to automate clicking a specific pop down menu's item.
For Example, I want to change the Value of "Message receive Sound" to something else. How can I do this with AppleScript? And how can I do this with other pop down menus in AppleScript?
(To open the iMessage Settings menu, shown in the image, type CMD COMMA, once you open iMessage)
Note: I have successfully done this Automator, I just want to do it in applescript.
It's called GUI scripting. You have to identify the reference to the UI element(s).
GUI scripting strongly depends on the system version. If an update changes the UI structure the script will brake.
This selects the sound "Popcorn" in the sound popup menu. It's for El Capitan. In systems < 10.11 the UI elements may be different and the process name might be "iChat"
tell application "System Events"
tell process "Messages"
set frontmost to true
if not (exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")) then
keystroke "," using command down
repeat until exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
delay 0.1
end repeat
end if
tell (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
tell pop up button 4 of group 1
click
delay 0.2
click menu item "Popcorn" of menu 1
end tell
end tell
end tell
end tell

Resources