I am trying to automate my chrome extension addition to chrome.
I am able to click on add extension from chrome web store using apple script but I am able to click on the Add extension button.
My current code is
tell application "System Events"
tell application process "Google Chrome"
set frontmost to true
--click button 1 of group 1 of sheet1 of window 2
keystroke "´"
tell window 1
tell (group whose title is "Add "Symantec Extension"?")
get properties
tell button 1
perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
I am trying to click on this button
Testing under macOS Catalina with Google Chrome 89.0.4389.90, the following example AppleScript code worked for me, from Script Editor:
tell application "System Events" to ¬
click button "Add extension" of ¬
group 1 of ¬
sheet 1 of ¬
window 1 of ¬
process "Google Chrome"
Notes:
I only had one window with two tabs open in Google Chrome with the sheet showing when I ran that code.
Related
I'm trying to make an application automating the Messages app on Mac. One of the automations requires interactions with the conversation info button. I have tried to write a script to start playing around with it:
tell application "System Events"
tell process "Messages"
set infoButton to "Conversation Details"
click toolbar item infoButton of toolbar 1
end tell
end tell
However I receive the error: error System Events got an error: Can’t get item \"Conversation Details\" of process \"Messages\"." number -1728 from item "Conversation Details" of process "Messages Below I pasted a screenshot of using the Accessibility Inspector, which has the information surrounding the ui element I am trying to manipulate. Any help would be greatly appreciated.
If you are trying to click the Conversation Details button on the Toolbar of Messages in macOS Big Sur with AppleScript, then the following example AppleScript code will do it:
tell application "System Events" to ¬
if exists (buttons of ¬
toolbars of ¬
front window of ¬
process "Messages" whose ¬
description is "Conversation Details") ¬
then click (buttons of ¬
toolbars of ¬
front window of ¬
process "Messages" whose ¬
description is "Conversation Details")
In short, it's:
button 2 of toolbar 1 of window 1 of application process "Messages"
However, I prefer to write it in an error handling method as in the full tell statement shown above this.
How can the youtube be opened in safari private mode?
I have tried this, but it is not working:
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Private Window" of menu "File" of menu bar 1
open location "https://www.youtube.com" -- this will open default browser
end tell
end tell
My default browser is Chrome, and it opens youtube in chrome not in safari private mode.
How to fix this?
The following example AppleScript code works for me:
For Safari use:
activate application "Safari"
tell application "System Events" to ¬
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
tell application "Safari" to ¬
set URL of current tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
For Google Chrome use:
activate application "Google Chrome"
tell application "System Events" to ¬
click menu item "New Incognito Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Google Chrome"
tell application "Google Chrome" to ¬
set URL of active tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
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.
I don't really have a better solution for Safari than the one offered by #user3439894. The only thing I would have done differently would be to make the new private window using this code (which is probably "6 of 1 or 1/2 a dozen of the other")
tell application "Safari" to activate
tell application "System Events" to tell its application process "Safari"
set frontmost to true
keystroke "n" using {shift down, command down}
end tell
However, you may prefer this solution for Google Chrome because it does not require the use of System Events and it does not require Google Chrome to be active or frontmost.
tell application "Google Chrome"
set incognitoWindow to (make new window with properties {mode:"incognito"})
repeat while loading of active tab of incognitoWindow
delay 0.1
end repeat
set URL of active tab of incognitoWindow to "http://youtube.com"
end tell
tell application "Opera"
try
make new window with properties {mode:"incognito"}
end try
set URL of active tab of front window to "https://yoururl.com"
end tell
I wrapped make new window with properties {mode:"incognito"} in try because I get an error "Opera got an error: AppleEvent handler failed." number -10000 because i use Opera browser.
It is not necessary when using Google Chrome.
I have been using below script to clear the safari history from selenium test in Yosemite OSX. I have upgraded the OS to El Capitan and now the script wont work.
Error returned : Can't get "Clear History" button
Any help on this please.
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "History"
tell menu 1
click menu item "Clear History…"
end tell
end tell
end tell
delay 1 -- optional
click button "Clear History"
end tell
end tell
tell application "Safari" to activate
set frontmost to true
tell application "System Events"
tell process "Safari"
set frontmost to true
click menu item "Clear History…" of menu "History" of menu bar 1
delay 3
click button "Clear History" of sheet 1 of window 1
end tell
end tell
tell application "Safari" to quit
I wanted to share a very useful AppleScript with other Mac admins but it is losing focus. When it's run in AppleScript editor it runs to a certain stage and then instead of copying text to required windows it replaces text in AppleScript editor. Can anyone suggest why?
set vpnname to "VPN (Primary)"
set vpnserver to "vpn.website.com"
set vpnsecret to "abcdefghij"
set vpnusername to system attribute "USER"
set groupname to "GROUP"
activate application "System Preferences"
tell application "System Events"
-- Checks whether Universal Access is enabled - this is required for all scripts to work
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the security dialog." with icon note
end tell
set UI elements enabled to true
if UI elements enabled is false then return "User Cancelled"
delay 1
end if
tell process "System Preferences"
click button "Network" of scroll area 1 of window "System Preferences"
--Creating VPN (CU Primary) interface
tell window "Network"
click button "Add Service"
delay 1
end tell
tell sheet 1 of window "Network"
click pop up button 1
click menu item "VPN" of menu 1 of pop up button 1
delay 1
click pop up button 2
click menu item "Cisco IPSec" of menu 1 of pop up button 2
set focused of text field 1 to true
keystroke "a" using command down
keystroke vpnname
click button "Create"
delay 1
end tell
--Entering server / account details
tell group 1 of window "Network"
click checkbox "Show VPN status in menu bar"
set focused of text field 3 to true
keystroke vpnserver
set focused of text field 1 to true
keystroke vpnusername
click button 2 --pressing "Advanced…" button
delay 1
end tell
--Entering "Advanced…" details
tell sheet 1 of window "Network"
activate
set focused of text field 2 to true
keystroke vpnsecret
set focused of text field 1 to true
keystroke groupname
click button "OK"
delay 1
end tell
-- Apply all changes
tell window "Network"
click button "Apply"
delay 1
end tell
tell application "System Preferences"
quit saving yes
end tell
end tell
end tell
I'm not sure if it will work but if any of the key strokes are going into applescript just run the
tell application "application name here"
activate
end tell
before the keystrokes that are being put into apple script then it will focus on the window you have told it to
Sorry for the lack of code snippet I am new to this forum and I'm not sure how to use it properly
How can I tell Chrome or Firefox to reload the document in the top window? Here's what I'm using for Safari:
osascript -e '
tell application "Safari"
activate
do JavaScript "history.go(0)" in document 1
end tell
'
Here's the code for Chrome:
tell application "Google Chrome"
tell the active tab of its first window
reload
end tell
end tell
Or more concisely:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
I do not think Firefox or Chrome have special Applescript support, but you can send the keystrokes (Cmd-R) to refresh the page:
tell application "Firefox"
activate
tell application "System Events" to keystroke "r" using command down
end tell
Here's another way to do it in Safari without using JavaScript:
tell application "Safari"
tell its first document
set its URL to (get its URL)
end tell
end tell
Automator
Open Automator and choose a New Document
Choose Service
Set Service receives to no input
Choose Run AppleScript action from the action list.
Paste the following code in the script:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
Save the the service, for example, using the name Chrome Refresh
System Preferences
Open System Preferences > Keyboard
In the Shortcuts tab, choose Services
Assign a new shortcut
The following answers above work well but using them result in DevTools refreshing in a new tab if it was the last tab/window in focus. I don't want DevTools to refresh in a new tab, I just want the first tab to refresh regardless of last focus/active and this worked well for me. Leaving for someone searching for this use case as well.
tell application "Google Chrome"
activate
tell application "System Events"
tell process "Google Chrome"
keystroke "r" using {command down, shift down}
end tell
end tell
end tell