AppleScript keystroke - applescript

I use an app, and I want to get a data from this app,
I found a button on the menu bar > Edit > Optionwanted
I also found that the option have a shortcut (C + COMD + CTRL + SHIFT)
I'm not sure this is the best approach but I tried :
activate application "App"
delay 1
tell application "System Events" to keystroke "C" using {control down, command down, shift}
but then the result is
System Events got an error: Can’t make {control down, command down,
shift} into type constant or list of constant.
is that the best approach to get text from an App ?
I can I fix my script.
PS : when I run
tell application "System Events" to tell application process "App"
set stuff to entire contents of front window
end tell
return stuff
I can see what I want there :
application process "App", static text "445511" of group 3 of
group 1 of group 2 of group 5 of UI element 1 of scroll area 1 of
group 2 of group 1 of group 2 of UI element 1 of scroll area 1 of
splitter group 1 of window "The Title is actually not static"
That can be a another approach but the title and Text is dynamic and change all the time.

You are close.
This is what I got:
activate application "App"
delay 1
tell application "System Events"
keystroke "C" using {control down, command down, shift down}
end tell

Related

Check whether scroll is finished using AppleScript

I have an AppleScript script to open the Mail app, open a mail, scroll down and take screenshot of the whole mail body. I am taking the screenshot by scrolling the page using keystroke 121 (page down). I don't know how much time I have to repeat the loop to reach the page end. Is there anyway we can identify if the scroll has reached the page end using AppleScript?
This is my code snippet for page down and taking screenshot:
open each_message
tell application "System Events"
delay 5
keystroke "f" using {command down, control down}
delay 3
key code 20 using {command down, shift down}
delay 5
key code 121
delay 5
key code 20 using {command down, shift down}
delay 5
keystroke "f" using {command down, control down}
end tell
After reading your question and ensuing comments...
Is there anyway we can identify if the scroll has reached the page end using AppleScript?
Yes
For some emails, it's fine. For some, it is duplicating the last page, for some, the screenshots are incomplete.   (From comments under the question.)
That is the nature of different length documents and pressing the Page Down key, and why maybe using a third-party application like Snagit is a better way to go.
That all said...
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
As you did not include the complete code of your script, the following example AppleScript code is presented more as a proof of concept. It too may/will have extra screenshots, however, it ensures the page it all the way to the top before it starts taking the screenshots and ensures a screenshot of the whole last page is taken too, based on it stopping appropriately.
For testing purposes I started with an individual email opened in its own window, assuming that is what the open each_message at the top of the code you included does or represents.
Example AppleScript code:
tell application "Mail" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
delay 1
repeat while my getEmailFullScreenWindowScrollPosition() > 0.0
key code 116
delay 0.1
end repeat
delay 1
repeat while my getEmailFullScreenWindowScrollPosition() < 1.0
key code 20 using {command down, shift down}
delay 1
key code 121
delay 1
end repeat
key code 121
delay 1
key code 20 using {command down, shift down}
delay 1
keystroke "f" using {command down, control down}
end tell
to getEmailFullScreenWindowScrollPosition()
tell application "System Events" to ¬
return value of ¬
scroll bar 1 of ¬
scroll area 1 of ¬
window 1 of ¬
process "Mail"
end getEmailFullScreenWindowScrollPosition
Notes:
In Mail in macOS Catalina on my system there was no horizontal scroll bar when an opened email in its own window is in Full Screen view, and why the handler is directed to scroll bar 1. However, if you do need to query for its orientation, then for the handler use:
to getEmailFullScreenWindowScrollPosition()
tell application "System Events" to ¬
return value of ¬
first scroll bar of ¬
scroll area 1 of ¬
window 1 of ¬
process "Mail" whose ¬
orientation is ¬
"AXVerticalOrientation"
end getEmailFullScreenWindowScrollPosition
In testing the getEmailFullScreenWindowScrollPosition() handler, it would report 1.0 when there was still some page left but not showing on the screen, so I added the additional lines of code after the second repeat loop to ensure it was all captured.
The test email had content to the very end of the page and maybe isn't really representative of most emails. If you find that there is ample white space at the end of your emails then you may want to remove the following lines of code after the second repeat loop:
key code 121
delay 1
key code 20 using {command down, shift down}
Also note that I ended up using delay 1 for all but one of the delay commands, and it worked fine for me in testing, however, adjust as necessary for your system.
As this is just being presented as a proof of concept, I'll leave it to you to tweak the code to fit your needs.
Note that I am not affiliated with the developer of Snagit and have mentioned it as a possible option to capturing the entire scroll area of the screen in a single screenshot.
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.
Here's a handler that returns the current vertical scroll bar position for a message viewed in the frontmost message viewer. It makes a bunch of assumptions about which panels you have open and what orientation the viewer has, but it should give you something to work with:
set sp to my findMailScrollPosition()
on findMailScrollPosition()
tell application "System Events"
tell process "Mail"
tell window 1
tell splitter group 1
tell splitter group 1
tell scroll area 2
tell (first scroll bar whose orientation is "AXVerticalOrientation")
-- this will return a value between 0 and 1 (top to bottom)
return value
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end findMailScrollPosition

How do I double click on an image/or link to initiate installation? APPLESCRIPT [Please view image]

