2012-09-28 82 views
0

我用脚本编辑器试过这个苹果脚本。 但它不会将我带到手动点击时通常会出现的页面。 它似乎什么都不做:(如何在AppleScript中执行JavaScript函数?

do JavaScript "doCallerPage('FORM_PAGE', 'FR_AN', '/as/an/Check.do');return false;" in document 1 

在HTML源链接

<a id="LNK0001" href="javascript:void(0);" onclick="doCallerPage('FORM_PAGE', 'FR_AN', '/as/an/Check.do');return false;">Check Page</a> 

JavaScript函数

function doCallerPage(formName, target, actionPath, _action, _t) { 
     if (actionPath == "") { 
      return false; 
     } 

     if (target && target != "_self") { 
      checkSubWindow(target); 
     } 
     if (document.FORM_PAGE.ctrlflg.value == "") { 
      if (_action) { 
       document.FORM_PAGE._ACTION.value = _action; 
      } 
      else if (!_action) { 
       document.FORM_PAGE._ACTION.value = ""; 
      } 
     } 
     if (_t) { 
      document.FORM_PAGE._T.value = _t; 
     } 
     else { 
      document.FORM_PAGE._T.value = ""; 
     } 
     document.FORM_PAGE.action = actionPath; 
     document.FORM_PAGE.target = target; 
     document.FORM_PAGE.submit(); 
     document.FORM_PAGE._P.value = ""; 
     document.FORM_PAGE._T.value = ""; 
     document.FORM_PAGE._ACTION.value = ""; 
     document.FORM_PAGE.ctrlflg.value = ""; 
} 
+0

可能的重复[为什么在JavaScript中调用Applescript时执行相同的代码输入到浏览器执行?](http://stackoverflow.com/questions/6561452/why-doesnt-do-javascript- call-in-applescript-execute-when-the-code-typed) – Barmar

+0

谢谢指出。我之前尝试过。但是它并不适用于我的情况:( – MKK

+0

)基本问题是JavaScript函数只存在于定义它们的页面上下文中,不能仅仅运行一个JavaScript函数本身 – Barmar

回答

0

的语法是有点不同的,它必须是JavaScript的。使用jQuery在Chrome上可以这样做:

tell application "Google Chrome" activate set jq to do shell script "curl https://code.jquery.com/jquery-latest.min.js" execute front window's active tab javascript jq execute front window's active tab javascript "jQuery('body').css({'color':'blue'});" end tell

香草JS:

tell application "Google Chrome" activate execute front window's active tab javascript "if (window.console) console.log('hi');" end tell

您可以在其他应用中做其他事情,而这种情况正在发生的留在浏览器窗口中的,直到脚本完成脚本,只要执行。