2012-07-25 73 views
0

我試圖自動化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&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類型文本。」

我也嘗試使用下面的,而不是用keystroke命令一起改變字段的值
execute javascript "document.getElementById('Email').click()"。但是,這也沒有用。我做錯了什麼,還是僅僅是AppleScript?

值得注意的是,下面的代碼工作
execute javascript "document.getElementById('Email').value = '" & "Hello" & "'"

回答

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 
+0

這解決了這個問題。謝謝! – Josh 2012-07-25 14:47:53