Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Development
Goto page 1, 2  Next

More problems with widget.system

Author Message
therealmithrandir



Joined: 30 Apr 2005
Posts: 2

Posted: Sat Apr 30, 2005 - 10:39 pm    Post subject: More problems with widget.system Reply with quote

I am getting seemingly inconsistent results with the widget.system method.

with one command I get what I expect with the same command with an added switch I get no results at all. In fact my widget stops executing at that point. I have inserted alerts to confirm this.

Here is a code snippet:
Code:

   outString = widget.system("/bin/ps -vcxrww", null).outputString;
//   outString = widget.system("/bin/ps -vacxrww", null).outputString;


   var split = outString.split("\n");
   var html = "";
   for ($i = 0; $i <= split.length-1; $i++)
      {
      
      html += split[$i] + "<br>";
      }
   
   document.getElementById("output").innerHTML = html;


This works fine with the line:
outString = widget.system("/bin/ps -vcxrww", null).outputString;

but does not work with this line instead:
outString = widget.system("/bin/ps -vacxrww ", null).outputString;

The only differeance between these line is the addition of the -a switch to the command.

Any ideas?

Thanks.
Josh
View user's profile Send private message Send e-mail Widgets
ebustaman



Joined: 22 Apr 2005
Posts: 8

Posted: Sun May 01, 2005 - 12:31 am    Post subject: Reply with quote

Yes, since my widgets are relying heavily on outside scripts I ran into this problem quite a few times. This is a major flaw in the widget.system design where the output can only be less than 4096bytes (4k). You can see this for yourself: make a file of 4095bytes, and do a simple system.widget("/bin/cat 4095",null).outputString, you will get back those 4095 bytes. Now add 1 more byte to the file to make it 4096bytes, and run the same command. You will receive nothing back.

The work around for this is to dump the output to a file, and then read it in with the following function:

Code:

function readInFile(filename) {
    req = new XMLHttpRequest();
    req.open("GET", filename ,false);
    req.send(null);
    response = req.responseText;
    if (response) {
        return response;
    }
    return null
}


In your code, run your widget.system("/bin/ps -blah > output.txt",null) and then call readInFile like var theOutput = readInFile("output.txt")
It's a huge pain in the ass and it took me many hours to find out the problem. Considering the documentation on widget.system makes no mention of this utterly ridiculous limitation. I hope this helps you all stuck with this issue.[/code]
View user's profile Send private message Send e-mail Widgets
therealmithrandir



Joined: 30 Apr 2005
Posts: 2

Posted: Sun May 01, 2005 - 12:38 am    Post subject: Suspicions realized Reply with quote

That was my suspicion but I couldn't imagine that there would be such a silly limitation!

Thanks for the tip and work around! Much appreciated!

Josh
View user's profile Send private message Send e-mail Widgets
ebustaman



Joined: 22 Apr 2005
Posts: 8

Posted: Sun May 01, 2005 - 12:44 am    Post subject: Reply with quote

For something Apple prominently displays as being a killer feature for widget development, and for it to be limited to such a small amount of output, is simply dumb. If it weren't for XmlHTTPRequest, it would be a larger pain in the ass to solve the problem Razz
View user's profile Send private message Send e-mail Widgets
collin



Joined: 02 May 2005
Posts: 17

Posted: Mon May 02, 2005 - 7:46 am    Post subject: Reply with quote

This thread was most helpful. I decided to come here and sign up while working on a widget and discovered this solution. Now I can do something else besides bang my head against the desk for the rest of the day trying to fix my widget.system calls Smile
View user's profile Send private message Widgets
Chris



Joined: 27 Jan 2005
Posts: 344
Location: Durham, UK

Posted: Mon May 02, 2005 - 9:39 am    Post subject: Reply with quote

What about handling it asynchronously?

Code:

var cat = widget.system("/bin/cat somehugefile", callBack);
cat.onreadoutput = handleCat;

function handleCat(output) {
   alert(output);
}

function callBack() {
   alert('finished reading file');
}


I haven't tried it but...

Btw, this is the 4th time I've tried to post this, but I keep getting phpBB critical errors...
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
collin



Joined: 02 May 2005
Posts: 17

Posted: Mon May 02, 2005 - 9:57 am    Post subject: Reply with quote

Chris wrote:
What about handling it asynchronously?

Code:

var cat = widget.system("/bin/cat somehugefile", callBack);
cat.onreadoutput = handleCat;

function handleCat(output) {
   alert(output);
}

function callBack() {
   alert('finished reading file');
}


I haven't tried it but...

Btw, this is the 4th time I've tried to post this, but I keep getting phpBB critical errors...


Given the above 4K limitation, I'm pretty sure that won't work. I can confim, however, that the XMLHttpRequest method works beatifully...but it's not async Confused
View user's profile Send private message Widgets
Chris



Joined: 27 Jan 2005
Posts: 344
Location: Durham, UK

Posted: Mon May 02, 2005 - 11:44 am    Post subject: Reply with quote

The reason I posted about going asynchrnous was as a way to get round that 4k limitation.

Anyhoo, I just tried it with a 52kb file and it worked.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
collin



Joined: 02 May 2005
Posts: 17

Posted: Tue May 03, 2005 - 1:59 am    Post subject: Reply with quote

Chris wrote:
The reason I posted about going asynchrnous was as a way to get round that 4k limitation.

Anyhoo, I just tried it with a 52kb file and it worked.


That's awesome.. How do you handle the data as it's returned, though?

Maybe I'm misunderstanding, but my concept of it was that it continuously returns data and you have to sort through it as it becomes available. But from the code it looks as though it simply starts reading the file and gives control back to the user, then just runs the callback when it's all done. That would make more sense..
View user's profile Send private message Widgets
Chris



Joined: 27 Jan 2005
Posts: 344
Location: Durham, UK

Posted: Tue May 03, 2005 - 2:20 am    Post subject: Reply with quote

You handle the data in the function designated to be the onreadoutput handle, which is handleCat() in my example.

The outputString is sent as an argument to the hadler function, so you can work on that - hence why I was alert()'ing `output' - you would just do whatever you want to that in the same way you would for outputString. Or even keep appending `output' to a global variable to work on later.

The callBack is just so you know it's finished all it's stuff.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
crahan



Joined: 11 Jul 2005
Posts: 4
Location: Belgium

Posted: Tue Jul 12, 2005 - 9:22 am    Post subject: Reply with quote

The asynchronous method doesn't work in all cases. I've run into a situation using curl, where it fetches a partial html file and then writes the following error to the console log:

Code:
2005-07-12 15:59:37.292 DashboardClient[2213] Exception raised during posting of notification.  Ignored.  exception: *** +[NSArray arrayWithObject:]: attempt to insert nil


Afterwards it starts writing a lot of

Code:
(event handler):Null value


In the console log.

It happens when Curl follows a redirect and fetches the data on the new site. When there's no redirect, all goes well and all the html is returned. In this case it happens when going to http://www.google.com and getting redirected to http://www.google.be.

Example curl command that does this:

Code:

var url = "http://www.google.com";
var curlcommandstring = "/usr/bin/curl  -s -k -L " + url;
var command = widget.system(curlcommandstring, callBack);
command.onreadoutput = handleData;

function handleData(output) {
   alert(output);
}

function callBack() {
   alert('finished reading file');
}


The synchronous method mentioned earlier (using xmlHttpRequest) works fine and is what I'm using for now as a workaround.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
crahan



Joined: 11 Jul 2005
Posts: 4
Location: Belgium

Posted: Wed Jul 13, 2005 - 4:00 am    Post subject: Reply with quote

Kind of replying to myself, but I thought it'd be useful for some people. You can combine an asynchronous widget.system call with the XMLHttpRequest workaround:

Code:

var commandstring = "curl http://www.google.com > outputfile.txt";
var command = widget.system(commandstring, endHandler);

function endHandler(command)
{
     var output = readInFile("outputfile.txt");
     // do some stuff with the output here
}

// previously posted function
function readInFile(filename)
{
    req = new XMLHttpRequest();
    req.open("GET", filename ,false);
    req.send(null);
    response = req.responseText;
    if (response)
    {
        return response;
    }
    return null
}


This will allow you to get output > 4k and won't tie up the widget until the command is finished running. It's probably a bit more convoluted than the asynchronous method using the outputHandler function, but some might find it handy.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
Ludge



Joined: 09 May 2005
Posts: 98
Location: UK

Posted: Wed Jul 13, 2005 - 6:56 am    Post subject: Reply with quote

crahan wrote:
Kind of replying to myself, but I thought it'd be useful for some people. You can combine an asynchronous widget.system call with the XMLHttpRequest workaround:


--CODE--

This will allow you to get output > 4k and won't tie up the widget until the command is finished running. It's probably a bit more convoluted than the asynchronous method using the outputHandler function, but some might find it handy.


I already use this system. It works a treat although it is noticeably slower, which is not very surprising.
_________________
BatteryInfo widget
AdiumList widget.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
StopMakingSense



Joined: 20 Aug 2005
Posts: 2
Location: Santa Monica, CA

Posted: Sat Aug 20, 2005 - 1:44 pm    Post subject: Reply with quote

Thank you so much for this workaround, I was pulling my har out trying to figure out how to handle a timeout error, and this workaround + curl is perfect. TAHNK YOU.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
SpiralOcean



Joined: 16 Jul 2006
Posts: 5

Posted: Sun Jul 16, 2006 - 11:04 am    Post subject: Reply with quote

crahan wrote:
Kind of replying to myself, but I thought it'd be useful for some people. You can combine an asynchronous widget.system call with the XMLHttpRequest workaround:

This will allow you to get output > 4k and won't tie up the widget until the command is finished running. It's probably a bit more convoluted than the asynchronous method using the outputHandler function, but some might find it handy.


This is increadible! Thank you everyone for posting this.

I have a question about that output file? Where is it created. I searched for it but didn't find it. Is it stored in memory and then just evaporates once the script is done?
View user's profile Send private message Send e-mail Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Goto page 1, 2  Next
Powered by phpBB © 2001, 2002 phpBB Group