使用AppleScript自动单击对话框按钮

时间:2013-10-23 08:08:10

标签: applescript

我想使用AppleScript通过Safari自动登录网页。

当我打开网址并提供用户名和密码时,如果是第一次或尚未保存密码,它会显示消息:“您要保存此密码吗?”。

我想用AppleScript选择“Not now”。我怎么能这样做?

这是我目前的代码:

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

如果已从“系统偏好设置”启用辅助设备访问,则可以使用系统事件:

on enabledGUIScripting()
    do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with     administrator privileges
end enabledGUIScripting()

然后:

tell application "System Events" to tell process "Safari"
    click button 3 of sheet 1 of window 1
end tell

1 个答案:

答案 0 :(得分:2)

如果已从“系统偏好设置”启用辅助设备访问,则可以使用“系统事件:

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