OSX脚本用于打开新的iTerm窗口并基于变量运行命令

时间:2015-05-28 23:18:50

标签: macos bash shell iterm osascript

我正在尝试使用显示here的iTerm示例来回答另一个查询。

基本上我有一个大约20个文本文件的列表,代表来自不同服务器的报告。我想读取它们所在目录中的每个文件名,并从中构建一个存在于命令目录中的shell命令,然后打开一个新的iTerm窗口,然后执行我构建的shell脚本。

我不想一个接一个地在一个窗口中运行它们,我希望它们各自在自己的窗口中执行以加快处理速度。

这就是我所拥有的,我可以非常愉快地构建shell脚本名称并存储在foo中,似乎我也可以打开新的iTerm窗口,但它正在接受$ foo作为要运行的命令我遇到了麻烦。

#!/bin/sh
FILES=/Volumes/reporter/uplod/lists/*
# eg each filename is of the type <path>/X07QXL29.txt
for f in $FILES
do
  foo="del"
  foo+=${f:32:1}
  foo+=${f:36:2}
  foo+=".sh"
  # foo is now for example del729.sh using the above comment as the filename
  # foo is the command I will want to run in its own new window
  osascript <<END
    tell application "iTerm"
        tell the first terminal
        tell myterm
            launch session "Default Session"
            tell the last session
                write text "$foo"
                write text "\n"
            end tell
        end tell
    end tell
  END
done

我得到的错误是:deltrash.sh:第22行:语法错误:意外的文件结束

任何人都可以给我一个指针吗?

1 个答案:

答案 0 :(得分:3)

查看iTerm applescript examples,这样的事情应该有效。基本上,您必须将myterm变量设置为新的终端实例。另外一定要将END标记放在行的开头。在您的脚本中未检测到,因此unexpected end of file错误。

#!/bin/sh
FILES=/Volumes/reporter/uplod/lists/*
# eg each filename is of the type <path>/X07QXL29.txt
for f in $FILES
do
  foo="del"
  foo+=${f:32:1}
  foo+=${f:36:2}
  foo+=".sh"
  # foo is now for example del729.sh using the above comment as the filename
  # foo is the command I will want to run in its own new window
  osascript <<END
    tell application "iTerm"
        set myterm to (make new terminal)
        tell myterm
            launch session "Default Session"
            tell the last session
                write text "$foo"
                write text "\n"
            end tell
        end tell
    end tell
END
done
相关问题