Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The tricky part of trying to do this is the fact that the user could have their shell set to anything: sh, bash, csh, tcsh, and so on, and each shell sets up its terminal environment differently. I'm not sure if I'd go to the trouble for this myself, but if you <em>really</em> want to, here's the route I would take.</p> <p>The first step is to figure out the user's shell. On OS X, this information is stored in Directory Services, which can be accesed either through the APIs in DirectoryService.framework or by using the <code>dscl</code> command line tool. The DirectoryService API is a royal pain in the ass, so I would probably go the CLI route. In Cocoa, you can use NSTask to execute the tool with arguments to get the user's shell (I'll leave the details of this for elsewhere). The command would look something like:</p> <p><code>dscl -plist localhost -read /Local/Default/Users/username UserShell</code></p> <p>This will return XML text that you can interpret as a plist and transform into an NSDictionary, or you can omit the <code>-plist</code> option and parse the textual output yourself.</p> <p>Once you know the path to the user's shell, the next step would be to execute that shell and tell it to run the <code>env</code> command to print out the user's environment. It looks like most shells accept a <code>-c</code> command line option that lets you pass in a string to execute - I guess you'll just have to assume that as being the common interface for whatever shell the user has chosen.</p> <p>Once you have the user's environment, you can then grab their list of paths out of that, and do the search for whatever executable you're looking for from that. Like I said, I really don't know whether this is worth the trouble, but that's the direction I would go if I were implementing this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload