尝试从 Widget widget.system() 获取环境变量

发布于 2024-10-27 20:30:37 字数 392 浏览 1 评论 0原文

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

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

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

感谢您的任何想法!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

全部不再 2024-11-03 20:30:37

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

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.

迷乱花海 2024-11-03 20:30:37

我知道这个线程已经很老了,但无论如何,这个问题仍然是最新的:-)

我自己刚开始使用 Dashcode 和小部件,我对此做了一个快速破解:

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

对于我的实验小部件,我确实使用了滚动区域和一个按钮。 doGetEnv(event) 在 onclick 时触发,通过检查器设置。 Id“内容”是滚动区域内内容的标准命名。

out var 包含一个带有 '\n' 字符的字符串,使用 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];
    }
}

在我的例子中,第一个条目是“BASH...”。
如果您搜索特定项目,请使用 STRING 的匹配方法(另请参阅 http://www.w3schools. com/jsref/jsref_match.asp)以及以下有关正则表达式的页面:

要缓存环境,您可以使用:

var envCache = "";

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

这将在 envCache 中留下一个数组。选择:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文