srudland

Q: Applescript Variable Error

Hi all,

 

I am trying to write an applescript that is called with a variable and then uses that variable in the script. For example:

 

on run argv 
  set a to item 1 of argv 
  tell application "iTerm-2" 
    tell current window 
      tell the current session 
      write text "ssh" & a 
      end tell 
    end tell 
  end tell 
end run

 

So the app would be called on the command line:  open mypp.app --args machine. The app iTerm-2 would open and run the command "ssh machine". At the moment it launches iTerm-2 and runs the ssh command but ignores the variable. Am I called the variable of "a" incorrectly?

 

Thanks,
Sam

Posted on Jul 21, 2016 3:14 AM

Close

Q: Applescript Variable Error

  • All replies
  • Helpful answers

  • by VikingOSX,Helpful

    VikingOSX VikingOSX Jul 21, 2016 6:41 AM in response to srudland
    Level 7 (20,606 points)
    Mac OS X
    Jul 21, 2016 6:41 AM in response to srudland

    There is something fishy going on with the open command when used with --args and AppleScript. I suggest that you run your app as follows to capture item 1 of argv:

     

    osascript mypp.app machine

  • by srudland,

    srudland srudland Jul 21, 2016 6:47 AM in response to VikingOSX
    Level 1 (4 points)
    Mac OS X
    Jul 21, 2016 6:47 AM in response to VikingOSX

    OK - so that worked! However I am a bit up the creek now. I have a software solution that when I am logged into  an define a "launcher" which fires up a Terminal Emulator on my Mac. I am pointing this software to my script which as you can see fires up the emulator and gives it a command to run. This software also hands over an ID and password as an argument which can be added to the command to run. Unfortunately it looks like it uses "open -a" and I can't change that. Should open -args be working or is this a problem to be fixed in the future?

  • by VikingOSX,Solvedanswer

    VikingOSX VikingOSX Jul 21, 2016 9:24 AM in response to srudland
    Level 7 (20,606 points)
    Mac OS X
    Jul 21, 2016 9:24 AM in response to srudland

    This also works to get machine into your script. It will still work without the on run argv block. Use quotes for arguments containing white-space. You can adapt this to parse multiple arguments of different data types from a single command line.

     

    -- reference: http://unixjunkie.blogspot.com/2006/07/command-line-processing-in-cocoa.html

    -- reference: http://perspx.com/archives/parsing-command-line-arguments-nsuserdefaults/

    -- Terminal invocation: open mypp.app --args -stringArg machine

     

    use scripting additions

    use framework "Foundation"

     

    on run argv

           set argx to current application's NSUserDefaults's standardUserDefaults()

           set sarg to (argx's stringForKey:"stringArg") as text

           if sarg is not "" then

                display dialog sarg

           end if

    end run

  • by srudland,

    srudland srudland Jul 21, 2016 9:25 AM in response to VikingOSX
    Level 1 (4 points)
    Mac OS X
    Jul 21, 2016 9:25 AM in response to VikingOSX

    This is awesome! I am 60% of the way there now! I'll have a poke around the last stuff tonight. Thank you a hundred times over