Applescript delay issue - applescript

I am testing applescripts that I will use later in my OSX app.
I'm getting a 6 sec delay after the click button command below.
After some research it seems that this is a known issue.
What I find interesting is, if i use the commercial app QuicKeys to perform the same
button click there is no delay, so I assume they found a work around.
Anybody have any ideas?
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
click button "Track List pop-up" of window 1
-- 6 seconds delay before next command is sent
key code 36 -- return key stroke
end tell
end tell

Was having the same problem and resolved it by enclosing the click causing delay in the ignoring application responses block. Here is a quick summary:
OLD CODE (Causes 6 sec delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
NEW CODE (No delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
Please check detailed answer in the thread listed below.
Speed up AppleScript UI scripting?
Hope this helps.

It seems click or axpress causes a big delay.
Instead - get position and use a third party shell script to do the clicking. Much Much faster.
using clicclik : https://www.bluem.net/en/mac/cliclick/
put in user library/application support/Click
set clickCommandPath to ((path to application support from user domain) as string) & "Click:cliclick"
set clickCommandPosix to POSIX path of clickCommandPath
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
tell button "Track List pop-up" of window 1
set {xPosition, yPosition} to position
set x to xPosition
set y to yPosition
end tell
do shell script quoted form of clickCommandPosix & " c:" & xPosition & "," & yPosition
key code 36 -- return key stroke
end tell
end tell

Related

Mac OSX11 (BigSur) - changing Region / Country (locale) via appleScript

I need to automate the change of locale (Region/Country) for format purpose from:
Region: Americas
Country: United States
to
Region: Europe
Country: Spain
So I have the following code, which seems to work up to the point where the system asks to restart the machine (or not).
property theSettings : {"", ""}
set settings1 to {"Americas", "United States"}
set settings2 to {"Europe", "Spain"}
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences"
reveal anchor "Language" of ¬
pane id "com.apple.Localization"
activate
end tell
tell application "System Events"
tell window "Language & Region" of process "System Preferences"
if (value of pop up button "Region:" of tab group 1) is "United States" then
set theSettings to settings2
else
set theSettings to settings1
end if
set {theRegion, theCountry} to theSettings
tell pop up button "Region:" of tab group 1
delay 0.25
click
delay 0.25
click menu item theRegion of menu 1
delay 0.25
click menu item theCountry of menu 1 of menu item theRegion of menu 1
end tell
end tell
delay 1
tell application "System Preferences" to quit
tell sheet 1 of window "Language & Region" of process "System Preferences"
delay 1
keystroke tab
delay 1
keystroke return
end tell
end tell
In the script above, after switching locale, I am trying to click the button Don't Restart. But this does not work as the sheet window does not seem to be active.
Any idea on how to save the settings?
This issue is, at that point the sheet is modal and typically requires physical intervention. In other words, based on its current appearance, normally one would either press enter/return key to accept the default or use the mouse to click either Don’t Restart or Cancel as tabbing in not active by default in this use case.
However, if the [√] Use keyboard navigation to move focus between controls checkbox in System Preferences > Keyboard > Shortcuts is checked, which it is not by default, then one could simply press the space bar as Don’t Restart would have focus even though Restart Now would be, by default, solid blue.
To get past the modality of the situation, one would have to add code that would programmatically check to see that the aforementioned checkbox is checked, and if not check it.
In addition to the check and taking action if necessary, the modifications to your existing code are as follows:
In between:
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
And:
tell application "System Preferences"
reveal anchor "Language" of ¬
pane id "com.apple.Localization"
activate
end tell
Add:
-- # Get the fully qualified POSIX pathname of the target .plist file.
set thePropertyListFilePath to ¬
the POSIX path of ¬
(path to preferences from user domain as string) & ¬
".GlobalPreferences.plist"
-- # Get the value of AppleKeyboardUIMode to determine if the
-- # 'Use keyboard navigation to move focus between controls'
-- # checkbox is checked on the System Preferences >
-- # Keyboard > Shortcuts tab.
tell application "System Events" to ¬
tell the property list file thePropertyListFilePath to ¬
set keyboardNavigation to the value of ¬
the property list item "AppleKeyboardUIMode"
if keyboardNavigation = 0 then
-- # Check the checkbox.
my toggleKeyboardNavigation()
end if
And to the end of the script add:
-- # Handler #
-- # Toggles checkbox: 'Use keyboard navigation
-- # to move focus between controls'
on toggleKeyboardNavigation()
tell application "System Preferences"
activate
reveal anchor "shortcutsTab" of ¬
pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell front window of ¬
application process "System Preferences"
set i to 0
repeat until (exists checkbox 1 of tab group 1)
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
click checkbox 1 of tab group 1
end tell
end tell
end toggleKeyboardNavigation
After making the aforementioned changes, then starting with:
tell application "System Preferences" to quit
Replace it and the ensuing tell block with:
ignoring application responses
tell application "System Preferences" to quit
end ignoring
delay 1
key code 49 -- # space key
Then after the end tell of the primary tell application "System Events" block, the one just below the previous code change, add the following:
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
if keyboardNavigation = 0 then
-- # Uncheck the checkbox if it
-- # was previously unchecked.
my toggleKeyboardNavigation()
delay 0.2
tell application "System Preferences" to quit
end if
Notes:
The example AppleScript code shown herein to check to see if [] Use keyboard navigation to move focus between controls in System Preferences > Keyboard > Shortcuts is checked, can be done differently by opening to that pane and ascertaining whether of not it's checked and act accordingly. The reason I've added the code for it as is, is because I've used it to reset that checkbox later on in the script in the past. If the checkbox is already checked then one doesn't have to see the extra changing of panes.

Notes export all as PDF

Ability to export all Notes in macOS Notes.app as PDFs.
execution error: Notes got an error: AppleEvent handler failed. (-10000)
Multiple scripts, latest below.
tell application "Notes"
activate
repeat with theFolder in every folder
repeat with theNote in every note of theFolder
tell application "System Events"
tell process "Notes"
set dockPrefs to dock preferences
set appearancePrefs to appearance preferences
delay 1
display dialog "Foo"
tell menu bar 1 of process "Notes"
click menu bar item "File"
click menu item "Export as PDF..." of menu "File" of menu bar of process "Notes"
end tell
click button "Save" of sheet 1 of window "Notes" of process "Notes"
delay 1
key code 125
end tell
end tell
end repeat
end repeat
end tell
execution error: Notes got an error: AppleEvent handler failed. (-10000)
There are a few problems in there:
You are already targeting the Notes process, so including that in
the click statements is adding another process target - use one or the other, but if you are
doing a lot of menu clicks you might look at using a general
purpose handler;
The export menu item uses an ellipse (a single character), not three
periods;
By placing a display dialog statement in the System Events tell statement, you are moving the focus away from the application.
Also note that the text field is selected and the save button is the default in the sheet, so you can use keystrokes instead of trying to click UI elements. A cleaned up example (tested in Mojave) would look something like:
tell application "Notes"
launch -- seems to work better than 'activate'
repeat with aFolder in folders
repeat with aNote in notes of aFolder
set noteName to (name of aNote)
try -- keep the name a reasonable length
set noteName to text 1 thru 20 of noteName
end try
tell (current date) to set timeStamp to text 2 thru -1 of (get (1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) -- hhmmss
tell application "System Events"
#display dialog noteName -- testing?
tell process "Notes"
set frontmost to true -- retarget the Notes app
delay 0.5
click menu item "Export as PDF…" of menu "File" of menu bar item "File" of menu bar 1
repeat until exists sheet 1 of window 1 -- wait for the sheet
delay 0.02
end repeat
end tell
keystroke noteName & "_" & timeStamp -- update the name, trying to avoid duplicates
delay 0.5
keystroke return -- dismiss the sheet
delay 0.5
key code 125
end tell
end repeat
end repeat
end tell

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.

How to take user voice input in applescript

I want to be able to take in input from the user in a program and have them speak it instead of type it into a text box. Is there any way to do this?
Any help would be appreciated.....
If you change the shortcut used to start dictation (System Preferences > Keyboard > Dictation > Shortcut) to either Press Left Command Key Twice or Press Either Command Key Twice, you can trigger this shortcut using System Events (provided the necessary accessibility privileges are granted):
delay 0.1
tell application "System Events" to repeat 2 times
key down command
key up command
end repeat
Alternatively, you can trigger the menu item:
tell application "System Events" to tell ¬
(the first process whose frontmost is true) to tell ¬
menu bar 1 to tell ¬
menu bar item "Edit" to tell ¬
menu "Edit" to tell ¬
menu item "Start Dictation" to ¬
if exists then click it
Edited to correct the referencing of the menu item chain, as highlighted by #wch1zpink, which previously prevented it working in *Google Chrome*.
#CJK really deserves the vote love, as this is only an adjusted extension of his thorough answer...
As taken from #CJK 's solution...
"Alternatively, you can trigger the menu item:"
tell application "System Events" to tell ¬
(the first process whose frontmost is true) to tell ¬
menu bar 1 to tell ¬
menu "Edit" to tell ¬
menu item "Start Dictation" to ¬
if exists then click it
Worked in almtost every application I tested the above code with... It did not work in Google Chrome.
However, this minor adjustment to his original code, Will work in Google Chrome.
tell application "System Events" to tell (the first process whose frontmost is true)
tell menu bar 1 to tell menu bar item "Edit"
tell menu "Edit"
try
click menu item "Start Dictation"
end try
end tell
end tell
end tell
For either of these solutions to actually "Enable Dictation" the mouse cursor must be in a field which allows text input, before the code is run.

Speed up AppleScript UI scripting?

I'm using NetShade as a proxy service and thought I could try to automate the switching between the different proxies as a nice start for my first AppleScript script.
The NetShade-app has no AppleScript support, so I have to use UI scripting. After a few tries (and some posts here) I managed to have a script, that switches the proxies via the menu bar item (here is a picture of it, since I can't post it inline due to reputation limit).
Unfortunately my code is extremely slow (≈6sec), which makes it kind of impractical as a script. The first menu opens immediately, but the selection of the sub-menu and the proxy server takes several seconds.
I'm using the following code:
set theProxy to "Netshade US 4"
tell application "System Events" to tell process "NetShade"
tell menu bar item 1 of menu bar 2
click
tell menu item "NetShade Proxy" of menu 1
click
tell menu item theProxy of menu 1
click
end tell
end tell
end tell
end tell
I already tried to add ignoring application responses, like suggested in a different thread (link), but that didn't help.
So finally my questions:
Is there a way to speed the process up? Maybe even a way to do all this in the background, without showing the menu items?
P.S.: I'm running OS X 10.9.1
Summary of the fix
To remove delay you need to do two things:
(I) Identify the click which is causing the delay and enclose only that line in the ignoring application responses block as shown below. In my case, it was click bt after which the execution was going into a wait mode for 5 to 6 seconds.
ignoring application responses
click bt
end ignoring
(II) I then also had to kill System Events to and start it again using the following commands.
do shell script "killall System\\ Events"
delay 0.1
-- Rest of the code to click stuff or send keycodes
This resolved the delay issue.
Details
I was having the same problem where I created a script to connect/disconnect my bluetooth headset through AppleScript. The script is given below.
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
The script was working fine but had a problem where it would wait for 5 to 6 seconds after executing "click bt" above. I modified the code as follows and it is working absolutely fine now without any delay.
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell

Resources