🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #9821

[AS/JS/VBScript] Socket frustrations and a Mac Only workaround - how would this be done in VBScript?

#9821
From: Robb_Irrgang
Date: Tue, 18 Jul 2006 20:34
46 lines
3039 bytes
After spending 5 hours trying to figure out why a (niche, non-standard) webserver was returning blanks on my Socket connection calls, I decided to bite the bullet and switch to something I knew would work: using CURL to pull down the file and pull it back in. To do so, I'm using the following chimera of JavaScript and AppleScript code:

function randomString(length) {
    if (!length)
        var length = 8;
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var randomstring = '';
    for (var i=0; i<length; i++) { var rnum = Math.floor(Math.random()*chars.length); randomstring += chars.substring(rnum, rnum+1); } return randomstring; }

function doCURL(url, postVars){
    var fName = randomString(48);
    var CURLFile = new File('/tmp/'+fName+'.applescript');
    CURLFile.open("w");
    if (!postVars)
        CURLFile.writeln("do shell script \"curl "+url+" > /tmp/"+fName+".output\"");
    else {
        var postVarString = "";
        for (var i in postVars)
            postVarString += "-d "+i+"="+postVars[i]+" ";
        CURLFile.writeln("do shell script \"curl "+postVarString+url+" > /tmp/"+fName+".output\"");
    }
    CURLFile.close();
    app.doScript(CURLFile, 1095978087);
    CURLFile.remove();
    var CURLOutput = new File('/tmp/'+fName+'.output');
    CURLOutput.open("r");
    var CURLResults = CURLOutput.read(CURLOutput.length);
    CURLOutput.remove();
    return CURLResults;
}

usage (GET):

var foo = doCURL('http://www.adobe.com/');

POST:

var foo = doCURL('http://www.yourserver.com/something.php', {"field1":"value1", "field2":"value2"});

(POST is ofcourse great for passing variables to your server, and I'm using it for password verification of the person using the script, by using a JavaScript MD5 implementation)

this works perfectly on my server, and if you have your web interface passing back JSON, you're pulling down perfectly valid JS variable data that is accessible after a quick eval.

Smashing. Awesome. Great. However, I'm depending on AppleScript, which means that (if I can't use the socket object) I'm writing Mac OS X only code.

Is there a way to do the same type of trick using VBScript? Either through CURL (could probably distribute a CURL executable with the script, if I really had to) or through Microsoft's http/socket implementations?
Technical Details
Message-ID: <3bc0f4f2.-1@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news2.volia.net!news.musoftware.de!news.albasani.net!news.ecp.fr!news.stben.net!webcrossing

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,e4eefea6d13f0ae,start
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Robb_Irrgang@adobeforums.com
Newsgroups: adobe.scripting.indesign
Subject: [AS/JS/VBScript] Socket frustrations and a
 Mac Only workaround - how would this be done in
 VBScript?
Message-ID: <3bc0f4f2.-1@webcrossing.la2eafNXanI>
Date: Tue, 18 Jul 2006 13:34:48 -0700
Lines: 67
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news2.volia.net!news.musoftware.de!news.albasani.net!news.ecp.fr!news.stben.net!webcrossing