缺失值误差在Safari中大量使用Javascript通过AppleScript的时 [英] Missing Value Errors when heavily using Javascript in Safari by Applescript

查看:150
本文介绍了缺失值误差在Safari中大量使用Javascript通过AppleScript的时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我可能是混乱的称号。

Sorry for the possibly confusing title.

我的问题是,我想下载苹果的命令行工具X code,无需要点击按钮单。我想用这个为新的开发人员工作站,厨师结合更快的设置。

My problem is that I'd like to download Apple's Command Line Tools for XCode without the need to click on the single buttons. I want to use this for a faster setup of new developer workstations combined with Chef.

脚本和错误如下:

tell application "Safari"
    make new document at end of every document --> document "Ohne Titel"
    set URL of document 1 to "https://developer.apple.com/downloads/index.action"
    get name of document 1 --> "Ohne Titel"
    do JavaScript "var head= document.getElementsByTagName('head')[0];
                   var script= document.createElement('script');
                   script.type= 'text/javascript';
                   script.src= 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
                   head.appendChild(script);" in document 1 --> missing value
    do JavaScript "alert('test')" in document 1 --> missing value
    do JavaScript "document.forms['appleConnectForm']['accountname'].value = 'somename'" in document 1 --> missing value
    do JavaScript "document.forms['appleConnectForm']['accountpassword'].value = 'somepassword'" in document 1 --> missing value
    do JavaScript "document.appleConnectForm.submit();" in document 1 --> missing value
    do JavaScript "var atags = document.getElementsByTagName('a')" in document 1 --> missing value
    do JavaScript "for (var i=0; i<atags.length; i++){ if (atags.textContent=='Command Line Tools for Xcode'){ atags[i].click(); } }" in document 1 --> missing value
    set name of window 1 to "Ohne Titel"
end tell

更新的脚本和错误我遇到了在这里: https://gist.github.com/1993352

Updates to the the script and errors I am running into here: https://gist.github.com/1993352

17行JavaScript的正确执行,我看到一条警告消息。如预期的任何其他线路的不执行。

The Javascript on line 17 is properly executed and I see an alert message. Any of the other lines is not executed as expected.

我怎样才能得到这个工作,是当前的Javascript除了这一条线不执行?

How can I get this to work and is the current Javascript not executed except for this one line?

推荐答案

由于@Jordan陈述<一个href=\"http://stackoverflow.com/questions/9603288/missing-value-errors-when-heavily-using-javascript-in-safari-by-applescript#comment12182793_9603288\">above,在缺失值返回做的JavaScript 不表示一个错误。但是,您的code从做停止工作那几个问题的困扰:

As @Jordan stated above, the missing value returned from do JavaScript does not indicate an error. However, your code suffers from a few problems that do stop it from working:


  1. 不等待网页试图访问窗体之前完全加载 - 因此你的分配和提交()调用进入虚空。你可以去幻想和尝试等待匹配的窗口名称,但一个简单的延迟做到;

  2. 它试图检查所有找到的元素的文本内容,而不是( atags.textContent )当前循环结束一( atags [我] .textContent );

  3. 它试图触发点击事件通过调用点击()方法,大多数浏览器拒绝这样做,表面上是出于安全原因(见这个问题并的的>的答案)。由于苹果不包括jQuery的,这个问题是不是很简单的回避在我挂了答案 - 你可以,但是,有一点Protolicious的 event.simulate.js

  1. it does not wait for the page to load completely before trying to access the form – hence your value assignments and submit() call go into the void. You can go fancy and try to wait for a matching window name, but a simple delay will do;
  2. it tries to inspect the text content of all found elements (atags.textContent) instead of the currently looped-over one (atags[i].textContent);
  3. it tries to trigger a click event by calling the click() method, which most browsers refuse to do, ostensibly for security reasons (see this question and answer of mine). As Apple does not include jQuery, the issue is not quite as straightforward to sidestep as in the answer I linked to – you can, however, simulate the click event with a bit of code courtesy of Protolicious’ event.simulate.js.

以下code会做你问什么:

The following code will do what you asked for:

tell application "Safari"
    set loadDelay to 2 -- in seconds; test for your system
    make new document at end of every document
    set URL of document 1 to "https://developer.apple.com/downloads/index.action"
    delay loadDelay
    do JavaScript "_connectForm = document.forms.appleConnectForm;
                   _connectForm.accountname.value = 'your_accountname';
                   _connectForm.accountpassword.value = 'your_accountpassword';
                   _connectForm.submit();" in document 1
    delay loadDelay
    do JavaScript "var _atags = document.querySelectorAll('a');
                   for (var i=0; i<_atags.length; i++){ 
                       if (_atags[i].innerText.indexOf('Command Line Tools for Xcode') === 0){ 
                           _event = document.createEvent('MouseEvents');
                           _event.initMouseEvent('click', true, true, document.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, _atags[i]);
                           _atags[i].dispatchEvent(_event);
                           break;
                       }
                   }" in document 1
end tell

用我自己的账户进行测试;最新的X code命令行工具包下载(是precise - 最上面列出)。成功启动

这篇关于缺失值误差在Safari中大量使用Javascript通过AppleScript的时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