• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Execute a VBScript inside a JavaScript

Explorer ,
Oct 23, 2017 Oct 23, 2017

Copy link to clipboard

Copied

Hi there

How are you doing?

I´ve found an excellent script from IndiSnip [InDesign® Snippets] | Adobe® InDesign® Scripting Snippets

It´s a VBScript that I would like to use inside a JavaScript.

When I use it as a VBS file, everything works good, but I am not successful when I write it in JavaScript using the
app.doScript('My VBSCript line', ScriptLanguage.visualBasic);

I did it in many ways, I could not complete the script in debug mode.

Anyone could help me?

Link of script:

Get network adapter Name/MAC/Speed | IndiSnip [InDesign® Snippets]

Thanks

strComputer = "."

SetobjWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")

SetIPConfigSet = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

SetobjWMI = GetObject("winmgmts:\\"& strComputer & "\root\WMI")

SetAdaptInfo = objWMI.InstancesOf("MSNdis_LinkSpeed WHERE Active = True",48)

ForEachadaptInfo in AdaptInfo

    AdaptNameLink = AdaptNameLink & adaptInfo.InstanceName & vbNewLine

    AdaptSpeed = AdaptSpeed & adaptInfo.NdisLinkSpeed & vbNewLine

Next

ForEachIPConfig in IPConfigSet

    IfNotIsNull(IPConfig.Description) Then

        AdaptName = AdaptName + IPConfig.Description & vbNewLine

    EndIf

    IfNotIsNull(IPConfig.MACAddress) Then

        MACadd = MACadd + IPConfig.MACAddress & vbNewLine

    EndIf

Next

app.scriptArgs.SetValue "myAdaptNameLink", AdaptNameLink

app.scriptArgs.SetValue "myAdaptSpeed", AdaptSpeed

app.scriptArgs.SetValue "myAdapt", AdaptName

app.scriptArgs.SetValue "myMAC", MACadd

TOPICS
Scripting

Views

13.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Oct 25, 2017 Oct 25, 2017

Oh sorry, I forgot to mention: you have to double the backslashes in the vbs string.

25-10-2017 15-55-07.png

This code works:

main();

