3
$\begingroup$

I'm hoping to find (or assign at startup) an identifier code that corresponds to the frontend session that's running. It should behave like \$SessionID, except that it should be persistent across kernel restarts within the same frontend session, and the same across different kernels attached to the same frontend (but different between concurrently running frontends, say, running different versions of Mathematica). I've thought of extracting the identifier that lives inside of the link object returned as part 1 of \$FrontEnd, but unfortunately this identifier doesn't have the properties mentioned above (but is there maybe something else I can extract from the link object that does? ...like process start-time or something?). I've been going in circles on this for over a day and the lack of anything simple that accomplishes my goals has become maddening... any thoughts, however fully-developed or otherwise, would be greatly appreciated!

EDIT: I should add that I have tried something like:

SetOptions[\$FrontEndSession, $FrontEndSessionID -> CreateUUID[]]

but I want this ID to be assigned automatically (not having to evaluate any cells by hand on startup). When I add the line above to my FrontEnd init.m file and save, the file gets overwritten (probably by MMA itself) and removes my added line. If I can avoid init.m getting clobbered, my problem will be solved. Any ideas how to prevent this?

$\endgroup$
  • $\begingroup$ Something like s = DateList[]; SetOptions[$FrontEndSession, nbr -> s]; Print[nbr /. Options[$FrontEndSession]]; Quit[]; ? and then ... Print[nbr /. Options[$FrontEndSession]]; $\endgroup$ – Dr. belisarius Oct 23 '15 at 15:24
  • $\begingroup$ I tried this before posting, belisarius, and it worked when done by hand, but I don't know how to have the assignment made automatically. When I tried adding it to my FrontEnd init.m, the file gets clobbered and removes my changes (not sure why). Obviously, adding it to the kernel init.m is the wrong thing to do. Any idea why the frontend init.m gets overwritten? $\endgroup$ – Paco Jain Oct 23 '15 at 15:35
2
$\begingroup$

Inspired by belisarius' comment (and resurrecting my earlier attempts in that direction), I wondered if I could add the SetOptions command he suggested to the kernel init.m (see comments to original question... I had troubles when trying to add it to the front-end version of init.m). By wrapping the SetOptions in a conditional which checks for an already-defined "$FrontEndSessionID", I was able to avoid resetting the ID whenever the kernel was restarted. In short, adding the following to my kernel init.m file, solves my problem!

If[FreeQ[Options[\$FrontEndSession],"\$FrontEndSessionID"], 
    SetOptions[\$FrontEndSession, "\$FrontEndSessionID" -> CreateUUID[]] ]

Then, to access the \$FrontEndSessionID later in the session, one merely executes

Options[\$FrontEndSession]

and extracts the appropriate piece :). Alternatively, one can, as I have, define (also in init.m)

\$FrontEndSessionID= "\$FrontEndSessionID" /. Options[\$FrontEndSession]

to get something that acts like a global variable containing the FESessionID, but which persists across kernels.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.