I am trying to trap mouse click events in Safari with Applescript. My Applescript does what it needs to do, but I want to terminate if the user clicks on the document in Safari. I would like to set up an On_Click event in Applescript that would fire if the user clicks in the Safari document.
I do not know how to "trap" a mouse event with applescript. You'd have to use objective-c and the cocoa APIs for that.
However you might do it another way. Here's a quick example. Save this as a stay-open applescript application. When you run it the Safari window will scroll. To pause the scrolling just click the dock icon of the applescript. If you click the dock icon again then the scrolling will start again and vice versa. You'll notice that every time you click the dock icon the "on reopen" handler is executed which toggles the shouldScroll variable between true and false.
Anyway, I hope this gives you an idea for making your own script work. Remember you can quit the stay-open applescript by right-clicking on its dock icon and choosing quit. Good luck.
property shouldScroll : missing value
property theCounter : missing value
property counterMaxValue : missing value
on run
tell application "Safari" to activate
set shouldScroll to true
set theCounter to 0
set counterMaxValue to 2000
end run
on reopen
tell application "Safari" to activate
set shouldScroll to not shouldScroll
end reopen
on idle
if shouldScroll then
if theCounter is greater than counterMaxValue then set theCounter to 0
tell application "Safari" to tell document 1
do JavaScript "window.scroll (0," & (theCounter as text) & ")"
end tell
set theCounter to theCounter + 20
end if
return 0.1
end idle
You might exit the script when the focused window becomes a normal browser window, by for example testing if the frontmost window has a full screen button:
tell application "System Events" to tell process "Safari"
repeat until exists value of attribute "AXFullScreenButton" of window 1
--do something
delay 1
end repeat
end tell
Or if the script is supposed to stop when Safari becomes the frontmost application:
repeat until frontmost of application "Safari"
--do something
delay 1
end repeat
Related
I have this script here which is loading a profile on an unfocused safari tab and saving the text I want in a variable
tell application "Safari" to set the URL of tab 2 of window 1 to "https://URLproduct/" & serialNumber & "/definition"
delay 2.5
However some of the data is only available after a click on a button (this is a pop-up like overlay with a button, which make the another text not accessible until you press on "OK", also this button is not always here)
button HTML is :
<button class="btn btn-primary" autofocus="" ng-click="ok()" tabindex="0">OK</button>
which I press with this script :
do JavaScript "document.getElementsByClassName('btn btn-primary')[1].click();" in tab 2 of window 1
Weirdly sometime is working sometime not and I can't add too much delay
Can I add a script which would wait for this button ('btn btn-primary') been availible (fully loaded) and then try again to get the text by with my 1st script?
--
May be I can add something like
tell application "Safari"
tell document 1
repeat until ("btn btn-primary" is in its source)
delay 0.5
end repeat
end tell
end tell
then the click script?
yes, you can start executing your script after the page has loaded. This makes sure that the button is clickable (in case it appears at all).
Put this code in to your script:
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
end repeat
end tell
Happy scripting!
I'm looking for the way to access UI which applescript can not access. I know applescript can access UI element. But sometimes I meet the case I can't access UI element by applescript. Like below ...
tell application "System Preferences" to activate
delay 1
tell application "System Events"
tell process "System Preferences"
set frontmost to true
click button 22 of scroll area 1 of window 1
delay 1
UI elements of last group of list 1 of window 1
end tell
end tell
This just returns 2 UI elements image and static text.
{
image 1 of group 3 of list 1 of window "Users & Groups" of application process "System Preferences" of application "System Events",
static text "Login Options" of group 3 of list 1 of window "Users & Groups" of application process "System Preferences" of application "System Events"
}
Apparently there is a clickable UI element which above 2 elements belonging. Because I can click "Login Options".
Fortunately by sending key stroke and key code commands were received properly.
set DownArrow to 125
set UpArrow to 126
tell application "System Preferences" to activate
delay 1
tell application "System Events"
tell process "System Preferences"
set frontmost to true
click button 22 of scroll area 1 of window 1
delay 1
keystroke tab
repeat 3 times
delay 1
key code DownArrow
delay 1
key code UpArrow
end repeat
end tell
end tell
tell application "System Preferences" to quit
My question is ...
Why does applescript seem to ignore "Login Options" UI element?
Why can't I click "Login Options" UI element with a mouse or trackpad?
Why can't key stroke and key code command access the "Login Options" UI element?
Is there really no way to select the "Login Options" UI element without using key stroke and key code commands?
How to access applescript non-accessible UI element?
Any hint is appreciated. Any code(python, c, objective-c, etc) is more appreciated.:)
Thanks.
To solve many of these problems you do not need System Events. For example, System Preferences is script-able enough to at least do this, which solves a few of your problems:
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preferences.users"
reveal anchor "loginOptionsPref" of current pane
end tell
So,
AppleScript isn't "ignoring" the Login Options control;
You don't need to click, you can use "reveal" (do you mean to type "can't" instead of "can"? -- I've edited your post to format your questions more correctly);
I think this makes the 3rd question moot;
My example shows the way to do it w/o clicking
How to access "non-accessable" items -- much bigger question:
While it is true that System Preferences is script-able, there are times when you may need System Events. For example, I have a script for the Sound pane that uses the following handler (subroutine):
on doSysEvSelect(whatToSelect, whatWindow)
tell application "System Events"
tell process "System Preferences"
select (row 1 of table 1 of scroll area 1 of tab group 1 of window whatWindow whose value of text field 1 is whatToSelect)
end tell
end tell
end doSysEvSelect
, which I can use by doing, for example:
doSysEvSelect("Internal Microphone", "Sound")
Using System Events can be frustrating to figure out. But of course there are many examples around to find. And if you're really in a bind, you can use Sikuli (http://sikuli.org/) to automate just about anything, but which relies totally on the UI and clicking.
[edit]
And here's an example of what to do with an item in Login Options once you're there (this is where you need to use System Events):
tell application "System Events"
tell process "System Preferences"
if value of checkbox "Show password hints" of group 1 of window "Users & Groups" is 0 then
click checkbox "Show password hints" of group 1 of window "Users & Groups"
end if
end tell
end tell
You can simulate clics like you do directly when using system preferences, but you need to know which object to click.
Fyi, here is an example of system preferences click and select for Bluetooth. it gives an idea of the way to do it :
tell application "System Preferences"
activate
set current pane to pane "com.apple.preferences.Bluetooth"
tell application "System Events"
tell process "System Preferences"
-- select row containing the word "mouse"
repeat with I from 1 to count of row of table 1 of scroll area 2 of group 1 of front window
set Nom_Perif to (description of static text 1 of row I of table 1 of scroll area 2 of group 1 of front window) as string
if Nom_Perif contains "Mouse" then
set selected of row I of table 1 of scroll area 2 of group 1 of front window to true
if Nom_Perif contains "not connected" then
-- sert connection via item 1 of menu
click menu button of group 1 of front window
delay 1
click menu item "Connect" of menu of menu button of group 1 of front window
delay 3
end if
end if
end repeat
end tell
end tell
end tell
quit application "System Preferences"
I need to perform automatic installation extension in Safari.
Now I have this part of code:
property extension_list : {"safariextz"}
on adding folder items to this_folder after receiving these_items
try
tell application "Finder"
repeat with i from 1 to (number of items in these_items)
set this_item to item i of these_items
set item_extension to name extension of this_item
if item_extension = "safariextz" then
tell application "Safari" to open this_item
delete this_item
end if
end repeat
end tell
on error errmsg
display dialog errmsg buttons {"OK"} default button 1
end try
end adding folder items to
This works, file is running after downloading.
But I cannot press button Install for starting installation extension.
I tried something like that
tell application "System Events"
tell process "Safari"
click the button "Install"
end tell
end tell
But this doesn't help.
Cloud you please assist to me with completing script for extension installation?
You must specify a window like this : click button 1 of window 1
open an "safariextz" file in Safari blocks the script, you must use ignoring application responses
The script must check that the dialog is displayed
on adding folder items to this_folder after receiving these_items
repeat with this_item in these_items
if (this_item as string) ends with ".safariextz" then
ignoring application responses
tell application "Safari" to open this_item
end ignoring
tell application "System Events"
tell process "Safari"
set frontmost to true
repeat until (exists window 1) and subrole of window 1 is "AXDialog" -- wait until the dialog is displayed.
delay 1
end repeat
click button 1 of front window -- install
end tell
end tell
end if
end repeat
end adding folder items to
Updated : as ignoring application responses doesn't work for you, try this
on adding folder items to this_folder after receiving these_items
repeat with this_item in these_items
if (this_item as string) ends with ".safariextz" then
tell application "Finder" to open this_item
For Safari 9, the button's context changed. Here's the script I figured out to get it back working. (This is only to show the order of commands and how to click the button.)
tell application "Safari" to activate
delay 2
tell application "System Events"
tell application process "Safari"
set frontmost to true
tell application "Safari" to open location "/path/to/SafariDriver.safariextz"
delay 2
click button 1 of sheet 1 of window 1
end tell
end tell
tell application "Safari" to quit
with
tell application "Safari"
set winlist to every window
repeat with win in winlist
set tablist to every tab in win
repeat with t in tablist
name of t as string
end repeat
end repeat
end tell
I can get the name of safari tab
how I can close a tab with a specific name ?
Here is another approach:
tell application "Safari"
set winlist to count of every window
repeat with i from 1 to winlist
close (every tab of window i whose name = "Google")
end repeat
end tell
tell application "Safari"
repeat with t in tabs of windows
tell t
if name starts with "autom" then close
end tell
end repeat
end tell
tell application "Safari" to close documents where name starts with "autom" would close all windows that contain tabs that start with autom.
It doesn't work for me 2
Neither of them work
The strange thing is, it does work when the safari is not in fullscreen
I guess it is a bug in safari
I'm trying to automatically select a menu item in iTunes using applescript. However, every time that I run this script I get an error message that says "expected end of line but found number, and points me to "scrollarea 1". I'm using Apple's UIElement inspector to get the names of the GUI elements in iTunes. Any help would be greatly appreciated.
Here's what I have so far:
tell application "iTunes"
activate
delay 10
tell application "System Events"
key code 53
tell outline 1 of scrollarea 1 of window 1
click menu 1
end tell
end tell
end tell
Well, I have it clicking what I need it to be clicking. The only problem now is that it's not actually clicking it, it's just returning the AXValue. I need it to actually click the item in iTunes itself.
activate application "iTunes"
tell application "System Events"
tell process "iTunes"
select row 15 of outline 1 of scroll area 2 of window "iTunes"
end tell
end tell