function main() {

    var vbs = 'strComputer = "."\r';

    vbs += 'Set objWMIService = GetObject("winmgmts:\\\\" & strComputer & "\\root\\cimv2")\r';

    vbs += 'Set IPConfigSet = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")\r';

    vbs += 'Set objWMI = GetObject("winmgmts:\\\\" & strComputer & "\\root\\WMI")\r';

    vbs += 'Set AdaptInfo = o

...

Votes

Translate

Translate
Engaged ,
Oct 24, 2017 Oct 24, 2017

Copy link to clipboard

Copied

Your VB script needs to be a string.

Put your VB script in a variable, and use quotes and line breaks (\n). Then call the script by passing in your variable name (without quotes).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 24, 2017 Oct 24, 2017

Copy link to clipboard

Copied

Hi Jake

How are you doing?

About the \n, I've never done it before.

I will try it later and you will send you a feedback

Thanks a lot for the tip.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 24, 2017 Oct 24, 2017

Copy link to clipboard

Copied

Jake

How are you doing?

Sorry, but it didn´t work. 😞

Take a look at the above image and you will see the early error on the second line.

Do you have any suggestions?

Thanks

PS.: I did the same thing with an AppleScript and it worked fine.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 24, 2017 Oct 24, 2017

Copy link to clipboard

Copied

Place both scripts in the same folder, like so:

25-10-2017 3-00-13.png

JavaScript

var activeScript = GetActiveScript();

app.doScript(File(activeScript.path+'/GetMyAdapter.vbs'), ScriptLanguage.visualBasic);

var myAdaptName = app.scriptArgs.getValue("myAdaptNameLink");

var myAdaptSpeed = app.scriptArgs.getValue("myAdaptSpeed");

var myAdapt = app.scriptArgs.getValue("myAdapt");

var myMAC = app.scriptArgs.getValue("myMAC");

alert ("myAdaptName: " + myAdaptName + " | myAdaptSpeed: " + myAdaptSpeed + " | myAdapt: " + myAdapt + "| myMAC: " + myMAC);

function GetActiveScript() {

    try {

        return app.activeScript;

    }

    catch(err) {

        return new File(err.fileName);

    }

}

VBS

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\WMI")

Set AdaptInfo = objWMI.InstancesOf("MSNdis_LinkSpeed WHERE Active = True",48)

For Each adaptInfo in AdaptInfo

    AdaptNameLink = AdaptNameLink & adaptInfo.InstanceName & vbNewLine

    AdaptSpeed = AdaptSpeed & adaptInfo.NdisLinkSpeed & vbNewLine

Next

For Each IPConfig in IPConfigSet

    If Not IsNull(IPConfig.Description) Then

        AdaptName = AdaptName + IPConfig.Description & vbNewLine

    End If

    If Not IsNull(IPConfig.MACAddress) Then

        MACadd = MACadd + IPConfig.MACAddress & vbNewLine

    End If

Next

app.scriptArgs.SetValue "myAdaptNameLink", AdaptNameLink

app.scriptArgs.SetValue "myAdaptSpeed", AdaptSpeed

app.scriptArgs.SetValue "myAdapt", AdaptName

app.scriptArgs.SetValue "myMAC", MACadd

I added the GetActiveScript function so you could run it from ESTK as well.

The result

25-10-2017 2-58-59.png

You can combine both scripts into one jsx-file. In this case VBS should be a string. Use two pairs of quotes -- double and single -- there to include js variables into vbs. I use single outside and double inside.

See here an example of how to do this (Function for Windows).

25-10-2017 3-13-12.png

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 24, 2017 Oct 24, 2017

Copy link to clipboard

Copied

Kas

When I try to execute the VBS as a string in the JS, it not works.
When I do it separated, it works fine.

I would like to convert it in a binary file, and the file as a VBS, I can´t.

The AppleScript made using the same idea works fine on Mac.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 25, 2017 Oct 25, 2017

Copy link to clipboard

Copied

Oh sorry, I forgot to mention: you have to double the backslashes in the vbs string.

25-10-2017 15-55-07.png

This code works:

main();

function main() {

    var vbs = 'strComputer = "."\r';

    vbs += 'Set objWMIService = GetObject("winmgmts:\\\\" & strComputer & "\\root\\cimv2")\r';

    vbs += 'Set IPConfigSet = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")\r';

    vbs += 'Set objWMI = GetObject("winmgmts:\\\\" & strComputer & "\\root\\WMI")\r';

    vbs += 'Set AdaptInfo = objWMI.InstancesOf("MSNdis_LinkSpeed WHERE Active = True",48)\r';

    vbs += 'For Each adaptInfo in AdaptInfo\r';

    vbs += 'AdaptNameLink = AdaptNameLink & adaptInfo.InstanceName & vbNewLine\r';

    vbs += 'AdaptSpeed = AdaptSpeed & adaptInfo.NdisLinkSpeed & vbNewLine\r';

    vbs += 'Next\r';

    vbs += 'For Each IPConfig in IPConfigSet\r';

    vbs += 'If Not IsNull(IPConfig.Description) Then\r';

    vbs += 'AdaptName = AdaptName + IPConfig.Description & vbNewLine\r';

    vbs += 'End If\r';

    vbs += 'If Not IsNull(IPConfig.MACAddress) Then\r';

    vbs += 'MACadd = MACadd + IPConfig.MACAddress & vbNewLine\r';

    vbs += 'End If\r';

    vbs += 'Next\r';

    vbs += 'app.scriptArgs.SetValue "myAdaptNameLink", AdaptNameLink\r';

    vbs += 'app.scriptArgs.SetValue "myAdaptSpeed", AdaptSpeed\r';

    vbs += 'app.scriptArgs.SetValue "myAdapt", AdaptName\r';

    vbs += 'app.scriptArgs.SetValue "myMAC", MACadd\r';

    app.doScript(vbs, ScriptLanguage.VISUAL_BASIC, undefined, UndoModes.FAST_ENTIRE_SCRIPT);

}

var myAdaptName = app.scriptArgs.getValue("myAdaptNameLink");

var myAdaptSpeed = app.scriptArgs.getValue("myAdaptSpeed");

var myAdapt = app.scriptArgs.getValue("myAdapt");

var myMAC = app.scriptArgs.getValue("myMAC");

alert("myAdaptName: " + myAdaptName + " | myAdaptSpeed: " + myAdaptSpeed + " | myAdapt: " + myAdapt + "| myMAC: " + myMAC);

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 28, 2017 Oct 28, 2017

Copy link to clipboard

Copied

Kas

You were amazing!!!

The problem was the backslashes.

Thanks, thanks and one thousand more thanks.

Vinicius

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 28, 2017 Oct 28, 2017

Copy link to clipboard

Copied

I posted it here on my site -- in the tips & tricks section -- for the future generations.

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 29, 2017 Nov 29, 2017

Copy link to clipboard

Copied

Kas

How are you doing?

I am here again because I´m suffering with the same mistakes.

Now I have a new VBS and I´m trying to execute it from a JSX.

The result is an error.

Original VBS

URL="http://viniciusamaral.net"

Set WshShell = WScript.CreateObject("WScript.Shell")

Set http = CreateObject("Microsoft.XmlHttp")

On Error Resume Next

http.open "GET", URL, False

http.send ""

if err.Number = 0 Then

     WScript.Echo http.responseText

Else

     Wscript.Echo "error " & Err.Number & ": " & Err.Description

End If

set WshShell = Nothing

Set http = Nothing

My version in JSX

#target indesign

main()

function main(){

    var vbs = 'URL ="http://viniciusamaral.net"\r';

    vbs += 'Set WshShell = WScript.CreateObject("WScript.Shell")\r';

    vbs += 'Set http = CreateObject("Microsoft.XmlHttp")\r';

    vbs += 'On Error Resume Next\r';

    vbs += 'http.open "GET", URL, False\r';

    vbs += 'http.send ""\r';

    vbs += 'if err.Number = 0 Then\r';

         vbs += 'MsgBox(http.responseText)\r';

    vbs += 'Else\r';

         vbs += 'MsgBox("error " & Err.Number & ": " & Err.Description)\r';

    vbs += 'End If\r';

    vbs += 'set WshShell = Nothing\r';

    vbs += 'Set http = Nothing\r';

    try{

        app.doScript(vbs, ScriptLanguage.VISUAL_BASIC, undefined, UndoModes.FAST_ENTIRE_SCRIPT); 

        }catch(e){

            alert(e)

            }

    }

I´ve changed the slash to double slash and it didn´t work.

Can you help me, please?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

How are you doing?

I'm fine, thanks. And you?

Can you help me, please?

I know nothing at all about the Windows Script Host thing, but after comparing your script with another working example found here on the forum, the second line stroke my eye.

Try to change

vbs += 'Set WshShell = WScript.CreateObject("WScript.Shell")\r';

to

vbs += 'Set WshShell = CreateObject("WScript.Shell")\r';

This code works for me:

main() 

 

function main() {

    var vbs = 'URL ="http://viniciusamaral.net"\r';

    vbs += 'Set WshShell = CreateObject("WScript.Shell")\r'; 

    vbs += 'Set http = CreateObject("Microsoft.XmlHttp")\r'; 

    vbs += 'On Error Resume Next\r'; 

    vbs += 'http.open "GET", URL, False\r'; 

    vbs += 'http.send ""\r'; 

    vbs += 'if err.Number = 0 Then\r'; 

    vbs += 'MsgBox(http.responseText)\r'; 

    vbs += 'Else\r'; 

    vbs += 'MsgBox("error " & Err.Number & ": " & Err.Description)\r'; 

    vbs += 'End If\r'; 

    vbs += 'set WshShell = Nothing\r'; 

    vbs += 'Set http = Nothing'; 

   

    try { 

        app.doScript(vbs, ScriptLanguage.VISUAL_BASIC, undefined, UndoModes.FAST_ENTIRE_SCRIPT);  

    }

    catch(err) {

        alert(err.message + ", line: " + err.line);

    }

}

I don't have the slightest idea of what the code does, but on my side it results in the following pop-up window:

30-11-2017 13-59-14.png

Hope it helps!

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 01, 2017 Dec 01, 2017

Copy link to clipboard

Copied

Kas

I´m also fine. Thanks for asking.

You are amazing!

I should compare this line with another example, but I´ve focused at the same old error of backslash and reserved characters.

Thank you again.

This VBS reads an web page and return the contents as HTML.
I´m working in a project with online contents.

A´m using an AppleScript too, but it works fine doing the same thing with only 2 or 3 lines.

I have no experience with both languages (AppleScript and VBS), but I should to use it because the Socket of JavaScript is too slow and reliable.

Below you can see one link for a question that I did before about one socket function.
It was solved, but the results take much more time than I have and doesn´t work inside networks handled by proxies.

https://forums.adobe.com/thread/2285205

So, I decided to create a script with 3 different languages inside an only JSX file.

Using ifs and elses, I ready the platform and use the correspondent function.
I don´t know if it is the better way to do it, but it was the way that works fine to me.

You were so important to my ideas become true.

I appreciate, because you spend some time to help me and I know that this issue will help a lot of javascripters.

Thanks!

Vinícius

https://forums.adobe.com/thread/2285205

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 02, 2017 Dec 02, 2017

Copy link to clipboard

Copied

Hi Vinícius,

This is fantastic! Getting the data using the 'run WshShell via VBS' approach takes nothing in comparison with getting it via the built-in and super-slow Socket object. According to my rough estimates: the latter is, at least, 5 times slower:

02-12-2017 14-34-00.png

Below is the code I used for the test. Both functions perform the same task: read the front page on my site and output the html-contents to console.

Main();

function Main() {

    var webAddress = "kasyan.ho.com.ua";   

    var startTime = new Date();

    GetDataFromSocket(webAddress);

    var endTime = new Date();

    var duration_1 = GetDuration(startTime, endTime);

    if (File.fs == "Windows") {

        startTime = new Date();

        GetDataFromWshShell(webAddress);

        endTime = new Date();

        var duration_2 = GetDuration(startTime, endTime);

    }

    else { // Mac

        //  The code to be written in AppleScript

    }

   

    alert("Time elapsed:" + "\rSocket - " + duration_1 + "\rWshShell - " + duration_2, "Finished");

}

function GetDataFromSocket(webAddress) { 

    var conn = new Socket; 

    var host = webAddress; 

    var cmd = "GET /index.html HTTP/1.1\r\nHost:"+host+"\r\n\r\n"; 

    var reply=''; 

    conn.timeout = 100; 

    if (conn.open (host +':80', 'BINARY')) { 

            conn.write (cmd); 

            while (conn.connected && ! conn.eof) { 

               reply += conn.read(1024) + "\n"; 

            } 

            conn.close(); 

           $.writeln("Output from Socket:\n" + reply); 

    }

}

function GetDataFromWshShell(webAddress) { 

    var vbs = 'Dim str\r';

    vbs += 'URL = "http://' + webAddress + '"\r';

    vbs += 'Set WshShell = CreateObject("WScript.Shell")\r';

    vbs += 'Set http = CreateObject("Microsoft.XmlHttp")\r'; 

    vbs += 'On Error Resume Next\r'; 

    vbs += 'http.open "GET", URL, False\r'; 

    vbs += 'http.send ""\r'; 

    vbs += 'if err.Number = 0 Then\r'; 

    vbs += 'str = http.responseText\r';

    vbs += 'Else\r'; 

    vbs += 'MsgBox("error " & Err.Number & ": " & Err.Description)\r';

    vbs += 'End If\r'; 

    vbs += 'set WshShell = Nothing\r'; 

    vbs += 'Set http = Nothing\r';

    vbs += 'Set objInDesign = CreateObject("InDesign.Application")\r';

    vbs += 'objInDesign.ScriptArgs.SetValue "WshShellResponse", str';

     

    try {   

        app.doScript(vbs, ScriptLanguage.VISUAL_BASIC, undefined, UndoModes.FAST_ENTIRE_SCRIPT);    

    } 

    catch(err) { 

        alert(err.message + ", line: " + err.line); 

    }

    var str = app.scriptArgs.getValue("WshShellResponse");

    app.scriptArgs.clear();

    $.writeln("Output from WshShell:\n" + str);

}  

function GetDuration(startTime, endTime) {

    var str;

    var duration = (endTime - startTime)/1000;

    duration = Math.round(duration);

    if (duration >= 60) {

        var minutes = Math.floor(duration/60);

        var seconds = duration - (minutes * 60);

        str = minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");

        if (minutes >= 60) {

            var hours = Math.floor(minutes/60);

            minutes = minutes - (hours * 60);

            str = hours + ((hours != 1) ? " hours, " : " hour, ") + minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");

        }

    }

    else {

        str = duration + ((duration != 1) ? " seconds" : " second");

    }

    return str;

}

Vinícius, could you post the code you use in AppleScript? I'd like to test it on Mac when I get to work on Wednesday.

My fingers are itching to write a post on my site about this new discovery for the future generations!

Also, I found your site. Hope some day you will write about the project you're working on. I wonder what you have at start and what you get after running the script.

Thank you for your detailed explanation! We both learn from each other here on the forum.

Regards,
Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Hi Kasyan

How are you doing?

First of all, sorry for the delay in my answer.

I´m working hard now.

This is the AppleScript code to get a HTML page contents.

var myAppleScript = 'do shell script "curl http://www.viniciusamaral.net/"';

var myCode = app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);

See, it is so simple!!!

The life in Mac is easier.

Thanks again for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Thanks, Vinícius!

I will test it on my Mac at work later today.

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

Hi Kasyan,

curl has a massive amount of features:

curl - How To Use

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 06, 2017 Dec 06, 2017

Copy link to clipboard

Copied

Thanks, Uwe!

I will look into this later when I have time: at the moment I am too busy with reworking fonts in FontLab for the current issue of our magazine (to be finished this Friday). Today, all of a sudden, they (our bosses) decided to switch the language: from Russian to Ukrainian. Obviously, they have no idea that a few fonts we use don't have Ukrainian characters.

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jan 04, 2018 Jan 04, 2018

Copy link to clipboard

Copied

LATEST

I think we can remove a couple of lines from the VB-script -- by the way, the first one caused the problem in post 9 -- because they are unnecessary:

vbs += 'Set WshShell = CreateObject("WScript.Shell")\r';

and

vbs += 'set WshShell = Nothing\r';

The object is created but never used later in the script. So, why to create it at all?

Here I made a post on my site with updated version. It works without these two lines for me.

— Kas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines