Skip navigation

Automate terminal command

1660 Views 7 Replies Latest reply: May 11, 2012 10:19 PM by Camelot RSS
My Last Confession Level 1 Level 1 (60 points)
Currently Being Moderated
May 8, 2012 8:59 PM

I figured out how to run multiple copies of skype by launching the .app file from different osx user accounts in terminal.  What I'm trying to do now is script this or automate opening terminal and typing the line with automator.  The following is the line that I use.

 

su user2 -c "/Applications/Skype.app/Contents/MacOS/Skype && exit"

 

And I just change user2 to user3 for a third instance of skype and so on.  Can someone show me how ot automate this process?  Thanks.? Thanks.

iMac, Mac OS X (10.7), 27" / i5 / 8GB RAM
  • Camelot Level 8 Level 8 (45,680 points)
    Currently Being Moderated
    May 8, 2012 10:19 PM (in response to My Last Confession)

    set theUsers to {"joe", "bob", "jane"}

     

    repeat with eachUser in theUsers

              tell application "Terminal"

                        do script "su " & eachUser & " -c /Applications/Skype.app/Contents/MacOS/Skype && exit"

              end tell

    end repeat

  • X423424X Level 6 Level 6 (14,190 points)
    Currently Being Moderated
    May 8, 2012 10:32 PM (in response to My Last Confession)

    (post deleted)

  • BobHarris Level 6 Level 6 (12,545 points)
    Currently Being Moderated
    May 9, 2012 5:20 AM (in response to Camelot)

    Camelot, isn't he going to need run with admin privs added to the do script statement?  Generally speaking the 'su' command needs to be root in order to switch users.

     

    My Last Confession, you can put the Camelot's script into an Automator app selecting the "Run Shell Script" action, and then saving it as an app.   You could also use Applications -> Utilities -> Applescript Editor to create an Applescript app.

  • Camelot Level 8 Level 8 (45,680 points)
    Currently Being Moderated
    May 9, 2012 9:04 AM (in response to BobHarris)

    Camelot, isn't he going to need run with admin privs added to the do script statement? 

    No. Terminal.app's do script doesn't need any special permissions, per se. It does little more than typing the command in a terminal window and pressing return. Whether or not the user has permissions to run the command is a completely different issue

     

    In this case, yes, there's going to be a prompt to enter a password because you're switching user account.

     

    The only way to avoid the password prompt is to use sudo along with changes to /etc/sudoers to allow this command to run without a password.


    For example, adding this to /etc/sudoers:

    %users   ALL=/Applications/Skype.app/Contents/MacOS/Skype

    will let any use launch skype under sudo without a password, so you can then edit your shell command to:

     

                        do script "su -u " & eachUser & " /Applications/Skype.app/Contents/MacOS/Skype"


  • Camelot Level 8 Level 8 (45,680 points)
    Currently Being Moderated
    May 11, 2012 10:19 PM (in response to My Last Confession)

    i want to keep the password though,  I'm thinking about just using keystroke to have it put the password in each time.  something like:

     

    This is doable, but is also an incredibly bad idea. You see, for the most part, keystroke is blind - it doesn't care (or know) where it's typing and you'd be blindly typing each user's password with no idea of where it's going. Is it going to Terminal.app? maybe, maybe not. Even if it is there's no guarantee that it's typing in the right window, or that the window is even expecting a password.

    It's also entirely reasonable (probable, even) that you're not in Terminal.app. Nothing in the script activates Terminal.app, so you could be in another application at the time, or the user could have switched applications while the script is running - the script will continue to launch Terminal.app windows and scripts in the background while keystroke happily types the password into whatever frontmost app is around.

     

    OK, this is over-simplified, because it is possible to target an application to receive the keystrokes, but my aim is to illustrate the point.

     

    In either case, I wouldn't use keystroke at all. It would be better to use a second do script command - remember, do script does little more than type the text into a terminal window.... the difference is that at least you know you're in Terminal.app. The trick is getting to know where you're typing. The trick here is that do script will return a reference to the window/tab where the command was executed, so you can capture this after your first command (to launch Skype) and use that as the target for subsequent commands/typing in the same window, e.g. (untested):

     

    set theUsers to {"user2", "user3", "user8", "user11", "user4", "user5", "user6", "user7", "user9"}

    set thePWs to {"aaaa", "bbbb", "cccc", "dddd", "eeee", "ffff", "gggg", "hhhh", "iiii"}

     

    repeat with i from 1 to count theUsers

              set eachUser to item i of theUsers

              set eachPW to item i of thePWs

     

              tell application "Terminal"

                        set myWindow to do script "su " & eachUser & " -c /Applications/Skype.app/Contents/MacOS/Skype"

      delay 1 -- optional - allow time for Terminal.app to do its thing

      do script eachPW in myWindow

                        do script "exit" in myWindow

              end tell

    end repeat


    In thie way, do script has the command to launch skype and passes back the window/tab reference. After a short delay the password is typed in that window/tab, followed by the 'exit' to close the window.

     

    Incidentally, the use of sudo, as suggested above, obviates the need to type each user's password in a shell and avoids this issue altogether.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.