我試圖自動化Google Docs登錄過程,但我認爲這樣做的方法不會產生任何結果。代碼如下。JavaScript中的AppleScript字符串連接
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<mpl=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類型文本。」
我也嘗試使用下面的,而不是用keystroke
命令一起改變字段的值
execute javascript "document.getElementById('Email').click()"
。但是,這也沒有用。我做錯了什麼,還是僅僅是AppleScript?
值得注意的是,下面的代碼工作execute javascript "document.getElementById('Email').value = '" & "Hello" & "'"
。
這解決了這個問題。謝謝! – Josh 2012-07-25 14:47:53