6

熟悉编程,我缺少使用三元运算符分配变量的能力(即“x如果某事为真,则将变量设置为,否则将其设置为y”)。我在想类似的东西:

set my_string to (if a is 0 return "" else return " - substring")

这当然行不通,我还没有发现任何类似的东西。是否有另一种方法可以用 applescript 实现这一目标?

4

2 回答 2

3

看起来 AppleScript 不支持条件运算符,但您可以为此目的使用带有两个元素的列表。当然,总的来说它不是很优雅:

set my_string to item (((a is 0) as integer) + 1) of {"", " - substring"}

还有另一种方法:您可以使用 shell 脚本

set b to (do shell script "test " & a & " -eq 0 && echo 'is 0' || echo 'is not 0'")

我怎么能忘记这个?:)

在您的情况下,它会更加简单(因为如果根本没有回声,将返回一个空字符串)。

set b to (do shell script "test " & a & " -eq 0 || echo '- substring'")
于 2012-12-27T12:54:05.320 回答
2
if a is 0 then
    set my_string to ""
else
    set my_string to " - substring"
end if

或者

set a to 7

set my_string to my subTern(a)

on subTern(aLocalVar)
    if aLocalVar is 0 then return ""
    if aLocalVar is not 0 then return " - substring"
end subTern
于 2012-12-25T21:50:45.140 回答