如何检查文本是否可以在 AppleScript 中转换为数字? [英] How do I check if text can be converted to a number in AppleScript?

查看:29
本文介绍了如何检查文本是否可以在 AppleScript 中转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write a simple AppleScript to do some text manipulation on the contents of the clipboard. The following code works only if the text in the clipboard can be converted to a number:

set CB to the clipboard
set bugNum to CB as number

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

But if the text in the clipboard is not a number I get an AppleScript error: "Can’t make "foo" into type number."

我如何编写一个 if 条件,我可以用它来检查 CB(类文本)是否可以转换为数字(并将将 bugNum 设置为 CB 作为数字"放在里面)?或者我可以以某种方式捕获"错误?

How do I write an if condition that I can use to check if CB (of class text) can be converted to a number (and put the "set bugNum to CB as number" inside that)? Or can I "catch" the error somehow?

推荐答案

您是否已经阅读了 在 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

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

I skipped the code the first time round since I figured the guide was pretty clear, and far more useful than anything I could write here :)

这篇关于如何检查文本是否可以在 AppleScript 中转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