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

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

问题描述

我试图写一个简单的AppleScript做剪贴板上的内容,一些文本操作。下面code工作只有在剪贴板中的文字可以转换成一个数字:

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的错误:到类型号,不能使富

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

我如何写,如果我可以用它来检查,​​如果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?

推荐答案

你已经读过<一个href=\"http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_error_xmpls.html\"相对=nofollow>有错误的工作的AppleScript的指南?

Did you already read working with errors in the Applescript guide?

样code:

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

我跳过了code中的第一次全面,因为我想通了导游是pretty清晰,远远超过任何我可以在这里写更多有用:)

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天全站免登陆