如何将隐藏在 applescript 字符串中的变量转换为可读变量?

How to convert a variable hidden in a string in applescript to a readable variable?

我在 applescript 中制作了一个邮件宏,使用 excel 作为数据向不同的人发送邮件(例如,亲爱的 ,您的队友是 )。这个脚本有效。

为了使脚本更灵活并且也可供其他人使用,我使用上面的伪代码将邮件信息放在一个文本文件中。

themessage.txt

Dear <firstname>, Your team mates are <teammate1> and <teammate2>.

然后我让脚本将文本从 themessage.txt 更改为 applescript 语法:

“Dear “ & firstname & “, Your team mates are “ & teammate1 & “ and “ & teammate2 & “.”

并将其保存在一个变量中:

set themessage to “Dear “ & firstname & “, Your team mates are “ & teammate1 & “ and “ & teammate2 & “.”

不幸的是,这被解释为没有变量的长字符串。

这就是我使用“运行 脚本”的原因(参见:Use string variable as Applescript command argument)。但是现在变量()没有定义。它给出脚本错误 2753:未定义变量名字。

正在使用

property name: “”

global name
set name to “”

没有解决问题。

我已经找出我在以下代码中遇到的问题:

set firstname to "Alice" as text

--Read contents of themessage.txt. and save it to variable themessage
tell application "Finder"
    set current_path to container of (path to me) as text
end tell

set txtfile to (current_path & "themessage.txt") as alias
open for access txtfile
set themessage to (read txtfile)
close access txtfile

--Convert text string to command
run script "set thecontent to" & space & themessage

和简化的themessage.txt

"Dear " & firstname & ","

为了在带有“运行 脚本”的行中识别变量 firstname,我应该更改什么?

您可以将语句放入接受参数列表的 run 处理程序中,并使用 run script 命令的 with parameters 参数将参数传递给剧本。多个参数可以放在列表中,它们像常规处理程序位置参数一样使用,例如:

set someVariable to "Alice"

set textFile to "path:to:message.txt" as alias -- the path to the text file
set theMessage to (read textFile) -- no need to use access for just reading

set theScript to "
on run {firstname} -- the variable names used in the script
   return " & theMessage & "
end run"

-- Run the script and get the result
set theResult to (run script theScript with parameters {someVariable}) -- the variables to pass to the script

有关详细信息,请参阅 AppleScript Language Guide