invoking programs from command line

travelgz

Registered
Hi,

How can I invoke a program like Internet Explorer from the command line? On Linux, you can, for example, just type netscape & in your terminal. Well, my reason for asking this is that forte wants to invoke netscape when I run a jsp. I don't have netscape and not in the mood to install the beta version of netscape for OS X. Since Forte is calling a function similiar to the way I invoke netscape from linux from command line, I am wondering if that's possible for IE. If it is possible, how do I find out what that command is? I tried IE and InternetExplorer, neither worked. When I tried to call the full path to Internet Explorer (from the Application dir) I get a permission denied error.

So, as a non-root user, how do I call Internet Explorer from command line?

Thanks!
G
 
You can use a command like

open /Applications/Internet\ Explorer

This is an exceptionally nice command. You can even use

open .

to have the Finder open the enclosing folder of wherever you're currently cd'ed to.
 
Good. I tried what you did and IE is opened as if I double clicked on the icon. The trouble is that I need to open a URL using IE. Doing the netscape way didnt work:
open /Application/IEPATH http://www.apple.com

It just ignores the path. Doing simple: open http://www.apple.com results in a file not found error.

Any clue or should I switch to buggy Netscape?

Thanks in advance!
 
Okay, at this point, I'd suggest doing it with a one-line AppleScript being run from the command-line. Most GUI Mac applications don't really allow for parameters to be passed to them, but do support AppleScript. The only real problem with this is that currently, I'm only getting this to work with Mozilla (and therefore Netscape). Whenever I try IE, it just uses the Classic version. Someone else could probably see a way around this. But here is what you can do for Mozilla/Netscape:

/usr/bin/osascript 'tell application "Mozilla" to GetURL "www.apple.com"'

You'll need the full path to osascript, because otherwise it probably won't run.
 
So I guess I will need to learn another language, AppleScript? Sad but tough luck. Any good resources where I can poke around?

Thanks!
G
 
Like I said, there may be other possibilities, but coming from the world of OS 9, the only way I can think of is with AppleScript. You can read up on it at http://www.apple.com/applescript/. Don't worry about difficulty, though. Scientific studies have shown that even hamsters can learn to program in AppleScript. Granted, they were really smart hamsters, but I think you get the idea.
 
I just thought I ought to mention that things are slightly different with OS X.I. You now don't need to include the /usr/bin part, and in order to do a one line script, you need a -e. So to do the same thing as before, just say

osascript -e 'tell application "Mozilla" to GetURL "www.apple.com"'

Internet Explorer seems to be a little trickier, for me at least. When I try using osascript on it and IE isn't already open, it tries to open the Classic version, for some reason. Fortunately, you can just do

open /Applications/Internet\ Explorer.app/ ; osascript -e 'tell application "Internet Explorer" to OpenURL "www.apple.com"' > /dev/null

Notice that IE uses OpenURL, instead of GetURL. In addition, using the "open" command on a program that is already open seems to not give an error anymore. Lastly, using osascript keeps returning "-2" when I run it, and hence the >/dev/null, because it gets annoying after a very short while.

This may seem like a lot to do for a very simple command, but there is a lot more you can do without saying much else. Plus, if you use Mozilla, Netscape, or OmniWeb, you don't need to do as much (and interestingly OmniWeb, in its quest for complete and utter compatibility, accepts both OpenURL and GetURL.)
 
It seems to work. But using an argument would not work because $1 is translated literally:

open /Applications/Internet\ Explorer.app/ ; osascript -e 'tell application "Internet Explorer" to OpenURL "$1"' > /dev/null

This is placed in a file ie.sh, and I wanted to do something like:

./ie.sh www.macosx.com

swapping the single quotes with the double quotes caused some mysterious syntax error. Is there anything in applescript that wll allow me to insert variables?

Thanks,
G
 
Another cool feature is the ability to do this to other networked machines...
If I have 2 networked macs... and have enabled remote logins, i can ssh onto one and open applications ON THEIR COMPUTER.

Pretty cool if you ask me :)
 
Well, you are saying that if you type open blah on someone's mac, you will have that "blah" opened on that someone's mac? How can that be? What if that mac has no one logged in? Or not the same person as you? Or do you mean like X Windows where you borrow someone else's resources to invoke "blah" to show up on your mac though invoked from their mac? But ssh wouldn't let you do that anyway.

Weird. I can't try since I have no one else's account on a mac :)

G
 
I finally got this shell script to work:

#!/bin/sh

`open "/Applications/Internet Explorer.app/"`
the_command="tell application \"Internet Explorer\" to OpenURL \"$1\""
`osascript -e "$the_command"`


If you take a look at man osascript, it appears that because of the fact that AppleScripts require proper quoting, *'s, ('s, and )'s, things can get a little hairy when it comes to running one line scripts. And unfortunately, there's currently a bug in osascript that prevents passing along parameters to scripts.

And in general, no, the open command cannot open up another GUI application on someone else. For the open command to work, two things must be true:

*Someone must be physically logged in
*The open command has to be invoked by the user logged in, or root (I love typing sudo open /Applications/Text\ Edit. It makes it much easier to edit my httpd.conf, and I don't have to log out and in to do it.)

Mac OS 10.1 does include what's called Program Linking in OS 9. Now they're just called AppleEvents. Basically, you can create an AppleScript that interacts with another Mac. I used to delight in having one computer tell another to eject a CD (they were simpler times back then), although the things one can do with this are very powerful (and dangerous). Having not tried it in OS X, I don't know how this works, however.
 
Wow, that script worked. I used it in my forte as the script to call for viewing jsp and html files, and it worked too.

Such smarties on this forum :)

Thanks!
G
PS: wonder if apple script can open a new IE window :) OK, asking for too much now.
 
Sure, this is easy. Just use something like this:

#!/bin/sh

`open "/Applications/Internet Explorer.app/"`
the_url=$1
option_n="0"
the_command="tell application \"Internet Explorer\" to OpenURL \"$the_url\" toWindow $option_n"
`osascript -e "$the_command"`


Setting $option_n to -1 would ensure that it opened the URL in the frontmost window. This should be able to be easily modified so that one could be able to pass on a -n parameter, in order to indicate a new window. Unfortunately, I'm not very good at shell scripts yet, so I don't know how to do this.

You can find the syntax for particular programs by opening /Applications/AppleScript/Script Editor and picking "Open Dictionary...". It's actually quite amazing what you can do with AppleScripts.
 
Back
Top