running two applescripts together

Discussion in 'Mac Programming' started by hehe299792458, Dec 2, 2015.

  1. hehe299792458 macrumors 6502a

    hehe299792458

    Joined:
    Dec 13, 2008
    #1
    I'm trying to run these two scripts in one file in AppleScript:

    Code:
    global UserName
    global ServerName
    global passwd
    
    on ActivateX11()
        tell application "Terminal"
            activate
        end tell
    end ActivateX11
    
    on ActivateTerminal()
        set ScriptCommand to "ssh -X -N -D 8080 " & UserName & "@" & ServerName
        tell application "Terminal"
            activate
            do script ScriptCommand --  Establish the SSH connection
            delay 3 -- Wait 3 seconds for the password prompt to appear
            do script passwd in window 1
        end tell
    end ActivateTerminal
    
    on run
        set ServerName to "xxx" --  The name of the server to connect to
        set UserName to "yyy" --  The user to connect as
        set passwd to "zzz" --  The password for the user
        ActivateX11()
        ActivateTerminal()
    end run
    
    
    tell application "Finder"
        activate
    end tell
    
    tell application "System Events"
        if UI elements enabled then
            click menu item "SSH" of menu "Location" of menu item "Location" of menu "Apple" of menu bar 1 of process "Finder"
        else
            tell application "System Preferences"
                activate
                set current pane to pane "com.apple.preference.universalaccess"
                display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
            end tell
        end if
    end tell
    


    But I get the following error message:

    Code:
    The run handler is specified more than once, or there were top-level commands in addition to the run handler.

    What can I do to fix this?
     
  2. hehe299792458 thread starter macrumors 6502a

    hehe299792458

    Joined:
    Dec 13, 2008
    #3
  3. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Yep, you're right. The run handlers are closed. It's the second part of the error message you need to be looking at:

    Code:
    ...or there were top-level commands in addition to the run handler
    
    I expect that all the stuff after the "end run" line probably belongs one of your handlers. Or perhaps in a handler of its own, depending on how you expect your script to behave.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    Did you try leaving out the explicit run handler?
     

Share This Page