Search code examples
textinsertapplescriptquotes

Trying to insert text in between quotes on a telnet script


Sorry new to coding Applescript so any help is appreciated.

I'm trying to create a script that pastes text from clipboard into the middle of a telnet command. The output needs to be in the same window and look kind of like this:

I8,A,001
Q102,024
q448
rN
S4
D15
ZT
JF
O
R71,0
f100
N
B264,65,2,UA0,2,4,56,B,"100000000045"
A203,82,2,1,2,2,N,"xxxxx"
P1

The quoted 12 digit number on line 13 is what I need to insert.

This is what I've coded so far but it's not working:

tell application "Terminal"

    do script "telnet xxx.xxx.xx.xx xxxx"
    delay 1
    do script "I8,A,001" in window 1
    do script "Q102,024" in window 1
    do script "q448" in window 1
    do script "rN" in window 1
    do script "S4" in window 1
    do script "D15" in window 1
    do script "ZT" in window 1
    do script "JF" in window 1
    do script "O" in window 1
    do script "R71,0" in window 1
    do script "f100" in window 1
    do script "N" in window 1
    do script "B264,65,2,UA0,2,4,56,B,\""
    tell application "System Events"
        tell application process "Terminal" in window 1
            keystroke "v" using {command down}
        end tell
        keystroke "\""
        keystroke return
        do script "\"A203,82,2,1,2,2,N,\"xxxxx\""
        do script "P1"
        keystroke return
    end tell
end tell

As soon as I try to use Command V to paste it exits the Terminal window and pastes whats on the clipboard on the script instead and it wont let me tell it to stay in Terminal window 1.


Solution

  • You do not need to use command-v to get the clipboard contents into your Terminal window. Applescript can get the clipboard and then you just add it to the other part of your string before "do script". Something like this works... of course you don't need the first line of the code because the clipboard should already have that value.

    set the clipboard to "100000000045"
    
    set t1 to "B264,65,2,UA0,2,4,56,B,\""
    set t2 to the clipboard
    set t to t1 & t2 & "\""
    do script t in window 1