1

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. Commented Oct 31, 2013 at 1:55

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help. As your answer: [access for assistive devices has been enabled from System Preferences]. I also write a script to auto enable that, but we cannot handle to input username, password or auto click on Unlock for "SecurityAgent" (Because access for assistive devices has been NOT enabled)
Done: on enabledGUIScripting() do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with administrator privileges end enabledGUIScripting
Would this work for things other than safari? I have the same problem with a dialog box in illustrator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.