一种解决方法是使用 AppleScript 的“do JavaScript”命令来执行 JavaScript 代码,并使用“return”语句来返回值。然后,使用“set”命令将该值存储在 AppleScript 变量中。
示例代码如下:
set theScript to "function myFunction() { return 'Hello World'; } myFunction();"
set theResult to do JavaScript theScript
set theValue to (theResult as text)
在上面的代码中,我们首先定义要执行的 JavaScript 代码,并将其存储在变量“theScript”中。然后,我们使用“do JavaScript”命令执行该代码,并将返回的结果存储在变量“theResult”中。最后,我们将变量“theValue”设置为从“theResult”变量转换而来的文本值。
通过这种方式,我们可以用 AppleScript 轻松地执行 JavaScript 代码,并获得返回的值。