0

我正在尝试编写一些 Dashcode,但在运行 /env 命令时似乎无法获取环境变量。该环境似乎没有来源,因为它总是返回“未定义”。下面是我的代码,我愿意接受任何建议(我需要的不仅仅是 LANG,LANG 只是示例)。

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

有没有一种简单的方法来获取我的环境并将其缓存在 Dashcode 中,或者我是否需要尝试编写一些能够以某种方式缓存整个环境的东西?

感谢您的任何想法!

4

2 回答 2

0

您是否允许命令行访问?转到小部件属性(在左侧菜单中),然后扩展并选中允许命令行访问,否则小部件无法与系统对话。不确定这是否是导致问题的原因。

于 2011-04-01T10:03:07.497 回答
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;
}
于 2013-05-09T10:38:52.757 回答