How to interact with "Messages" in applescript - applescript

I'm trying to make an application automating the Messages app on Mac. One of the automations requires interactions with the conversation info button. I have tried to write a script to start playing around with it:
tell application "System Events"
tell process "Messages"
set infoButton to "Conversation Details"
click toolbar item infoButton of toolbar 1
end tell
end tell
However I receive the error: error System Events got an error: Can’t get item \"Conversation Details\" of process \"Messages\"." number -1728 from item "Conversation Details" of process "Messages Below I pasted a screenshot of using the Accessibility Inspector, which has the information surrounding the ui element I am trying to manipulate. Any help would be greatly appreciated.

If you are trying to click the Conversation Details button on the Toolbar of Messages in macOS Big Sur with AppleScript, then the following example AppleScript code will do it:
tell application "System Events" to ¬
if exists (buttons of ¬
toolbars of ¬
front window of ¬
process "Messages" whose ¬
description is "Conversation Details") ¬
then click (buttons of ¬
toolbars of ¬
front window of ¬
process "Messages" whose ¬
description is "Conversation Details")
In short, it's:
button 2 of toolbar 1 of window 1 of application process "Messages"
However, I prefer to write it in an error handling method as in the full tell statement shown above this.

Related

How to click on add extension button of chrome using apple script

I am trying to automate my chrome extension addition to chrome.
I am able to click on add extension from chrome web store using apple script but I am able to click on the Add extension button.
My current code is
tell application "System Events"
tell application process "Google Chrome"
set frontmost to true
--click button 1 of group 1 of sheet1 of window 2
keystroke "´"
tell window 1
tell (group whose title is "Add "Symantec Extension"?")
get properties
tell button 1
perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
I am trying to click on this button
Testing under macOS Catalina with Google Chrome 89.0.4389.90, the following example AppleScript code worked for me, from Script Editor:
tell application "System Events" to ¬
click button "Add extension" of ¬
group 1 of ¬
sheet 1 of ¬
window 1 of ¬
process "Google Chrome"
Notes:
I only had one window with two tabs open in Google Chrome with the sheet showing when I ran that code.

How to to select specific checkboxes in System Preferences?

I am trying to select the third checkbox of the Keyboard menu item and tab to change the basic fn key function with one run of the script. The rest of the code appears to work fine, but I just recently started trying to code at all so I have no idea.
Here is my current code:
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.keyboard"
delay 1
tell application "System Events"
tell (click checkbox 3 of tab group 1)
delay 2
end tell
end tell
end tell
tell application "System Preferences" to quit
And here is the error message:
error "System Events got an error: Can’t get tab group 1." number -1728 from tab group 1
It looks like I'm just not defining it correctly, but I can't find out how to. Any help is appreciated!
Also, this is not needed but would it be possible to run the script without visibly opening the System Preferences application?
The following example AppleScript code was tested under macOS Catalina and clicks the Turn keyboard backlight off after checkbox at: System Preferences > Keyboard > Keyboard
As coded, it does the following:
Checks to see if System Preferences is running and if it is, it closes it so as to not have to see the UI flashing thru the different panes.
If System Preferences is not running it opens to the target anchor/pane without showing the UI.
Clicks the target checkbox.
Closes System Preferences
Example AppleScript code:
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" to ¬
reveal anchor "keyboardTab" of pane id ¬
"com.apple.preference.keyboard"
tell application "System Events"
tell front window of application process "System Preferences"
repeat until (exists checkbox 3 of tab group 1)
delay 0.01
end repeat
click checkbox 3 of tab group 1
delay 0.1
end tell
end tell
tell application "System Preferences" to quit
Note: The example AppleScript code is just that and sans any included error handling, does not contain any additional 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. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Applescript to open youtube in safari private mode

How can the youtube be opened in safari private mode?
I have tried this, but it is not working:
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Private Window" of menu "File" of menu bar 1
open location "https://www.youtube.com" -- this will open default browser
end tell
end tell
My default browser is Chrome, and it opens youtube in chrome not in safari private mode.
How to fix this?
The following example AppleScript code works for me:
For Safari use:
activate application "Safari"
tell application "System Events" to ¬
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
tell application "Safari" to ¬
set URL of current tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
For Google Chrome use:
activate application "Google Chrome"
tell application "System Events" to ¬
click menu item "New Incognito Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Google Chrome"
tell application "Google Chrome" to ¬
set URL of active tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
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.
I don't really have a better solution for Safari than the one offered by #user3439894. The only thing I would have done differently would be to make the new private window using this code (which is probably "6 of 1 or 1/2 a dozen of the other")
tell application "Safari" to activate
tell application "System Events" to tell its application process "Safari"
set frontmost to true
keystroke "n" using {shift down, command down}
end tell
However, you may prefer this solution for Google Chrome because it does not require the use of System Events and it does not require Google Chrome to be active or frontmost.
tell application "Google Chrome"
set incognitoWindow to (make new window with properties {mode:"incognito"})
repeat while loading of active tab of incognitoWindow
delay 0.1
end repeat
set URL of active tab of incognitoWindow to "http://youtube.com"
end tell
tell application "Opera"
try
make new window with properties {mode:"incognito"}
end try
set URL of active tab of front window to "https://yoururl.com"
end tell
I wrapped make new window with properties {mode:"incognito"} in try because I get an error "Opera got an error: AppleEvent handler failed." number -10000 because i use Opera browser.
It is not necessary when using Google Chrome.

Apple Script Error: Can't continue click

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.

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

Resources