1
votes

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
1
If Lauri Ranta's answer was the solution to your problem, click the green check mark next to the answer to accept it.Rob Hruska

1 Answers

2
votes

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