I am trying to automate the installation of slack. I run the .dmg then must double click the "Slack" icon to start the install. using the '''click''' function on images is not really doable im guessing.. I see that the URL this image calls on is file:///Volumes/Slack.app/Slack.app/ Do I need to call on this link perhaps ? How can I get the same results of double clicking on the image.
Accessibility Inspector Screenshot
tell application "Finder"
set myFolder to container of (path to me) as alias
end tell
tell application "Finder"
open document file "Slack-4.3.3-macOS.dmg" of myFolder
end tell
tell application "System Events"
tell process "Finder"
click image "Slack" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack"
end tell
end tell
Accessibility Inspector Screenshot
If you are still interested in doing this job by clicking the image in AppleScript, you can try code below:
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
ignoring application responses
perform action "AXShowMenu"
end ignoring
end tell
end tell
end tell
delay 0.3
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
--key code 125 --also can try use arrow key to select item in context menu
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell
I test this code on my MacOS 10.12.6, it works OK. Even though it's easy to use terminal command to do this job, this method involves some very useful AS knowledge point.
1st : perform action "AXOpen" and "AXShowMenu"
2nd : show context menu and select menu item
3rd : deal with system delay
If it doesn't have delay issue, you can try the code below, which is more simpler,(also works on my machine):
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
perform action "AXShowMenu"
delay 0.3
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell

AppleScript error 1719

I've a problem with this applescript. Error 1719.
Instruments app
I need to set the new slider value to 2.
This is my code:
tell application "Instruments"
activate
end tell
activate application "Instruments"
tell application "System Events"
tell process "Instruments"
keystroke "," using command down --Instruments menubar -> Preferences
tell window 1
click button "CPUs" of toolbar 1
set value of slider 1 of group 1 to 2
end tell
end tell
end tell
Image
Thanks for help
It's a reference error.
The slider is in group 1 of group 1. When using GUI scripting it's also recommended to wait for particular UI elements in case there are time-critical changes.
activate application "Instruments"
tell application "System Events"
tell process "Instruments"
keystroke "," using command down --Instruments menubar -> Preferences
tell window 1
click button "CPUs" of toolbar 1
tell group 1 of group 1
repeat until exists slider 1
delay 0.2
end repeat
set value of slider 1 to 2
end tell
end tell
end tell
end tell

How to download a new voice for say's function with AppleScript?

I'm searching a method to install different voices (2 to be exact) on the system preferences.
The voices are "Alex" for an english voice and "Thomas" for a french voice.
I've tried directly by console but didn't succeed, that's why I've turned to the AppleScript language, but I never used this language.
The code I've for the moment is
set osver to system version of (system info)
if osver is equal to "10.6.8" then
display dialog ("Downloading voices is only available in OS X Lion and higher")
else
tell application "System Preferences"
activate
reveal (pane id "com.apple.preference.speech")
end tell
try
tell application "System Events"
click radio button 2 of tab group 1 of window 1 of process "System Preferences"
repeat until (exists pop up button of tab group 1 of window 1 of process "System Preferences")
delay 2
end repeat
delay 2
click pop up button 1 of tab group 1 of window 1 of process "System Preferences"
delay 2
click menu item -1 of menu 1 of pop up button of tab group 1 of window 1 of process "System Preferences"
delay 2
end tell
on error
display dialog ("An error happend")
end try
end if
This program is opening the voice window but the display dialog appears every time whatever the index I put.
If you have another idea to download the voices, or if you can help me to understand what is not working, I will be grateful.
This worked for me in 10.9:
tell application "System Preferences"
reveal anchor "TTS" of pane id "com.apple.preference.speech"
activate
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
tell pop up button 1 of tab group 1
click
click menu item "Customize..." of menu 1
end tell
delay 1
repeat with r in UI element 1 of rows of table 1 of scroll area 1 of sheet 1
if exists static text 1 of r then
if {"Alex", "Thomas"} contains value of static text 1 of r then
if value of checkbox 1 of r is 0 then click checkbox 1 of r
end if
end if
end repeat
click button "OK" of sheet 1
end tell
It took multiple seconds to run the script though.

Applescript delay so long when run click button command

i was write a simple code to click share button on Notes applicaion
set upArrow to ASCII character 30
activate application "Notes"
tell application "System Events"
tell process "Notes"
set frontmost to true
click button 2 of group 2 of splitter group of window 1
keystroke upArrow
keystroke return
end tell
end tell
the problem is
click button 2 of group 2 of splitter group 1 of window 1 of application process "Notes" --> missing value
it need 6s to complete run this and return error missing value
but if i dont add keystroke or any command follow that, this code work perfectly
I also get that delay and it returns missing value. But could you click a menu bar item instead?
activate application "Notes"
tell application "System Events" to tell process "Notes"
click menu item "Email" of menu 1 of menu item "Share" of menu "File" of menu bar 1
end tell
set focused of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
or
set value of attribute "AXFocused" of button 2 of group 2 of splitter group 1 of window 1 to true
keystroke space
Source:
https://lists.apple.com/archives/Accessibility-dev/2006/Oct/msg00013.html
There is a thread that seems to describe the same bug that appears to be limited to specific software. It is on MacScripter.net: Script delays on clicking button (controlling Sys Prefs pane)
Note the most recent post that describes a delay in FileMaker. It seems like the only work-around when software exhibits this bug is to do something else to open the new window. If the only way to run the desired function is via a button, one (terrible) option is clicking at coordinates relative to the corner of the front window. Obviously this will easily fail if the content can move relative to the corner or if a new version of the software moves the button desired. Any other ideas?
UPDATE, 2023-02-13:
In case anybody is still dealing with this bug, here's some useful AppleScript to get the coordinates of the object you'd like to click. You can then feed those into some coordinate-clicking utility, like cliclick (binary), Python code, Keyboard Maestro, and so on.
tell application "System Events"
set someObject to <REFERENCE TO YOUR OBJECT>
-- e.g. op's: button 2 of group 2 of splitter group of window 1 of application process "Notes"
set {xCoord, yCoord} to position of someObject
set {xSize, ySize} to size of someObject
end tell
set objOffset to round (minNum({xSize, ySize}) / 2) rounding down
set xClick to xCoord + objOffset
set yClick to yCoord + objOffset

Resources