Skip to main content
Roy Marshall
Known Participant
May 7, 2014
Question

Activating Browser window from within an InDesign Javascript

  • May 7, 2014
  • 4 replies
  • 7683 views

Hi All

A part of a Javascript I have inherited, relies on Applescript to call to open the active Safari/Chrome window, and extract some information, then use this in the JS with InDesign.

What I would like to do is eliminate AS altogether, as this restricts me to using Macs, and would like to open this up to PC users too.

I know I can do a platform query, and may be an option to use VBScript to do the equivalent AS routine. I have absolutely no experience with VBScript, so would need some help if this is the option.

Ideally I would like to stay as native JS, but that may be wishful thinking on my part.

Here is a sample of the AS I am using within a doScript:

tell application "Safari"

activate

set imagePath to do JavaScript "frames[2].frames[1].document.links[0].href" in document 1

end tell

then...

tell application "InDesign"

activate

end tell

Does anyone know if this may be possible?

Cheers

Roy

This topic has been closed for replies.

4 replies

Inspiring
January 9, 2023

What about using UXP Script?
You can communicate with BridgeTalk even from versions that cannot use UXP Script.

#target "indesign"
if (parseFloat(BridgeTalk.getSpecifier("indesign", -100).split("-")[1]) < 18) {
    alert("InDesign with UXP is not installed.");
    exit();
}
if(!BridgeTalk.isRunning(BridgeTalk.getSpecifier("indesign", -100))) {
    BridgeTalk.launch(BridgeTalk.getSpecifier("indesign", -100), "background");
}
var myBridgeTalk = new BridgeTalk;
myBridgeTalk.target = BridgeTalk.getSpecifier("indesign", -100);
myBridgeTalk.body = """
    if(app.documents.count() == 0){
        app.documents.add(); // It doesn't seem to work without the document.
    }
    app.activeWindow.minimize();
    app.insertLabel("response", "");
    app.doScript(\"\"\"
        await (async () => {
            return new Promise((resolve, reject) => {
                try{
                    const xhr = new XMLHttpRequest();
                    xhr.open("GET", "https://reqres.in/api/users/2", true);
                    xhr.onload = function(){
                        app.insertLabel("response", xhr.response);
                        resolve();
                    };
                    xhr.send();
                } catch (e) {
                    resolve(e);
                }
            });
        })();
    \"\"\", ScriptLanguage.UXPSCRIPT);
    for(var i = 0; i < 100; i++){
        if(app.extractLabel("response") != ""){
            var myExtractLabelValue = app.extractLabel("response");
            break;
        }else{
            $.sleep(100);
        }
    }
    app.insertLabel("response", ""); // Remains if not initialized
    myExtractLabelValue;  // Return value
""";
myBridgeTalk.onResult = function(result) {
    alert(result.body);
}
myBridgeTalk.onError = function(error) {
    alert("Error: " + error.body);
}
myBridgeTalk.send(10000); // If not supplied or 0, the message is sent asynchronously, and the function returns immediately without waiting for a result.

 

 

 

I used this as a reference for XMLHttpRequest.

https://github.com/AdobeDocs/uxp-indesign/blob/main/src/pages/reference/uxp-scripting-samples/UxpModuleNetwork.idjs 

 

I don't know how to use fetch.
Please someone tell me.

https://developer.adobe.com/indesign/uxp/uxp/reference-js/Global%20Members/Data%20Transfers/fetch/ 

Inspiring
January 9, 2023

I tried with fetch.
Is this usage correct?

