Applescript 在 Safari 中大量使用 Javascript 时出现缺失值错误 [英] Missing Value Errors when heavily using Javascript in Safari by Applescript
问题描述
抱歉,标题可能令人困惑.
Sorry for the possibly confusing title.
我的问题是我想下载 Apple 的 XCode 命令行工具,而无需单击单个按钮.我想用它来更快地设置与 Chef 结合的新开发人员工作站.
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 所述 以上,从 do JavaScript
返回的 missing value
并不表示错误.但是,您的代码存在一些问题,使其无法正常工作:
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:
- 在尝试访问表单之前它不会等待页面完全加载 - 因此您的
value
分配和submit()
调用进入无效状态.您可以尝试等待匹配的窗口名称,但是一个简单的delay
就可以了; - 它尝试检查所有找到的元素的文本内容 (
atags.textContent
) 而不是当前循环的元素 (atags[i].textContent
); - 它试图通过调用
click()
方法来触发click
事件,大多数浏览器拒绝这样做,表面上是出于安全原因(请参阅 这个问题 和我的答案).由于 Apple 不包含 jQuery,因此该问题并不像我链接到的答案中那样直接回避——但是,您可以使用 Protolicious 提供的一些代码模拟点击事件event.simulate.js
.
- it does not wait for the page to load completely before trying to access the form – hence your
value
assignments andsubmit()
call go into the void. You can go fancy and try to wait for a matching window name, but a simpledelay
will do; - it tries to inspect the text content of all found elements (
atags.textContent
) instead of the currently looped-over one (atags[i].textContent
); - it tries to trigger a
click
event by calling theclick()
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
.
以下代码将完成您的要求:
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
使用我自己的帐户进行测试;下载最新的 Xcode 命令行工具包(准确地说是最上面列出的)已成功启动.
这篇关于Applescript 在 Safari 中大量使用 Javascript 时出现缺失值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!