Applescript:number/number-word 值问题
Applescript: Issue with number/number-word values
到目前为止我有这个:
tell application "Finder"
set number_of_items to (count (every item of the desktop))
set correct_grammar to "" as text
if number_of_items is 1 then
set number_of_items to "one" as text
end if
if number_of_items is greater than 1 then
set s_letter to "s" as text
end if
set correct_grammar to "are" as text
if number_of_items is 1 then
set correct_grammar to "is" as text
end if
if number_of_items is greater than 1 then
set correct_grammar to "are"
end if
set s_letter to "s" as text
if number_of_items is 1 then
set s_letter to "" as text
end if
tell application "System Events"
display dialog "There " & correct_grammar & " " & number_of_items & " file" & s_letter & " in this folder." buttons {"Yes", "Exit"} with title "Test"
end tell
end tell
我试图让一个对话框显示文本 "one" 而不是数字值“1” 一旦我这样做,脚本就会停止修复语法。下面的代码部分是导致问题发生的原因,但这是正确语法的全部意图。
if number_of_items is 1 then
set number_of_items to "one" as text
end if
脚本的第二行,您必须将 number_of_items 强制转换为字符串:
set number_of_items to (count (every item of the desktop)) as string
并在(桌面上每个项目的计数)
上进行其他测试 "if"
实际上您的代码是正确的,因为 number_of_items
的整数值被隐式强制转换为文本,尽管明确地这样做是一个好习惯。
代码可以写的短一点:
tell application "Finder" to set number_of_items to (count items of desktop)
if number_of_items is 1 then
set correct_grammar to "is"
set number_of_items to "one"
set s_letter to ""
else
set s_letter to "s"
set correct_grammar to "are"
end if
tell application "System Events"
display dialog "There " & correct_grammar & " " & (number_of_items as text) & " file" & s_letter & " in this folder." buttons {"Yes", "Exit"} with title "Test"
end tell
编辑
如果您使用 Yosemite 及更高版本,您可以使用 AppleScriptObjectiveC 和 Cocoa 的 NSNumberFormatter
来创建数字字符串
use framework "Foundation"
use scripting additions
set formatter to current application's NSNumberFormatter's alloc()'s init()
set numberStyle of formatter to 5 -- NSNumberFormatterSpellOutStyle
tell application "Finder" to set number_of_items to (count items of desktop)
if number_of_items is 1 then
set correct_grammar to "is"
set s_letter to ""
else
set correct_grammar to "are"
set s_letter to "s"
end if
set numberString to (formatter's stringFromNumber:number_of_items) as text
tell application "System Events"
display dialog "There " & correct_grammar & " " & numberString & " file" & s_letter & " in this folder." buttons {"Yes", "Exit"} with title "Test"
end tell
到目前为止我有这个:
tell application "Finder"
set number_of_items to (count (every item of the desktop))
set correct_grammar to "" as text
if number_of_items is 1 then
set number_of_items to "one" as text
end if
if number_of_items is greater than 1 then
set s_letter to "s" as text
end if
set correct_grammar to "are" as text
if number_of_items is 1 then
set correct_grammar to "is" as text
end if
if number_of_items is greater than 1 then
set correct_grammar to "are"
end if
set s_letter to "s" as text
if number_of_items is 1 then
set s_letter to "" as text
end if
tell application "System Events"
display dialog "There " & correct_grammar & " " & number_of_items & " file" & s_letter & " in this folder." buttons {"Yes", "Exit"} with title "Test"
end tell
end tell
我试图让一个对话框显示文本 "one" 而不是数字值“1” 一旦我这样做,脚本就会停止修复语法。下面的代码部分是导致问题发生的原因,但这是正确语法的全部意图。
if number_of_items is 1 then
set number_of_items to "one" as text
end if
脚本的第二行,您必须将 number_of_items 强制转换为字符串:
set number_of_items to (count (every item of the desktop)) as string
并在(桌面上每个项目的计数)
上进行其他测试 "if"实际上您的代码是正确的,因为 number_of_items
的整数值被隐式强制转换为文本,尽管明确地这样做是一个好习惯。
代码可以写的短一点:
tell application "Finder" to set number_of_items to (count items of desktop)
if number_of_items is 1 then
set correct_grammar to "is"
set number_of_items to "one"
set s_letter to ""
else
set s_letter to "s"
set correct_grammar to "are"
end if
tell application "System Events"
display dialog "There " & correct_grammar & " " & (number_of_items as text) & " file" & s_letter & " in this folder." buttons {"Yes", "Exit"} with title "Test"
end tell
编辑
如果您使用 Yosemite 及更高版本,您可以使用 AppleScriptObjectiveC 和 Cocoa 的 NSNumberFormatter
来创建数字字符串
use framework "Foundation"
use scripting additions
set formatter to current application's NSNumberFormatter's alloc()'s init()
set numberStyle of formatter to 5 -- NSNumberFormatterSpellOutStyle
tell application "Finder" to set number_of_items to (count items of desktop)
if number_of_items is 1 then
set correct_grammar to "is"
set s_letter to ""
else
set correct_grammar to "are"
set s_letter to "s"
end if
set numberString to (formatter's stringFromNumber:number_of_items) as text
tell application "System Events"
display dialog "There " & correct_grammar & " " & numberString & " file" & s_letter & " in this folder." buttons {"Yes", "Exit"} with title "Test"
end tell