Page 1 of 1
Console app and default variables
Posted: Thu Oct 08, 2009 6:02 am
by gragon
I have been searching the forums and the runrev dictionary, but I am unable to find an answer. The last thing I want to do is ask something that is easy to find the answer to.
I come from a Java, C/C++, Python, and PHP background.
1. Is there a way to get the username, and user default directory (not the directory the app was launched from), without having to prompt the user for the information?
2. Is there an "ask" method equivalent for console applications? I would like to prompt the user for input from the command line.
Many thanks.
Posted: Thu Oct 08, 2009 2:35 pm
by Paul D
The last thing I want to do is ask something that is easy to find the answer to.
Balderdash! The community here is great and will help anyone with the simplest of questions, I know, I ask a lot of them.
If Im understanding you correctly you want to read the OS variables that store the username and default directory? You can use $ for that. In windows, $USERNAME returns the username of the computer for example. Does that help?
Im not sure about a command line ask.
Posted: Thu Oct 08, 2009 4:02 pm
by gragon
You can use $ for that.
Thank you, I was not aware of that. Admittedly, I was hoping for something similar to other high level languages like the following Java example.
System.getProperty("user.name");
System.getProperty("user.dir");
However after testing this a bit, I have some concerns. What concerns me about using environment variables is that they can be changed at execution by the user.
For example:
export USER=arnold
I can see this raising some security flags with some types of applications.
Posted: Thu Oct 08, 2009 4:06 pm
by FourthWorld
Paul's tip is very helpful. On OS X the user name is in $user, which I believe may also be the same on Ubuntu but I haven't checked.
To get a list of all such global variables run this in the Message Box:
put the globalnames
That will list all global variable names currently available. Those preceded with "$" are system variables.
For example, $HOME on OS X is the path to the user's home directory. Lots of good stuff to be found there.
Paul's comment about asking questions was also spot-on: I ask a lot of questions for which the answer turns out to seem obvious, but if it was so obvious why was it a question? I think for every "dumb" question posted there are a hundred other people who want the answer but haven't asked it. So please ask away; it'll help a lot more folks than just you.