PDA

View Full Version : Get user id



nickdigital
01-18-2013, 06:55 PM
Does lscript have something to return the login id of the user? I didn't find anything in the docs.

sami
01-19-2013, 05:48 AM
I think there's only licenseid() to get the dongle #.

However, this (even though not elegant and requires a dual-platform check) works, but requires 3 files (it writes out the user to a file and then reads it in):

getLoginName.ls


@version 2.6
@asyncspawn
@warnings

generic
{
var whatCommand;
var whatID;

var pathParts = split(SCRIPTID);
var dirScriptRunFrom = string(pathParts[1], pathParts[2]);

var whatPlatform;

whatPlatform = platform();

if (whatPlatform == INTEL)
{
whatCommand = dirScriptRunFrom + "getCurrentUser.bat";
}
else // assume Mac
{
whatCommand = dirScriptRunFrom + "getCurrentUser.applescript";
}
// run batch file to get the user name
whatID = spawn(whatCommand);
// wait for file to be written
sleep(1000);
var filCurrentUser;
var userName;

filCurrentUser = File("CurrentUserName.txt", "r");

userName = filCurrentUser.read();

info("User = " + userName);
}


getCurrentUser.bat


echo %USERNAME% > CurrentUserName.txt


getCurrentUser.applescript


do shell script "echo system attribute \"USER\" > CurrentUserName.txt"



There is probably a better way to do this, but this works off the top of my head. Also I haven't tested the applescript part but I think it will work. The windows version here does work though.

sami
01-19-2013, 06:01 AM
also, not sure if you are intending to use this for copy-protection or for some kind of writing to the user's folder. If it is for copy protection, the environment variable %USERNAME% can be spoofed so it's not totally secure. Not sure about the apple system attribute though, I'm not an expert in OSX...

iain_r
01-19-2013, 06:25 AM
Hi, If you are on windows you don't need any external script

@version 2.2
@warnings
@name Username
@script modeler
main
{

username = getenv("USERNAME");
info("name: ", username);
}

Will give the username.

Don't knpw about apple but getenv is in the reference guide. For a list of environment variables in windows open a cmd window and type 'set', it will show the variables and their values.

Regards

Iain

sami
01-19-2013, 06:55 AM
thanks. Had a feeling I did it the hard way. Didn't know about getenv. Hopefully getenv gets system attributes on OSX? I'll have to test some time...

iain_r
01-19-2013, 07:23 AM
No problem, getenv is a standard C function but I've no idea if everything in lscript works on all platforms.

nickdigital
01-19-2013, 09:30 AM
Thanks iain_r and sami.

sami
01-19-2013, 10:08 AM
Thanks iain_r and sami.

let me know if you test it on mac and if you need the external applescript to get the variable or if getenv works on OSX - I dont have LW installed on my Mac partition currently...

nickdigital
01-19-2013, 12:06 PM
I don't use a mac so not gonna be helpful on that front.

Phil
01-22-2013, 02:09 PM
Just as a note of caution, in the above code pathParts[1] will be 'nil' under OS X. That leads to all kinds of problems if you try to concatenate strings. You have to code defensively to avoid illegal arithmetic errors.

sami
01-22-2013, 07:50 PM
Just as a note of caution, in the above code pathParts[1] will be 'nil' under OS X. That leads to all kinds of problems if you try to concatenate strings. You have to code defensively to avoid illegal arithmetic errors.

thanks, I just slapped that together without testing on mac, but yes I should code with nil checks as yup there is no C: drive on the mac, thx for reminding me. i try to make crossplatformable lscripts when I can... even tho I dont use LW on the Mac since in the old days it used to be very buggy and also there are so few plugins in comparison...