0

我正在尝试自动化 Google Docs 登录过程,但我认为可行的方法一无所获。代码如下。

display dialog "Username" default answer ""
set Username to the result
display dialog "Password" default answer "" with hidden answer
set myPassword to the result
tell application "Google Chrome"
    tell window 1
        set newTab to make new tab with properties {URL:"https://accounts.google.com/ServiceLogin?service=writely&passive=1209600&continue=https://docs.google.com/?tab%3Dwo%23&followup=https://docs.google.com/?tab%3Dwo&ltmpl=homepage"}
        repeat 50 times --wait until the page loads...
            delay 0.1
        end repeat
        tell active tab
            execute javascript "document.getElementById('Email').value = '" & Username & "'"
        end tell
    end tell
end tell

有问题的代码是execute javascript "document.getElementById('Email').value = '" & Username & "'". 每当我尝试使用字符串连接执行 JavaScript 时,都会收到以下错误:“无法将 {text returned:"hello", button returned:"OK"} 转换为 Unicode 文本类型。”

我还尝试使用以下内容,而不是使用命令更改字段的
execute javascript "document.getElementById('Email').click()"keystroke。然而,这也没有奏效。我做错了什么,还是只是 AppleScript?

值得注意的是,以下代码有效
execute javascript "document.getElementById('Email').value = '" & "Hello" & "'"

4

1 回答 1

1

因为display dialog命令的结果是一条记录。

要获取字符串,请使用:

display dialog "Username" default answer ""
set Username to text returned of the result
display dialog "Password" default answer "" with hidden answer
set myPassword to text returned of the result
于 2012-07-25T14:42:35.293 回答