1

我正在尝试编写一个简单的 AppleScript 来对剪贴板的内容进行一些文本操作。以下代码仅在剪贴板中的文本可以转换为数字时才有效:

set CB to the clipboard
set bugNum to CB as number

但如果剪贴板中的文本不是数字,我会收到 AppleScript 错误:“无法将“foo”转换为类型数字。”

如何编写一个 if 条件来检查 CB(类文本)是否可以转换为数字(并将“将 bugNum 设置为 CB 作为数字”放入其中)?或者我能以某种方式“捕捉”错误吗?

4

1 回答 1

3

您是否已经阅读过 Applescript 指南中的处理错误?

示例代码:

set CB to the clipboard
try
    set bugNum to CB as number
on error errStr number errNum
    set bugNum to -1
end try
display dialog "Number from the clipboard: " & bugNum

我第一次跳过了代码,因为我认为指南非常清晰,而且比我在这里写的任何东西都要有用:)

于 2009-05-27T23:44:55.667 回答