如何创建Applescript动态变量
我有一个脚本,其中包含一系列术语。我想根据每个术语创建变量。我试图计算用户选择每个术语的次数,并记录与该术语相关联的数字(也来自用户输入) 我的jQuery历史让我想做如下事情:如何创建Applescript动态变量,applescript,Applescript,我有一个脚本,其中包含一系列术语。我想根据每个术语创建变量。我试图计算用户选择每个术语的次数,并记录与该术语相关联的数字(也来自用户输入) 我的jQuery历史让我想做如下事情: set term + "_Count" to term + "_Count" + 1 set term + "_Log" to term + "_Log" + inputNum 然而,(显然)这种语法在AppleScript中是不可能的。有没有办法将字符串连接到变量名上 --为了便于参考,我尽量避免列出每个术语,因为
set term + "_Count" to term + "_Count" + 1
set term + "_Log" to term + "_Log" + inputNum
然而,(显然)这种语法在AppleScript中是不可能的。有没有办法将字符串连接到变量名上
--为了便于参考,我尽量避免列出每个术语,因为我试图设置与每个术语相关的2个变量。目前,我有一个很长的If/Then语句,用于在用户选择术语时设置每个术语
- 术语:“项目”
- termCount:3——激活次数
- 术语日志:120-- 会议记录
变量名在编译时计算。动态变量(在运行时计算)是不可能的。您真的不可能。您需要的是一个
字典
数据类型(也称为“散列”、“映射”、“关联数组”等),它使用任意键(最常见的字符串)存储和检索值。AS没有本机字典类型,但是为了存储简单的值类型(布尔、整数、实数、字符串),可以通过AppleScript ObjC桥使用Cocoa的NSMutableDictionary
类:
use framework "Foundation"
set myDict to current application's NSMutableDictionary's dictionary()
myDict's setValue:32 forKey:"Bob"
myDict's setValue:48 forKey:"Sue"
(myDict's valueForKey:"Bob") as anything
--> 32
你真的不知道。您需要的是一个
字典
数据类型(也称为“散列”、“映射”、“关联数组”等),它使用任意键(最常见的字符串)存储和检索值。AS没有本机字典类型,但是为了存储简单的值类型(布尔、整数、实数、字符串),可以通过AppleScript ObjC桥使用Cocoa的NSMutableDictionary
类:
use framework "Foundation"
set myDict to current application's NSMutableDictionary's dictionary()
myDict's setValue:32 forKey:"Bob"
myDict's setValue:48 forKey:"Sue"
(myDict's valueForKey:"Bob") as anything
--> 32
在不查看所有代码的情况下,很难给出准确的答案,但可以使用串联来定义新变量 如果要将以下代码另存为应用程序,请将所选项目及其选择次数存储在脚本中,并更新值 同样,很难准确地确定您需要什么,但这是一个串联示例,可以定义一个项目被选择了多少次
property theUserChose : {"Project_1", "Project_2", "Project_3"}
property term_1_count : 0
property term_2_count : 0
property term_3_count : 0
property minutes : 120
property term : missing value
set resultValue to choose from list theUserChose ¬
with title "Make Your Choice" OK button name ¬
"OK" cancel button name "Cancel" without empty selection allowed
set resultValue to resultValue as string
if resultValue is item 1 of theUserChose then
set term_1_count to term_1_count + 1
set term to resultValue & "_" & term_1_count & "_" & minutes
else
if resultValue is item 2 of theUserChose then
set term_2_count to term_2_count + 1
set term to resultValue & "_" & term_2_count & "_" & minutes
else
if resultValue is item 3 of theUserChose then
set term_3_count to term_3_count + 1
set term to resultValue & "_" & term_3_count & "_" & minutes
end if
end if
end if
display dialog term
在不查看所有代码的情况下,很难给出准确的答案,但可以使用串联来定义新变量 如果要将以下代码另存为应用程序,请将所选项目及其选择次数存储在脚本中,并更新值 同样,很难准确地确定您需要什么,但这是一个串联示例,可以定义一个项目被选择了多少次
property theUserChose : {"Project_1", "Project_2", "Project_3"}
property term_1_count : 0
property term_2_count : 0
property term_3_count : 0
property minutes : 120
property term : missing value
set resultValue to choose from list theUserChose ¬
with title "Make Your Choice" OK button name ¬
"OK" cancel button name "Cancel" without empty selection allowed
set resultValue to resultValue as string
if resultValue is item 1 of theUserChose then
set term_1_count to term_1_count + 1
set term to resultValue & "_" & term_1_count & "_" & minutes
else
if resultValue is item 2 of theUserChose then
set term_2_count to term_2_count + 1
set term to resultValue & "_" & term_2_count & "_" & minutes
else
if resultValue is item 3 of theUserChose then
set term_3_count to term_3_count + 1
set term to resultValue & "_" & term_3_count & "_" & minutes
end if
end if
end if
display dialog term
好极了这很有帮助。它破坏了我代码的其他几个部分,但我想我可以解决这个问题。谢谢你的帮助:)太棒了!这很有帮助。它破坏了我代码的其他几个部分,但我想我可以解决这个问题。谢谢你的帮助:)