Applescript + Bash:脚本在Applescript编辑器中工作,但不使用osascript

问题描述:

我有这个运行一些Applescript的bash函数。如果我运行的AppleScript的编辑器或TextMate中的AppleScript的一部分,它工作正常,但在命令行上,则操作失败...Applescript + Bash:脚本在Applescript编辑器中工作,但不使用osascript

wtf() { 
    osascript - <<EOF 
    tell application "iTerm" 
     tell current terminal 
      launch session "Railscasts" 
      tell the last session 
       write text 'echo -ne "\\e]1;$account\\a"' 
      end tell 
     end tell 
    end tell 
EOF 
} 

和错误是:

190:191: syntax error: Expected expression but found unknown token. (-2741) 

我知道(认为)这个问题是在这条线的第一bash的转义序列:

write text 'echo -ne "\\e]1;$account\\a"' 
        ^

但我不知道它为什么失败?为什么这不工作,请任何想法?

编辑1:我也试过这和它的失败:

wtf() { 
    osascript - <<EOF 
    tell application "iTerm" 
     tell current terminal 
      launch session "Railscasts" 
      tell the last session 
       write text "echo -ne \\\"\\e]1;$account\\a\\\"" 
      end tell 
     end tell 
    end tell 
EOF 
} 

错误消息:

163:164: syntax error: Expected end of line but found unknown token. (-2741) 

的AppleScript不支持'…'为一个字符串。您必须使用"…",并用\转义内部对"…"

此外,它看起来像炮弹减少here文档反斜杠序列(!),所以你需要添加在现有的反斜杠反斜杠更多:

"echo -ne \"\\\\e]1;$account\\\\a\"" 
+0

我想你这么lution,它失败了一个不同的错误信息...请参阅我的编辑。 –

+0

我想''echo -ne \“\\ e] 1; $ account \\ a \”“'是你想要的。 – chepner

+0

@chepner我也试过了,但我又遇到了另外一个错误:'163:164:语法错误:期望的“”“但是找到未知的标记(-2741)' –

你也可以用<<EOF替换<<'EOF'

<<'END' 
\$` are not interpreted 
END 

<<END 
\$` are interpreted 
END 

或者只是使用单引号:

osascript -e 'tell application "iTerm" 
    -- 
end tell' 

X=999 osascript -e 'on run argv 
    item 1 of argv + system attribute "X" 
end run' 888 
+0

第二个例子末尾的'888'有什么意义? –

+0

我只是使用888和999作为示例,说明如何通过添加显式运行处理程序或使用环境变量将值传递给脚本 – user495470

+0

哦,我明白了,当脚本运行时,'argv'将是'{888}'(或也许是'{“888”}')。 –