如何在AppleScript的字符串中调用变量?附iTerm 2脚本示例
AppleScript中在字符串里调用变量的方法
在AppleScript中,无法像Bash那样用$i或括号直接在字符串中嵌入变量,需要使用**字符串连接运算符&**来将变量与静态字符串部分拼接。
针对你提供的代码,修正后的完整版本如下:
repeat with i from 1 to 2 tell application "iTerm 2" tell current window create tab with default profile end tell tell current tab of current window set _new_session to last item of sessions end tell tell _new_session select -- 通过&拼接变量i与字符串,生成动态标签名 set name to "d" & i & "-tech" end tell end tell end repeat
关键说明:
- 用
"d" & i & "-tech"的方式,将静态字符串"d"、循环变量i、静态字符串"-tech"拼接为完整的标签名称。比如i=1时标签名为d1-tech,i=2时为d2-tech。 - 原代码存在语法缺失(缺少
end repeat和外层end tell),已一并修正以保证脚本可正常运行。
内容的提问来源于stack exchange,提问作者Pradeep Jaiswal
相关产品推荐
相关产品推荐

