How to automate clicking a specific button repeatedly with AppleScript? - 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.

Related

AppleScript: Toggle Between Safari Windows?

If I have two safari windows open, just one tab in each, how do I get applescript to switch to either of the windows at will? Or in other words, toggle between them.
I tried, activate window 1 (or, the first window), and activate window 2 (or the second window), only ever activates the first window.
I tried open window 1 etc, open doesn't exist.
I tried using the system events, click menu bar 1 option, thinking maybe menu bar 2 was for window 2, didn't work.
I tried making do javascript on a specific tab show that page, couldn't get that to work.
Ultimately I did figure out I could use a keyboard shortcut, but I wanted to see if there was a more 'vanilla' applescript way.
If the windows are not full screen (in different spaces) just change the index of the window
tell application "Safari"
set index of window 2 to 1
end tell
If the windows are in different spaces you have to switch the spaces by executing the keystrokes with System Events. The default values on the US keyboard are ⌃←
tell application "System Events"
key code 123 using (control down)
end tell
and ⌃→
tell application "System Events"
key code 124 using (control down)
end tell
When I do just this
tell application "Safari"
set index of window 2 to 1
end tell
The new window that shows up is frozen. I fixed this by doing this
tell application "Safari"
set theWindows to windows
set win2 to item 2 of theWindows
tell win2
set visible to false
set visible to true
set index to 1
end tell
activate
end tell

How to click on a button in an app from the status bar with Apple Script

I have see how to click on menu items but this application does not have those. I was wondering how to click on the button Connect
I tried click at {818, 320} but it did not work
Here is the scrip, i was able to open the app with this
tell application "System Events"
tell UI element "Hotspot Shield"
tell menu bar item 1 of menu bar 2
click
delay 2
get the actions of button "Connect"
end tell
end tell
end tell
FWIW One would not normally use tell UI element "Hotspot Shield" rather use tell application process "Hotspot Shield", however, looking at this through Accessibility Inspector, I do not see a way to click the "Connect" button. Even using a Watch Me Do action in Automator, while it will record the process, it won't play it back successfully.
That said, here is a workaround solution that works for me on macOS Mojave using AppleScript and the third-party command line utility Cliclick.
Looking at the cropped screen shot in your OP and using the approximate center of the Connect button to be at 320 for the y axis and using AppleScript to get the position of the menu bar item for use as the approximate x axis, the following example AppleScript code should work for you too.
tell application "System Events" to ¬
tell application process "Hotspot Shield" to ¬
tell menu bar item 1 of menu bar 2
set xPos to first item of (get its position)
click
end tell
delay 0.5
do shell script "/Applications/cliclick c:" & xPos & ",320"
Note that the value of the delay command may need to be adjusted to ensure the Connect button is visible before the click event takes place using cliclick.
Adjust the path to cliclick as needed. I placed it in /Applications just for testing purposes.
The usual caveats, regarding System Preferences > Security & Privacy > Privacy, apply.
I am not affiliated with the developer of Cliclick, just a satisfied user of the product.
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.

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.

Change active finder tab

Is it possible to change the active Finder tab over applescript? I would like to create scripts to change the tabs with cmd-[0-9].
For Safari there is the possibility to set current tab to tab2 but that seems to be not the case with Finder.
Yes.
Actions supported by each app is defined in applescript Dictionary. Just because something work in one built-in app doesn't mean it will carry over to another.
Solution 1 is to call System Event and invoke keystroke for Previous Tab or Next Tab.
Solution 2: since you want to refer to tabs by its postion, use UI scripting. This way the scripting can switch to any tab that fits a certain criteria.
tell application "System Events" to tell process "Finder" to tell window 1 to tell radio button 1 to click

How do I scroll to the top of a window using applescript?

I want applescript to scroll a window all the way up.
I've tried the page up key, the home key, and I've tried looking for a way to scroll using the built in scrolling capabilities of the window, but I've so far been unable to even move the scrolled position at all.
Basically, use a tell app "System Events" statement to send keystrokes and key codes.
In theory, you could use the following:
keystroke page up key
keystroke page down key
keystroke home key
But for me this doesn´t work. The good news is that you can use the key codes instead. I suggest using the excellent free Full Key Codes application to read them, though it is a bit tricky to let it read two keys pressed simultaneously.
The key codes for the fn+ arrow keys-combos are as following:
Page up: fn+ up key: key code 116
Page down: fn+ down key: key code 121
Home: fn+ left key: key code 115
End: fn+ right key: key code 119
So for example if you had a long page open in Safari, and you want to scroll to its end, use
tell application "System Events"
tell application "Safari" to activate
— to see the animation, we wait a moment:
delay 0.5
key code 119
end tell
With browsers you could also use JavaScript:
tell application "Safari" to tell document 1
do JavaScript "window.scroll(0,0)"
end tell
tell application "Google Chrome" to tell active tab of window 1
execute javascript "window.scroll(0,0)"
end tell
The alternative to sending keystrokes is to use GUI scripting.
Caveat: While GUI scripting is more robust than sending keystrokes for a given version of an application, changes in the application's layout in future versions can break your code.
Also:
GUI scripting requires that access for assistive devices be enabled; enabling requires admin privileges:
up to 10.8, this could be done programmatically, system-wide by executing tell application "System Events" to set UI elements enabled to true (required admin privileges)
Sadly, on 10.9+, this no longer works, and apps must be authorized manually, individually - the system will prompt you on first run (requires admin privileges)
however, in both scenarios tell application "System Events" to get UI elements enabled will report whether access is enabled or not.
Determining the right UI element targets can be non-trivial and tedious; using the Accessibility Inspector utility that comes with Xcode helps. The class names reported by this utility correspond to the UI element classes contained in the System Events dictionary; e.g., AXSplitGroup corresponds to splitter group.
The following scrolls Safari 6.0.3's front window to the top (access for assistive devices must be enabled):
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of last group
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell
Update: As a reminder that this type of scripting works well for a given version of an application, the code had to be changed for Safari 8.0.4:
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of group 2
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell

Resources