Terminal script

I'd like to write a script that will run when I log into OS X, which will do the following:

1) Launch Terminal
2) Immediately close the window that Terminal creates when it opens.
3) Preferably, keep Terminal hidden while all this is happening, so all I see is the Terminal icon bouncing in the dock as the program launches.

Any advice appreciated :slight_smile:

PS: I'd prefer the script to be a .sh or a .c file (to compile), as those are the only two languages I have some experience in at the moment. Thanks.

Why not to do this when you enter runlevel 5?

Sorry, that's way beyond my experience right now. I don't even properly understand what you mean! Can you explain?

A runlevel is a software configuration of the system which allows only a selected group of processes to exist. The processes spawned by init process for each of these runlevels are defined in the /etc/inittab file. Runlevels 0, 1, and 6 are reserved. Other runlevels are dependent on how your particular distribution has defined them, and they vary significantly between distributions.

Most users run X from runlevels 3 or 5 (for linux?). Runlevel 3 places your system in multi-user mode with full networking capabilities. The machine will boot to a text-based login prompt with all necessary preconfigured services started. Most servers are run in runlevel 3, as X is not necessary to provide any services utilized by most users. Runlevel 5 is similar to 3, except that it automatically starts X and provides a graphical login screen. Many workstation users prefer this method, because it never forces them to see a command prompt.

You can add commands that must be executed in appropriate runlevel in some file/under some directory (e.g. /etc/rc.d/rc5.d)

Here is what happens when OS X boots:

http://www.kernelthread.com/mac/osx/arch_startup.html

It would be helpful to know why this operation is important because there may be a better way to accomplish the "why" than you might realize.
I can't image a use for having Terminal launch at startup without a window. Not that there isn't one, but I just can't imagine it.

AppleScript can be used to hide a window, but the application first has to create the window (which displays on creation), then hide it immediately. The window is thus only visible for a split second.

You can send applescript code from shell scripts using the osascript command (man osascript).

You can examine Terminal's applescript functionality from within Script Editor.app by selecting "Open Dictionary..." from the Script Editor "File" menu, then navigating to Terminal.app.

Otherwise, I don't know if this is possible from the command line.
:frowning:

Maybe a login hook could do the trick?

sudo defaults write com.apple.loginwindow LoginHook /path/to/script

(For more information see http://www.bombich.com/mactips/loginhooks.html and http://docs.info.apple.com/article.html?artnum=301446 )

Using AppleScript you could try something like this:
osascript -e 'tell application "Terminal" to launch' -e 'tell application "System Events" to set visible of some item of ( get processes whose name = "Terminal" ) to false'

Btw, you can 'reset' Terminal.app by deleting the Terminal.plist file:
rm "$HOME/Library/Preferences/com.apple.Terminal.plist"

Could you not simply add Terminal to your Accounts login items, along with an AppleScript:

tell application "Terminal"
activate
close every window
end tell

and add to Terminal's Info.plist:

LSUIElement String 1 <- This tells Finder to hide the application's Dock icon.

I'm unsure of what order the login items launch however.

I dont have a mac infront of me so I cant test but..

#!/bin/bash
open /Applications/Terminal.app &

maybe that will work, I dont know.. you can save it as a .sh file, then run chmod +x on it (I'm not sure that is even necessary, but shouldnt hurt).. then you can add it as a launchd item which I am not able to explain right now.. hopefully this info will help somewhat.

OR... just go to system prefs/accounts and add Terminal.app to your login items and check the hide box.. probably the easiest way.

Look into launchd and making a launchd item that runs when your user account logs in, all you have to do is place the script where launchd looks in that user's home folder and it will execute all scripts upon log in.

I am not sure how to have it close the window from the shell, but an apple script maybe able to do that.

black-eyed-dog,

One way to get the same effect without using a script is to add Terminal to your account's login items, as Ravelnze said, but specify that you want Terminal to run hidden.

Next time you log in, Terminal will start running, but no Terminal window will be visible. You even get the Terminal icon bouncing in the dock that you mentioned. When you click on the icon a window will appear.

If you are using Leopard, these are the steps -

  • Log in as the user you want to change.

  • Go to System Preferences. In the "System" section, select "Accounts".

  • At the top of the right-hand side of the window, select "Login Items".

  • Click the "+" sign near the bottom of the window to add an application.

  • In the window that appears, navigate to Applications>Utilities and click on Terminal.app.

  • Click the Add button.

  • Terminal will appear in the list of applications. Click the check box in the "Hide" column.

Regards,
Davf