3

I am trying to use the iTerm example shown here in answer to another query.

Basically I have a list of about 20 text files representing reports from different servers. I want to read each file name in the directory they live in and from that build a shell command that exists in a commands directory, then open a new iTerm window and then execute that shell script that I built.

I don't want to run them all in one window one after the other, I want them each to execute in their own window to speed up the processing.

Here is what I have, I can build the shell script name and store in foo quite happily and it seems I can open the new iTerm window OK too, but it is getting it to accept $foo as the command to be run I am having trouble with.

#!/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

The error I am getting is:deltrash.sh: line 22: syntax error: unexpected end of file

Can anyone please give me a pointer?

Community
  • 1
  • 1
user3169632
  • 43
  • 1
  • 4
  • 1
    Are you only doing each process in a new window in order to make it run faster? If so, you could run all processes in a single window in parallel by adding an ampersand to the end of each command. Or you could run them all in parallel with `GNU Parallel` using the `-k` switch to keep the output separated and in order. – Mark Setchell May 29 '15 at 09:20
  • Yes I'd like to be able to see the output of he command in each window, so having them all display in a single window would be problematic for me. – user3169632 May 29 '15 at 13:44

1 Answers1

3

Looking at iTerm applescript examples, something like that should work. Basically you have to set myterm variable to new terminal instance. Also be sure to put END marker at the beginning of line. It is not detected in your script, hence unexpected end of file error.

#!/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
baf
  • 4,531
  • 1
  • 21
  • 24