javascriptprintingextendscriptcomputer-name

Getting the current user name or computer name in Extendscript


I'm finally getting some headway made in my script for InDesign CS6. Now, in order for it to know to which printer it should print a document, I need to query which computer the script is running on. Barring that, perhaps the name of the currently logged-in user, which is really the same thing. This is a Mac OS X environment, by the way. Can anyone help or at least point me in the right direction?


Solution

  • As seen on http://jongware.mit.edu/idcs6js/pc_$.html, the $.getenv() method can be used to get any environment variable. On Windows you could use $.getenv("COMPUTERNAME"), and on Mac $.getenv("HOSTNAME") should work.

    Let me know if this doesn't work. I don't have a Mac to test on, but there's probably other options.

    EDIT 1: Are you using InDesign Server? If so, app.serverHostName and app.serverSettings.hostName would work.

    EDIT 2: Here's another possible solution:

    var outputFile = new File(Folder.desktop.fsName + "/GetHostName.sh");
    outputFile.open("w");
    try {
        outputFile.write("scutil --get HostName > ~/Desktop/HostName.txt");
    }
    finally {
        outputFile.close();
    }
    outputFile.execute();
    
    var inputFile = new File(Folder.desktop.fsName + "/HostName.txt");
    inputFile.open("r");
    try {
        var hostName = inputFile.read();
    }
    finally {
        inputFile.close();
    }
    
    $.writeln("Host Name: " + hostName);
    

    The idea is to write a shell script to file and then execute it. The shell script gets the host name and writes it to a file. Then we open the file and read the host name.