Applescript in MacOS is munging my data

Discussion in 'Mac Programming' started by techwarrior, Jan 10, 2017.

  1. techwarrior, Jan 10, 2017
    Last edited: Jan 10, 2017

    techwarrior macrumors 6502

    techwarrior

    Joined:
    Jul 30, 2009
    Location:
    Colorado
    #1
    I wrote scripts to prompt for SSH connections and it reads my .ssh/config file for known hosts, then runs a shell script to ssh <hostname>. I store known hosts in the config file to abbreviate the command, and store\retrieve the password from keychain. Since updating to Sierra, the script often transposes characters and fails to make the connection. Example:

    From ~/.ssh/config : Host AA7MainCM
    Script output: ssh AA7MAinCM or ssh AA7MAINCM
    ssh: Could not resolve hostname aa7maincm: nodename nor servname provided, or not known

    My config file contains a variety of hosts with different login routines. So, I append a #Descriptor in the config file to each host, and grep\awk the data and redirect it to a temporary file to read for selection in the Dialog prompt based on the type of server I am connecting to. This also lets me use different login script steps based on the prompts from various classes of servers.

    One sub reads the temp file into memory to display the list (appears correct in the dialog), and I highlight the one I wish to connect to which reads the string into a variable "myConnection" and proceeds to another sub which runs an execCmd("ssh " & myConnection) in terminal. I also use the myConnection variable to update the titlebar in Terminal which appears correct even when the script munges the execCmd output.

    A snippet of the portion of code:

    -- Run the SSH Login Script
    my execCmd("ssh " & myConnection, 1) # This is the line that munges the variable output
    set custom title of window 1 to myConnection # This line always displays the correct title
    try
    if conditional = true then
    ...

    Again, prior to Sierra, this worked flawlessly, it now works intermittently. I am wondering if something might have changed in Sierra, or if there is another method of sending a variable to terminal that would not behave erratically.

    Here is the execCmd routine I use:

    on execCmd(cmd, wait)
    tell application "System Events"
    tell application process "Terminal"
    set frontmost to true
    keystroke cmd
    keystroke return
    end tell
    end tell
    delay wait
    end execCmd
     
  2. neutrino23 macrumors 68000

    Joined:
    Feb 14, 2003
    Location:
    SF Bay area
    #2
    Sorry, I can't follow your script. I'm not familiar with this topic (SSH). Let me give you a hint that may help.

    I have a script that processes a number of text files. It stopped working with Yosemite. After a bit of sleuthing I found that it was because AppleScript had become much faster.

    It seems that Finder commands are not synchronized. You can launch a "tell Finder ..." and you just hope the Finder completes this by the time you look for a result.

    I put in a delay of one second (sorry, I can't recall exactly where) and things worked again. In my case it had something to do with opening and closing files. I just needed to change a short string in about a hundred files. As I recall, it worked for the first file or two or three then went off the rails.

    You might put in some break points to see what is going on. Good luck.

    AppleScript is now amazingly fast. I started using it in the 90s when it was painfully slow.
     
  3. techwarrior thread starter macrumors 6502

    techwarrior

    Joined:
    Jul 30, 2009
    Location:
    Colorado
    #3
    Thanks, will try that.
     

Share This Page