如何让osascript不构建不会在运行时命中的AppleScript代码?

时间:2013-11-11 07:15:29

标签: applescript

我们有一个脚本可以使用Microsoft outlookApple mail应用程序发送电子邮件。它将从系统首选项(也可能是用户输入)动态加载默认电子邮件,并使用它来决定使用哪个邮件客户端。

所以代码如下:

if (mailClientStr contains "outlook")
   tell application id "com.microsoft.outlook" 
   -- <<< there will be error if there is no outlook installed 
   -- <<< even else branch will be run.
      ...
   end tell
else
   tell application id "com.apple.mail"
      ...
   end tell
end if

在未安装Outlook的计算机上,mailClientStr将为“com.apple.mail”,但此脚本无法由osascript

运行

它会抱怨Can’t get application id "com.microsoft.outlook"即使第一个分支也不会被执行。我的理解是osascript在加载和编译此脚本时(运行之前)需要访问Outlook apple脚本界面。

我可以将与outlook相关的代码分成单独的脚本,但由于要传递大量数据,因此它很复杂,所以我不希望这种解决方法。 那苹果脚本语言方面有没有解决方案呢?

1 个答案:

答案 0 :(得分:0)

从AppleScript语言指南:

Entering Script Information in Raw Format

You can enter double angle brackets, or chevrons («»), directly into a script by typing Option-Backslash and Shift-Option-Backslash. You might want to do this if you’re working on a script that needs to use terminology that isn’t available on your current machine—for example, if you’re working at home and don’t have the latest dictionary for a scriptable application you are developing, but you know the codes for a supported term.

You can also use AppleScript to display the underlying codes for a script, using the following steps:

    Create a script using standard terms compiled against an available application or scripting addition.

    Save the script as text and quit Script Editor.

    Remove the application or scripting addition from the computer.

    Open the script again and compile it.

    When AppleScript asks you to locate the application or scripting addition, cancel the dialog.

Script Editor can compile the script, but displays chevron format for any terms that rely on a missing dictionary