I'm tinkering with an applescript that would clear notifications from Notification Center. At this point cannot figure out a way to click the clear button.
You see, the button only appears if cursor is hovering on the row of the app, or rows below that belongs to this app. I do:
tell application "System Events" to tell process "SystemUIServer" ¬
to click menu bar item "Notification Center" of menu bar 2
tell application "System Events" to tell process "Notification Center" ¬
to value of attribute "AXChildren" of UI element 1 of row 2 of table 1 of ¬
scroll area 1 of window "NotificationTableWindow"
Result:
{static text "iTunes" of UI element "iTunes" of row 2 of table 1 of ¬
scroll area 1 of window "NotificationTableWindow" of application process ¬
"NotificationCenter" of application "System Events"}
But if I strategically place the cursor beforehand, I get:
{static text "iTunes" of (ditto), button 1 of (ditto)}
button 1 is what I'm searching for. So far I tried three ways that didn't work out, listed from dumb to less dumb:
1) Keyboard Navigation
I tried to navigate down the list with key code 125. This doesn't make the button visible. While there, I tried a myriad of Delete combos. Nothing seems to delete notification entries.
2) click at (x,y) from System Events
On a 1280x800 screen, the button has an AXFrame of:
{x=1256.00 y=77.00 w=16.00 h=16.00}
which gives it a center of {1264, 85}, so:
tell application "System Events" to tell process "Notification Center"
click at {1264, 85}
value of attribute "AXChildren" of UI element 1 of row 2 of table 1 of ¬
scroll area 1 of window "NotificationTableWindow"
end tell
This, unsurprisingly, doesn't work.
3) select from System Events
tell application "System Events" to tell process "Notification Center" to ¬
tell window "NotificationTableWindow" to tell scroll area 1 to tell table 1 ¬
to tell row 2
select UI element 1
value of attribute "AXChildren" of UI element 1
end tell
Still, I only get static text 1 that is the app name. button 1 that is the clear button is nowhere to be found.
Does anyone know how to click this button - a button that only appears when hovered over?
I can't answer your question (the GUI Scripting, I'm not on Yosemite), but :
To clear all notifications (works on Maverick, I don't know on Yosemite), this script delete notifications from the database in "~/Library/Application Support/NotificationCenter" folder :
set notifCenterFolder to (path to application support from user domain as text) & "NotificationCenter:"
tell application "System Events" to set tDB to POSIX path of (first file of folder notifCenterFolder whose name extension is "db")
do shell script "/usr/bin/sqlite3 " & (quoted form of tDB) & " 'DELETE FROM notifications' && osascript -e 'quit application \"NotificationCenter\" ' && killall usernoted"
--
To clear notifications for a specific application (iTunes as example):
set iTunesPath to "/Applications/iTunes.app"
set notifCenterFolder to (path to application support from user domain as text) & "NotificationCenter:"
tell application "System Events" to set tDB to POSIX path of (first file of folder notifCenterFolder whose name extension is "db")
do shell script "/usr/bin/sqlite3 " & (quoted form of tDB) & " 'delete FROM notifications where app_id = (select app_id FROM app_source where last_known_path = \"" & iTunesPath & "\")' && osascript -e 'quit application \"NotificationCenter\" ' && killall usernoted"
--
On Yosemite, this file is in another folder --> /var/folder/... see this answer
Use this AppleScript on Yosemite :
do shell script "cd `getconf DARWIN_USER_DIR`com.apple.notificationcenter/db/ && /usr/bin/sqlite3 db 'DELETE FROM notifications' && osascript -e 'quit application \"NotificationCenter\" ' && killall usernoted"
Related
I've got a piece of Applescript that is currently functional and clicks a checkbox in System Prefs > Security & Privacy > Contacts. However, right now it only works because I am explicitly stating the row of the app that I'm targeting (in this case, row 2). This works fine for now, but if in the future I end up with a different app order in that pane, it will break. Is there a way to loop through all the items of a given window and say "if UI element is Alfred 4.app, then click the checkbox"? I'd like to harden the code so that it will work regardless of which order the apps are listed in this pane.
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.security.Privacy_Contacts"
delay 1
end tell
tell application "System Events"
click checkbox 1 of UI element appName of row 2 of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy" of application process "System Preferences"
end tell
Following script is tested on the Catalina:
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
reveal anchor "Privacy_Contacts" of pane id "com.apple.preference.security"
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
repeat with aRow in (rows of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy")
if name of UI element 1 of aRow is appName then
click checkbox 1 of UI element 1 of aRow
exit repeat
end if
end repeat
end tell
I am trying to write an applescript to change hot corners settings in System Preferences.
Here is what I have got until now.
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.desktopscreeneffect"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Hot Corners..."
end tell
end tell
But I get this error:error "System Events got an error: Can’t get button \"Hot Corners...\" of process \"System Preferences\"." number -1728 from button "Hot Corners..." of process "System Preferences". I would appreciate anyone explaining what is going wrong in here, plus is there any way to get properties(such as available buttons) of a pane?
Change:
click button "Hot Corners..."
To:
click button "Hot Corners…" of tab group 1 of window 1
Note the use of …, an ellipsis vs. ... three dots, as well as the missing UI Scripting hierarchal elements.
After the click line of code, and just to get the various UI elements you'll need to interact with, use the following example AppleScript code to get that info:
get UI elements of sheet 1 of window 1
get UI elements of group "Active Screen Corners" of sheet 1 of window 1
These lines of code do not remain in the finished script.
That all said, this was mainly to point out what was wrong with the code in your question, however the approach in the other answer is the way to go to get to the Hot Corners.
Update to address comment…
what does it mean 'UI Scripting hierarchical elements"
From the AppleScript dictionary for System Events:
UI element n : A piece of the user interface of a process
What this means is there is a hierarchy to the elements that comprise the User Interface, e.g., as shown in the line of code:
button "Hot Corners…" of tab group 1 of window 1
As illustrated in the top section of the following screen shot of Accessibility Inspector, a part of Xcode:
Here is one showing one of the hot corners I have set to show the Desktop:
If you mentioned in your question which drop-down menu in the hot corners that you want to select… I could have posted that additional code in my solution.
This AppleScript code works for me using the latest version of macOS Big Sur.
(* Quits "System Preferences" If Running *)
if application "System Preferences" is running then ¬
do shell script "killall 'System Preferences'"
(* Makes Sure "System Preferences" Is Not Running
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences"
(* Launches "System Preferences" Without Bringing It
To The Foreground, Going Directly To The Window Where You Can
`click button "Hot Corners..."` *)
reveal anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect"
(* Makes Sure anchor "ScreenSaverPref_HotCorners" exists
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat while not (exists of anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect")
delay 0.1
end repeat
(*.Makes application "System Preferences"
The Frontmost Visible App, Allowing You To Perform
Any Click Commands *)
activate
end tell
delay 0.1
tell application "System Events"
(* "Upper Left Dropdown Menu (pop up button 1) This Can Also Be Repeated
For `(pop up button 2) <-- Bottom Left, (pop up button 3) <-- Upper Right,
& (pop up button 4) <-- Bottom Right" *)
(*. By Now You Should Start Understanding The Purpose And Function
Of These Repeat Loops *)
repeat while not (exists of pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences")
delay 0.1
end repeat
click pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
delay 0.1
repeat while not (exists of menu item "Launchpad" of menu 1 of ¬
pop up button 1 of group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences")
delay 0.1
end repeat
-- Replace "Launchpad" Which Which Ever You Want
click menu item "Launchpad" of menu 1 of pop up button 1 of ¬
group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences"
delay 0.3
click UI element "OK" of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
end tell
delay 0.5
tell application "System Preferences" to quit
I am trying to interact with iTunes "Export Library.." dialog.
I tried "set choices to every menu item of menu 1 of pop up button 1 of group 1 of window winName" but it says "group 1" is an invalid index?
Here's the relevant code: (the call parameters are: "iMac-8GB", "iTunes", "iTunes", false"
on handleDir(dir, winName, appName, createIt)
local foundIt, ndx
set foundIt to false
if winName is not "" then
tell application "System Events" to tell process "iTunes"
set choices to every menu item of menu 1 of pop up button 1 of group 1 of window winName
You want to use the menu "Export Library…" from iTunes :
The first step is to select the relevant menu. In my iTunes version (12.5.5.5) the menu is in "File" menu, sub menu item "Library" and then sub Menu "Export Library…".
The second step is to fill the export file name and set the destination folder. The Mac "Save as…" window has many shortcuts, valid for all applications. Among others, command G allows to define the complete path to save the file. This path must be in Unix format (with "/" and not ":" for sub levels).
The bellow script does the complete Export Library function. The first 2 rows define the name of the file to save and the path where to save it. Adjust them to your needs.
set myTitle to "test" -- name of the exported file
set myPath to "/Users/myuser/Desktop/Test_folder" -- destination folder for export file
tell application "iTunes" to activate -- make iTunes front
tell application "System Events"
tell process "iTunes"
click menu 3 of menu bar 1 -- open the File menu
click menu item 12 of menu 3 of menu bar 1 -- select the Library menu item
delay 0.1
click menu item 5 of menu 1 of menu item 12 of menu 3 of menu bar 1 -- select the export library… item
delay 0.1
keystroke myTitle -- fill the export file name in the save as… dialog
keystroke "G" using command down -- shortcut to open Go-to folder window
keystroke myPath
keystroke return -- to close the go-to window
delay 0.1
keystroke return -- to close the export window
end tell -- process iTunes
end tell -- system Events
I added several delays to make sure your Mac has enough time to open or close windows.
Using this code, modified to specifically target iTunes':
tell application "System Events"
tell front window of (first application process whose frontmost is true)
set uiElems to entire contents
end tell
end tell
which came from an answer to Use AppleScript to list the names of all UI elements in a window (GUI scripting)
I discovered that a NSBox is referred to as an "outline" by Applescript.
Writing an AppleScript to open Image Capture and click the Import All button.
tell application "Image Capture"
activate
tell application "System Events"
tell process "Image Capture"
click button "Import All" of group 1 of splitter group 1 of window 1
end tell
end tell
end tell
Image Capture opens but the script throws an error message saying it couldn't find the button "Import All".
Have followed advice on other threads on how to check the location in Accessibility Inspector and how to translate that to AppleScript instructions.
What's missing?
To get the button and group numbers, you have 2 ways: use the utility aplication provided by Apple in the developper toolkit "Accessibility Inspector" or use small scripts to find the number yourselves.
I prefer using script method. it is a bit longer sometime, but it always works. Just write a script with instruction get UI elements. Here is a small example of such script:
-- return lis of UI elements of active application and paste result in Excel
property tab : ASCII character 9
global T
-- to find last active application
tell application "System Events"
set frontmostProcess to first process where it is frontmost
set visible of frontmostProcess to false
repeat while (frontmostProcess is frontmost)
delay 0.2
end repeat
set secondFrontmost to name of first process where it is frontmost
set frontmost of frontmostProcess to true
end tell
set ActifProcess to secondFrontmost as text
tell application ActifProcess to activate -- set active the last actived application
delay 1
-- recursive loop to list all elements of active window
tell application "System Events" to tell process ActifProcess to set myWindow to front window
set T to ""
getUI(myWindow, 1)
set the clipboard to T
display dialog "Result is in clipboard. paste in Excel or text document"
on getUI(UIObjet, myLevel) -- get recursively the list of UI elements
set AppleScript's text item delimiters to {"//"}
tell application "System Events" to set UIliste to UI elements of UIObjet
repeat with oneUI in UIliste
tell application "System Events"
set UItext to ("Level=" & myLevel & tab & "Name=" & (name of oneUI) & tab & (description of oneUI) & tab & "Class=" & (class of oneUI) as string) & tab & "Title=" & (title of oneUI) as string
set UItext to (UItext & tab & "Value=" & (value of oneUI) as string) & tab
try
set UItext to (UItext & "Position=" & (position of oneUI) as text)
end try
set UItext to UItext & return
try
set NbSub to count of UI elements of oneUI
on error
set NbSub to 0
end try
set T to T & return & UItext
end tell
if NbSub > 0 then
getUI(oneUI, myLevel + 1) -- there are other sub UI elements, get them
end if
end repeat
end getUI
Copy this script in Script Editor. Make active the window/application you want to get UI elements. Make this script active and run.
The result is sometime not easy to interpret because developper of the application/window you're looking for may not have use UI element clear names or titles which describe what they are. Then you will have to look their relative position in the window.
The "import all" button is "button 3 of group 2 of splitter group 1 of window 1" for image capture version 6.6. Also, I prefer to use button number, instead of button name to make sure the script works with any language.
tell application "Image Capture"
activate
tell application "System Events"
tell process "Image Capture"
click button 3 of group 2 of group 1 of splitter group 1 of window 1
end tell
end tell
end tell
Please note that any next changes done by Apple on Image Capture will impact your script.
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