#target "indesign"
if (parseFloat(BridgeTalk.getSpecifier("indesign", -100).split("-")[1]) < 18) {
    alert("InDesign with UXP is not installed.");
    exit();
}
if(!BridgeTalk.isRunning(BridgeTalk.getSpecifier("indesign", -100))) {
    BridgeTalk.launch(BridgeTalk.getSpecifier("indesign", -100), "background");
}
var myBridgeTalk = new BridgeTalk;
myBridgeTalk.target = BridgeTalk.getSpecifier("indesign", -100);
myBridgeTalk.body = """
    if (app.documents.count() == 0) {
        app.documents.add(); // It doesn't seem to work without the document.
        app.activeWindow.minimize();
    }
    app.scriptArgs.setValue("response", "");
    app.doScript(\"\"\"
        await (async () => {
            return new Promise((resolve) => {
                try {
                    fetch("https://reqres.in/api/users/2")
                        .then((response) => { return response.text(); })
                        .then((text) => { app.scriptArgs.setValue("response", text); })
                        .finally(() => { resolve(); });
                } catch (e) { 
                    app.scriptArgs.setValue("response", "error");
                    resolve(e);
                }
            })
        })();
    \"\"\", ScriptLanguage.UXPSCRIPT);
    for (var i = 0; i < 100; i++) {
        if (app.scriptArgs.getValue("response") != "") {
            break;
        } else { $.sleep(100); }
    }
    app.scriptArgs.getValue("response"); // Return value
""";
myBridgeTalk.onResult = function(result) {
    alert(result.body);
}
myBridgeTalk.onError = function(error) {
    alert("Error: " + error.body);
}
myBridgeTalk.send(10000); // If not supplied or 0, the message is sent asynchronously, and the function returns immediately without waiting for a result.

 

 

Loic.Aigon
Legend
January 9, 2023

It's my understanding that one can't mix ExtendScript and UXP Scripting. Staying open-minded, I tried your code and my InDesign kindly crashed. Plus point was to open the browser from within an InDesign Script. Yours would want to call a web service if I am not mistaken.

Community Expert
January 5, 2023

You can use the hyperlinkDestination object to launch the browser using javascript. The idea is to add a hyperlinkdestination to a document and the use its showDestination method to open the browser. See the sample code below

var h = app.documents[0].hyperlinkURLDestinations.add("www.google.com")
h.showDestination()

The above code requires a document to be opened in InDesign

-Manan

-Manan
Loic.Aigon
Legend
January 7, 2023

I like this universal solution and the cost of creating both a dummy doc and hyperlink may not be critical.

Roy Marshall
Known Participant
May 11, 2014

OK. Ill wait a little longer, but it looks like I cant do what I need simply in JS.

The difference in my request and the answers suggested is I already have the page open in the browser, and need to get at the variable data hidden in the page.

I will wait a little longer to see if any one else has an ideas, otherwise I will assume it cannot be done.

Thanks anyway.

Roy

Inspiring
May 12, 2014

This is what I suspected… Your problem lies with the URL of of the already open browser… Do your main code in JS and call out a do script to get this string…?

May 10, 2014

*Bump*

pixxxelschubser
Community Expert
Community Expert
May 10, 2014

Searching the forum you can find examples like this (written by Muppet Mark):

// OpenBrowserWithJavascript.jsx

// siehe http://forums.adobe.com/message/4904789#4904789

openURL( 'www.google.com' );

function openURL( address ) {

          var f = File( Folder.temp + '/aiOpenURL.url' );

          f.open( 'w' );

          f.write( '[InternetShortcut]' + '\r' + 'URL=http://' + address + '\r' );

          f.close();

         

          f.execute();

};

Have fun

Community Expert
May 10, 2014

@pixxxel schubser – that code by Muppet Mark is working very well.

Thank you for sharing the link.

Leaves the question: Is it possible to determine a specific browser but the one the operating system is chosing?

On my Mac OSX 10.6.8 I'd like to open a particular URL not in Safari, but in Firefox.

Firefox seems to be my default browser, because if I saved a website to my file system and double click the *.html file I will always get Firefox (even if it's *not* already open).

With the script above executed Safari will open.

I did not find a control for that behavior in my OSX 10.6.8.

Uwe