I want to use AppleScript to auto login for a web by Safari.
When I open a URL and give username and password, if it is the first time or hasn't saved the password yet, it will show message: "Would you like to save this password?".
I want to use AppleScript to choose "Not now". How can I do that?
Here is my current code:
tell application "Safari"
open location "http://www.amazon.com"
activate
end tell
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
set loginURL to do JavaScript "document.getElementById('nav-your-account').getAttribute('href')" in document 1
open location loginURL
end tell
else
say "Page Failed to load or Safari didn't open"
end if
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
do JavaScript "document.getElementById('ap_email').value = 'mritjp'; document.getElementById('ap_password').value = 'mritjp'; document.forms['signIn'].submit();" in document 1
---HOW TO CONTROL TO CHOOSE "Not Now" in message "Would you like to save this password?" of safari
activate
delay 2
end tell
else
say "Page Failed to load or Safari didn't open"
end if
on page_loaded(timeout_value) -- in seconds
delay 1
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
Fixed by comment of Lauri Ranta:
If access for Assistive Devices has been enabled from System Preferences, you can use System Events:
on enabledGUIScripting()
do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with administrator privileges
end enabledGUIScripting()
Then:
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell
If access for assistive devices has been enabled from System Preferences, you can use System Events:
tell application "Safari"
do JavaScript "document.getElementById('login_button_id').click()" in document 1
end tell
delay 1
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell
Related
I am trying to write a script which can enter details in the system dialog. But i am not able to access it. Is there a way to access this? I tried getting the list of all the system dialogs and alerts but still I am not able to catch this.
tell application "System Events" to ¬
get name of every application process ¬
whose role description of window 1 is "system dialog"
system dialog
tell application "SecurityAgent"
activate
end tell
tell application "System Events"
tell process "SecurityAgent"
set value of text field 1 of window 1 to "Username"
set value of text field 2 of window 1 to "Password"
click button "Allow" of window 1
end tell
end tell
I am not sure following is what you asked for, but try yourself:
tell application "System Events" to tell process "SecurityAgent"
return name of every window
end tell
I am new to AppleScript and tried an AppleScript solution to open up a tab and load a webpage:
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
But if tab 2 doesn't exist, it won't do anything.
I then tried to solve it by checking whether an URL exist in any tab:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://mail.google.com/mail/u/0/#inbox"
end tell
But it is sort of an overkill, and it opens up a new tab, rather than opening up a tab 2.
I then tried
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
open tab 2 -- added this line
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
but no matter it is open tab 2 or activate tab 2 or start tab 2, it wouldn't work. How can we make it work and, in general, how to find out the vocabularies or verbs that we can use?
open tab 2 is the problem. If a browser window exists and you want to create a new tab and set its URL, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
make new tab at end of ¬
tabs of front window ¬
with properties ¬
{URL:"https://www.example.com"}
If a browser window exists and you want to change the URL of tab 2, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
tell front window to ¬
if (exists tab 2) then ¬
tell its tab 2 to ¬
set its URL to ¬
"https://www.example.com"
Notes:
The example AppleScript code works with Microsoft Edge too.
These examples can be expanded into normal block format using if statements to determine if Google Chrome and or Microsoft Edge exists, or are already running, and if running whether or not a window exists, etc. You just need to code it according to your needs/wants.
Have a look at AppleScript Language Guide
In Script Editor have a look at the Library for the AppleScript dictionary of any application you want to write code for. From the Window menu... Library ⇧⌘L
The example AppleScript code below shows an example of coding for the existence of either Google Chrome or Microsoft Edge and whether or not they are running when you can then write code based on the conditions. As written it favors Google Chrome but if it does exist then Microsoft Edge runs its code. To favor Microsoft Edge change the primary if statement structure to accommodate.
tell application "System Events"
set existsGoogleChrome to exists file "/Applications/Google Chrome.app"
set existsMicrosoftEdge to exists file "/Applications/Microsoft Edge.app"
end tell
if existsGoogleChrome then
if running of application "Google Chrome" then
tell application "Google Chrome"
# Do Something
end tell
else
tell application "Google Chrome"
# Do Something Else
end tell
end if
else
if existsMicrosoftEdge then
if running of application "Microsoft Edge" then
tell application "Microsoft Edge"
# Do Something
end tell
else
tell application "Microsoft Edge"
# Do Something Else
end tell
end if
end if
end if
I have a simple applescript that opens Safari, opens a web page 100 times (long story) and then waits 300 seconds to close Safari.
Here is the code:
set theURL to "https://sites.google.com"
tell application "Safari"
activate
repeat 100 times
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
end repeat
end tell
delay 300
tell application "Safari"
quit
end tell
The first part of the script runs famously. When I get past the delay, I am getting an error:
error "Safari got an error: User canceled." number -128
I tried to just run it without opening multiple web pages Just activate Safari, wait 10 second and quit. I get the same problem.
Has anyone dealt with this?
I'm not getting the error you describe. However I am seeing a bug where Safari is not activating if the application was not already running when I run the code.
This code seems to work as you'd expect...
set theURL to "https://sites.google.com"
tell application "Safari" to activate
tell application "Safari"
activate
repeat 2 times
try
tell window 1 to set current tab to make new tab
set URL of document 1 to theURL
on error
open location theURL
end try
end repeat
end tell
delay 5
tell application "Safari"
quit
end tell
Is it possible to use AppleScript to open a link in a new tab in Safari?
This will work:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
I think this also does what you asked for, but it is much shorter and is less browser-specific:
do shell script "open http://www.webpagehere.com"
This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:
do shell script "open -a Safari 'http://www.webpagehere.com'"
This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
It opens a new window if "Open pages in tabs instead of windows" is set to never though.
tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:
set u to "http://ja.wikipedia.org/wiki/漢字"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
This opens a new tab even when new pages are set to open in windows:
tell application "Safari"
activate
reopen
tell (window 1 where (its document is not missing value))
if name of its document is not "Untitled" then set current tab to (make new tab)
set index to 1
end tell
set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
perform action "AXRaise" of window 1
end tell
set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.
I've been using the following script to open hundreds of docs into tabs in a single window.
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://foo.com/bar"}
make new tab with properties {URL:"http://foo.com/baz"}
end tell
end tell
But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:
tell application "Safari"
tell window 1
set URL of (make new tab) to "http://foo.com/bar"
set make new tab to "http://foo.com/baz"
end tell
end tell
Code:
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell
One problem is that this only works if the system's language is set to English.
It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:theURL}
on error
open location theURL
end try
end tell
I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
Arrgh -- the one line is overflowing. Here it is without the code tags:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
I ended up using automator to do this which was much easier and it works.
You can try following approach:
//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
tell window 1
//create new tab and open specified URL
tab with properties {URL:"https://url.com"})
//make tab active
set visible to true
end tell
end tell
Also u can combine usage of Apple script within FastScript (free for 10 shortcut)
To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.
If you want to open new Window than new tab u can play within next:
tell application "System Events"
tell process "Safari"
click menu item "New window" of menu "File" of menu bar 1
end tell
end tell
Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.
Not the shortest solution but also works, and not only in English ...
tell application "Safari"
activate
end tell
tell application "System Events"
set frontmost of process "Safari" to true
keystroke "t" using {command down}
end tell
set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL
Worked for me in Safari v.11
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell
I found a way to open a new tab in the background with Safari.
tell application "Safari"
set the URL of (make new tab in window 1) to "your.url.net"
end tell
During the time I wrote this answer I made this
tell application "Safari"
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
end try
end tell
New version
tell application "Safari"
repeat
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
if theURL is "" then exit repeat
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
if button returned of result is "No" then exit repeat
end try
end repeat
end tell
Any suggestions will be appreciate
Best regards
I don't have a Wi-Fi router, so when at home I need to turn my laptop into a Wi-Fi source so that both myself and my partner can access the internet.
However during the days I work at a coffee shop and require the use of their Wi-Fi.
I'm running Snow Leopard and I find it stupidly cumbersome to constantly be turning off and on, first Internet Sharing and then my Wi-Fi.
Any ideas for a quick 'n' dirty AppleScript solution?
You can use launchctl to programmatically start or stop the Internet Sharing service.
The following AppleScript will start Internet Sharing:
do shell script "/bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges
The following AppleScript will stop Internet Sharing:
do shell script "/bin/launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges
I'm using this AppleScript from Automator so that I can easily use it as a service and give it a keyboard shortcut.
Toggle Internet Sharing:
register_growl()
try
if isRunning("InternetSharing") then
do shell script "launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges
if isRunning("InternetSharing") then
error "Internet Connection Sharing was Not Disabled"
else
my growlnote("Success", "Internet Connection Sharing Disabled")
end if
else
do shell script "launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges
if isRunning("InternetSharing") then
my growlnote("Success", "Internet Connection Sharing Enabled")
else
error "Internet Connection Sharing was Not Enabled"
end if
end if
on error errMsg
my growlnote("Error", errMsg)
end try
on isRunning(processName)
try
return 0 < length of (do shell script "ps ax | grep -v grep | grep " & processName)
on error
return false
end try
end isRunning
on register_growl()
try
tell application "GrowlHelperApp"
set the notificationsList to {"Success", "Warning", "Error"}
register as application "Toggle Internet Connection Sharing" all notifications notificationsList default notifications notificationsList icon of application "Sharing"
end tell
end try
end register_growl
on growlnote(growltype, str)
try
tell application "GrowlHelperApp"
notify with name growltype title growltype description str application name "Toggle Internet Connection Sharing"
end tell
end try
end growlnote
I am cross-posting this on the Apple stack exchange because the question was asked in both places.
Not sure if you are still looking for a solution but... here is an apple script to enable or disable internet sharing
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events"
tell process "System Preferences"
try
click checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing"
if checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
repeat until sheet of window 1 exists
delay 0.5
end repeat
end if
if (sheet of window 1 exists) then
click button "Start" of sheet of window 1
end if
tell application "System Preferences" to quit
activate (display dialog "Internet Sharing preferences sucessfully flipped")
on error
activate
display dialog "something went wrong in automation but you are probably in the right menu..."
return false
end try
end tell
end tell
I also will post this on the apple stack exchange post.
Here's what I came up with for Mojave to toggle Internet Sharing using (mostly) accessibility — unfortunately none of the solutions involving using launchctl and/or editing com.apple.nat.plist worked for me.
open location "x-apple.systempreferences:com.apple.preferences.sharing?Internet"
tell application "System Events"
tell process "System Preferences"
repeat until window "Sharing" exists
delay 0.1
end repeat
tell window "Sharing"
set _row to group 1's scroll area 1's table 1's first row whose selected is true
set _wasSharing to _row's checkbox's value as number
if _wasSharing is 1 then
click _row's checkbox
set _wasSharing to _row's checkbox's value as number
repeat until _wasSharing is 0
delay 0.1
end repeat
end if
if _wasSharing is 0 then
click _row's checkbox
repeat until sheet 1 exists
delay 0.1
end repeat
click sheet 1's button "Start"
end if
end tell
end tell
end tell
tell application "System Preferences" to quit