I need to run an app at startup/login on my mac. I want it to launch in the background and start doing it's work without interrupting me or me having to start it up because I invariably forget and then when I need it, it wasn't running!

I have tried using AppleScript to tell Terminal to run it and type my password in, but it ends up opening multiple Terminal windows and not working. Ideally I need a script that I can just add to the user login items and it will run for me.

The app has no way of taking a password argument either and it has a password as well as the sudo! I need a solution that can either be done as an applescript (which can be made into an executable) or i need a commandline script but I have no idea about them.

This is the manual code I type

$ sudo serverStatus

My AppleScript:

tell application Terminal
 do shell script "sudo serverStatus"
 delay 5
 do shell script "123456"
 delay 2
 do shell script "serverpass"
end tell
  • Is that app a custom-built one? What exactly is it? What AppleScript commands did you try? – slhck Jun 18 '12 at 8:46
  • It's a custom app built a while ago by a friend so I can do some server monitoring & logging but he is too busy to update it with the features I need (it was a freebie favor). I have updated my question with my applescript code I tried. It opened 3 windows and didn't seem to work! – Designer023 Jun 18 '12 at 9:03

do shell script is used to execute shell scripts within an AppleScript and won't actually open a Terminal window. You could open a new Terminal window with do script and then emulate typing the passwords with keystroke.

tell application "Terminal"
    do script "sudo serverStatus"
end tell
delay 1
tell application "System Events"
    keystroke "login password" & return
    delay 1
    keystroke "server password" & return
end tell

If you're up for it, you could also just use a login hook which would be run after logging into your user account. This hook could call the appropriate AppleScript with:

osascript /path/to/script.scpt

But this seems to be a little too much if you could just add it to the login items manually.

  • Thanks @Lauri – I wasn't on a machine where I could try this. – slhck Jun 18 '12 at 15:50
  • 1
    For those that found this wanting to open programs with spaces in the name, remember that it would be two backslashes.... example `do shell script "sudo /Applications/Aptana\\ Studio \\3/AptanaStudio.app/Contents/MacOS/AptanaStudio" – Senica Gonzalez Aug 4 '12 at 15:21
  • Just curious, why would Aptana need sudo? @sen – slhck Aug 4 '12 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.