Apple Script Error: Can't continue click - macos

I'm trying to open a messaging application (it does not have an Apple Script Dictionary (command + shift + o)), click on text, and type into the text box, and hit send.
Pop up: Script Error - Telegram got an error: Can't continue click after the application becomes active.
Result Tab: error "Telegram got an error: Can’t continue click." number -1708
P.S., The messaging application is Telegram.
Apple Script:
tell application "Telegram"
activate
delay 1
click on text "chat name"
keystroke "some text"
//assuming this works because text box is the first responder when the chat opens.
click on text "Send"
end tell

If an application lacks an AppleScript dictionary, any command except the standard commands launch, activate, open, reopen and quit will throw an error.
The solution is GUI scripting: The built-in application System Events is the bridge to send mouse clicks and keyboard events to the target application.
I don't know the application Telegram at all, so this code might fail, but it might also be a starting point
activate application "Telegram"
tell application "System Events"
tell process "Telegram"
tell window 1
keystroke "some text"
click button "Send"
end tell
end tell
end tell

You have two choices for a 3rd party app that lacks an AppleScript dictionary.
Option 1:
Use System Events as described above to perform an action on an element, e.g. click a button, keystroke text into a field, etc. The trick is to identify the element in syntax that is recognized by Applescript. Besides UIElementInspector mentioned above, which can be confusing and occasionally wrong/incomplete, you can also run the following commands in a separate Applescript Editor. For example, to get all UI elements for the active window (window 1) in Telegram:
tell application "System Events" to tell application process "Telegram" to tell window 1
UI elements
end tell
To get all UI elements for the main menu bar in Telegram:
tell application "System Events" to tell application process "Telegram" to tell menu bar 1
UI elements
end tell
In each case the Result pane will display a comma delimited list of all available UI elements in that window or menu bar. Moreover, the syntax as listed is guaranteed to be recognizable by Applescript. Just identify the correct element and tell System Events to tell it what to do.
For example if you want to click the Menu item "Format" In TextEdit first run the following:
tell application "System Events" to tell application process "TextEdit" to tell menu bar 1
UI elements
end tell
Among the results in the Result pane will be the following:
menu bar item "Format" of menu bar 1 of application process "TextEdit" of application "System Events"
Convert that to Applescript, run the script and it will click the "Format" Menu:
tell application "TextEdit" to activate --you need TexEdit up and running to click its menu bar
tell application "System Events" to click menu bar item "Format" of menu bar 1 of application process "TextEdit"
For submenus, etc. you just iterate the process asking for UI elements for the submenu. GUI scripting is iterative and empirical.
Option 2:
Download the free Terminal/Command Line app cliclick which allows you to click on any point in the screen. The screen coordinates you want to click can be manually identified with your cursor by holding down command + shift + 4.

Related

How can AppleScript be used to send key codes (or an equivalent) directly to an application?

