I want to block a person in the Messages application and then delete the message. As Messages has no applescript command to block a person, I wrote a GUI script to identify the menu Block Person. Although this script prompts the dialog to block a person, I am not sure how to click the appropriate “Block” button in that dialog, to complete the task.
tell application "Messages"
activate
end tell
tell application "System Events"
tell process "Messages"
set targetMenu to first menu of menu bar 1 whose name contains "Conversation"
tell targetMenu
click (first UI element whose name contains "Block Person…")
end tell
end tell
end tell
If you know of a non GUI AppleScript method that can block a person, I would learning about it to circumvent any graphic use interface, altogether.
In Messages, regarding the menu "Block Person, clicking a first UI element did not work, a year ago.
In another attempt to block a Person in Messages, I tried the System Event command perform action “AXPick” or “AXPress”. This attempt also failed.
tell application "System Events"
tell application process "Messages"
set BlockPerson to menu item "Block Person…" of menu 1 of menu bar item "Conversation" of menu bar 1
tell BlockPerson to perform action "AXPick"
tell BlockPerson to perform action "AXPress"
click BlockPerson
end tell
end tell
My goal is to block a person in the Messages application. if anyone is aware of a method to accomplish this goal, via AppleScript or otherwise, I would appreciate the help.
tell application "Messages" to activate
tell application "System Events"
tell application process "Messages"
click menu item 4 of menu 1 of menu bar item 6 of menu bar 1
delay 0.4
click button 1 of sheet 1 of window 1
end tell
end tell
Ionah,
Thanks for your suggestion. Although Sheet 1 displays, when I manually click the Message menu item, it does not display with your AppleScript.
For reasons that I do not understand, my AppleScript fails to click any Message’s menu items.
I have tried the following AppleScript to target both menu items Delete and Menu.
tell application "System Events"
tell application process "Messages"
set MenuItemList to {"Delete", "Block"}
tell menu 1 of menu bar item "Conversation" of menu bar 1
repeat with aMenu in MenuItemList
set TargetButton to (first menu item whose title contains aMenu)
tell TargetButton
perform action "AXPick"
perform action "AXPress"
end tell
click TargetButton
end repeat
end tell
end tell
end tell
Although the script throws no error, the intended effect of clicking those targeted menu items fails.
Perplexing!
Indeed.
Are you able to click a menu in any other app (using SE, obviously)?
Try to delete and add again SE in Settings›Privacy & Security›Accessibility.
You need to make sure that Messages is the frontmost process when the clicks are applied, either by activating the application as in ionah’s script or by setting the process’s frontmost to true. Doing both doesn’t hurt.
tell application "Messages" to activate
tell application "System Events"
tell application process "Messages"
set frontmost to true
set BlockPerson to menu item "Block Person…" of menu 1 of menu bar item "Conversation" of menu bar 1
click BlockPerson
tell sheet 1 of window 1
repeat until (it exists)
delay 0.2
end repeat
click button "Cancel" -- for testing
--click button "Block"
end tell
end tell
end tell
On my Sequoia and Ventura systems, “Block Person…” is menu item 7 of that menu, which may be why ionah’s script didn’t work for you.
Curiously, when I looked into this, the Messages app on both computers, which I’ve never used, contained a text that someone sent me two months ago! My phone hadn’t received it, but the computers had!
Nigel and Ionah, thank you for your help, in resolving this Messages scripting problem. I have assembled the completed script below as a reasonably working script. If I erred, please correct my script.
# This script, after a conversation is selected,
# clicks the Block and Delete menu items
# in Message application's Conversation menu bar.
tell application "System Events"
tell application process "Messages"
set frontmost to true -- contributed by Nigel
set MenuItemList to {"Block", "Delete"}
tell menu 1 of menu bar item "Conversation" of menu bar 1
repeat with aMenu in MenuItemList
set TargetMenu to (first menu item whose title contains aMenu)
tell TargetMenu
# AXPick sets a check mark as Block Menu Item's value
# and activates its function.
perform action "AXPick"
# AXPress activates the Delete Menu Item's function.
perform action "AXPress"
(my clickButton:aMenu)
end tell
end repeat
end tell
end tell
end tell
on clickButton:ButtonName
# Activating the menu button displays a small dialog sheet.
tell application "System Events"
tell application process "Messages"
set frontmost to true -- contributed by Nigel
tell sheet 1 of window 1 -- contributed by Iona
repeat until (it exists) -- contributed by Nigel
delay 0.2
end repeat
click button ButtonName
end tell
end tell
end tell
end clickButton: