0

I'm attempting to write some Dashcode to but can't seem to get the environment variables when I run the /env command. The environment doesn't appear to be sourced because it always returns "Undefined". Below is my code and I'm open for any suggestions (I need more than just LANG, LANG is just the example).

var textFieldToChange = document.getElementById("LangField"); var newFieldText = widget.system("/usr/bin/env | grep LANG").outputString; textFieldToChange.value = newFieldText;

Is there an easy way to source my environment and cache it in Dashcode or do I need to attempt to write something that will cache the entire environment somehow?

Thanks for any ideas!

Rick
  • 1

2 Answers2

0

I know this thread is quite aged, but anyway, the question is still up to date :-)

Just having started with Dashcode and widgets myself, I did a quick hack on this:

function doGetEnv(event)
{
    if (window.widget)
    {
        var out = widget.system("/bin/bash -c set", null).outputString;
        document.getElementById("content").innerText = out;
    }
}

For my experimental widget, I did use a scroll area and a button. The doGetEnv(event) is fired upon onclick, set via inspector. The Id "content" is the standard naming of the content within the scroll area.

The out var containes a string with '\n' charaters, to transform it into an array use split().

function doGetEnv(event)
{
    if (window.widget)
    {
        var out = widget.system("/bin/bash -c set", null).outputString;
        out = out.split("\n");
        document.getElementById("content").innerText = out[0];
    }
}

The first entry is "BASH..." in my case. If you search for a particular item, use STRING's match method (see also http://www.w3schools.com/jsref/jsref_match.asp) along with the following pages on regular expressions:

To cache the environment, you can use:

var envCache = "";

function cacheENV()
{
    envCache = widget.system("/bin/bash -c set", null).outputString;
    envCache = envCache.split("\n");
}

This will leave an array in envCache. Alternative:

function cacheENV()
{
    var envCache = widget.system("/bin/bash -c set", null).outputString;
    envCache = envCache.split("\n");
    return envCache;
}
Living Skull
  • 126
  • 6
0

Have you allowed Command Line Access? Go to Widget Attributes (in the left hand menu) , then extensions and check Allow Command Line Access else the widget is prevented from talking to the system. Not sure if this is what is causing the problem though.

PurplePilot
  • 6,652
  • 7
  • 36
  • 42
  • Thanks for the idea, but yes I have allowed command line access. I can actually use some of the commands, but anything that requires knowledge of environment variables doesn't work. – Rick May 31 '11 at 12:16