For instance, while the following is able to send multiple keys (shift+h) to the application "System Events", the script won't compile when attempting to send those key codes to an application such as Google Chrome. Is there another way?
tell application "System Events" (* won't work with "Google Chrome" *)
key code 58
key code 4
end tell
You can send key codes via System Events to an application like this:
tell application "System Events"
tell process "Google Chrome"
set frontmost to true
key code 4
key code 14
key code 37
key code 35
end tell
end tell
You can try this by opening Google Chrome first and making sure the focus is on the (empty) URL field and then run the above script - it should type "help" into the URL field.
If you don’t want Chrome to remain in front you can hide it again after sending the key codes.
As "commented", key codes & keystrokes can NOT be sent to BG apps by AppleScript!
It is ONLY possible to trigger some other ("visible"!) UI elements while in background.
To this purpose you must°° can "select" an application without bringing it to front by:
do shell script "open -g /System/Library/PreferencePanes/Keyboard.prefPane/"
… which works even if the app is already "open" in background.
°°I was reminded though: AS can reveal Sys.Prefs in background, other apps can not.
A 2nd alternative in AS is launch which with some(!) apps will work like "open -g …".
As CJK mentions any next command can be sent via AppleScript:
tell application "System Events" to tell process "System Preferences" ¬
to click checkbox 1 of tab group 1 of window 1
I use this feature to toggle some Sys-Prefs settings without losing focus. *
Remember: ONLY commands for "visible elements" (checkboxes, buttons …) will be executed; menu items therefore are excluded (= not visible while in background).
* ["At home" I use these lines in a Karabiner-shortcut where AS need an osascript wrapping.]

Applescript to click on a specific icon in the Mac Menu Bar

Sometimes I use PdaNet to tether using my iPhone. The desktop client for OSX is not as rich as the one for windows. One of the chief differences is, that the OSX does not allow to automatically connect to iPhone as soon as the latter is plugged in.
Would you know of a way using Applescript to click on the PdaNet icon on the Menu Bar and then select and click the 'Connect' option on it ?
Here is what the 'PdaNetMac' application's menu bar icon looks like:
I have looked at the following questions but am an applescript newbie and am not sure how to search for PdaNet's icon on the menu bar:
Click menu item on Mac OSX Lion using AppleScript
Applescript: on clicking Menu Bar item via gui script
Accessing dock icon right-click menu items with AppleScript
I have confirmed that 'Enable Access for assistive devices' is enabled.
Based on the second question above, Here is my current attempt at doing this:
ignoring application responses
tell application "System Events" to tell process "PdaNet"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNet"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Interestingly, the above script works for me fine when I change PdaNet to Flux.
Thanks!!
You were very close !!
I just downloaded the PdaNet application to test this, and the only edit I had to make to your script was change PdaNet to 'PdaNetMac` ( I was thinking that this is the Process Name and so used the process name displayed in Activity Monitor).
So this works for me:
ignoring application responses
tell application "System Events" to tell process "PdaNetMac"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "PdaNetMac"
tell menu bar item 1 of menu bar 2
click menu item "Connect" of menu 1
end tell
end tell
Hope this works for you too !!
(Very useful script, btw. Cheers !)

Toggle messages preview in notification center with applescript/quicksilver

Haven't ever scripted with apple script before was wondering if it was possible to create an applescript that could toggle the settings for messages in the notification that displays the message preview. I was then gonna use quicksilver to call that script, is this possible with applescript and would quicksilver be able to make the appropriate calls to initiate the script? I'm running mavericks OS.
This toggles the "Show message preview" checkbox:
tell application "System Preferences"
reveal pane id "com.apple.preference.notifications"
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
repeat with r in rows of table 1 of scroll area 1
if name of UI element 1 of r is "Messages" then
set selected of r to true
exit repeat
end if
end repeat
click checkbox "Show message preview" of group 1
end tell

How to automate clicking a specific button repeatedly with AppleScript?

I'm trying to write a script that will:
Open a particular program
Click on a button at a particular spot within the program window
Repeat the click a certain number of times with a specified delay between clicks
I'm planning to use iCal to schedule running the script, but what should the actual script look like? Could it be run on the background without making any windows visible?
Most native applications support UI scripting like this:
reopen application "Finder" -- open a default window if there are no open windows
tell application "System Events" to tell process "Finder"
repeat 3 times
click button 2 of window 1
delay 1
end repeat
end tell
If you get an error like "Access for assistive devices is disabled", enable access for assistive devices from the accessibility preference pane.
If you have Xcode, you can use Accessibility Inspector to inspect UI elements. Or use something like this:
reopen application "Finder"
tell application "System Events" to tell process "Finder" to tell window 1
{class, value} of UI elements of UI elements
description of UI elements
properties of some UI element
attributes of some UI element
value of attribute "AXFocused" of some UI element
actions of button 2
end tell
If click doesn't work, try perform action "AXPress", set selected to true, or set focused to true.

Applescript: hiding manipulation of gui when running a gui applescript

I'm working on an applescript that clicks menu items in an application menu bar. I'd like to run this script without it displaying the whole process of clicking the target menu item. For example, here's some code below to click the disable for an hour menu item in the menu bar for the F.lux application
ignoring application responses
tell application "System Events" to tell process "Flux"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Flux"
tell menu bar item 1 of menu bar 2
click menu item "Disable for an hour" of menu 1
end tell
end tell
How do I go about preventing the display of the gui manipulation when running the this script?
You cannot do this. For AppleScript GUI scripting to work the UI elements must be front and key as they would be for the human.

Resources