Skip to main content
New Participant
April 14, 2021
Answered

Is it possible to run a mac os shell command within javascript in Indesign like in photoshop

  • April 14, 2021
  • 4 replies
  • 1840 views

Hey Community, is it possible to run a shell command within javascript in Indesign
like in photoshop for example "app.system('say "hello world"')" is there a Indesign equivalent ?
Thanks for help.

    This topic has been closed for replies.
    Correct answer TᴀW

    Not with Javascript, but you can run an Applescript snippet with doScript in Javascript.

    4 replies

    New Participant
    January 24, 2024

    I am attaching how I do this in windows, it may also apply to mac as well writing to .sh text file. I basically create a .cmd file that gets executed. If it exits already, I delete it. In this example I am creating a mapped network drive.

     

     

        var cmdFolder = new Folder('~/Desktop/logs/');
            cmdFolder.create();
        var cmdFile = new File(cmdFolder + '/mapDrive.cmd');
            if(cmdFile.exists){
                cmdFile.remove();
            }
            if ( cmdFile.open( "e" )){
                cmdFile.seek( 0, 2 );
                cmdFile.writeln( 'net use Z: \\\\1.1.1.1\\ \/user:sampleUser SamplePassword' );
                cmdFile.close();
                cmdFile.execute();
            }

     

    New Participant
    March 11, 2024

    In Windows it's possible. But I was not able to run a script file (`.sh` or `.command`) which was created on-the-fly via jsx in MacOS. The permission of created file is not enough to run, so user should do `chmod` in Terminal, which is not acceptable. I'm looking for another ways but hopeless so far.

    TᴀW
    TᴀWCorrect answer
    Brainiac
    April 14, 2021

    Not with Javascript, but you can run an Applescript snippet with doScript in Javascript.

    New Participant
    April 14, 2021

    Thanks TaW and the Community to point me in the right direction.

     

    To execute a shell command with javascript in photoshop you can use this code:

    app.system('say "hello world"')

     

    To execute a shell command with javascript in indesign you can use this code:
    Pitfall: if you are using double quotes (") in your shell command you have to use
    "quoted form of" like in this example.
    myScript = ' do shell script "say " & quoted form of "hello world" '
    app.doScript(myScript, ScriptLanguage.applescriptLanguage);
     
    best regards
    -Thomas
    Adobe Expert
    April 14, 2021

    Hi Thomas,

    app.system() is not available with ExtendScript for InDesign.

    Would file.execute() work for your purpose?

     

    Regards,
    Uwe Laubender

    ( ACP )

    New Participant
    April 14, 2021

    Hi Uwe,

    thanks for your fast response but the "file.execute()" don´t fits my purpose.

    I m looking for an "easy" way to use some shell commands in combination with Indesign and Javascript.

    I try to avoid to execute a file.

    Regards,

    Thomas

     

     

    Peru Bob
    Adobe Expert
    April 14, 2021

    I've moved this from the Using the Community forum (which is the forum for issues using the forums) to the InDesign forum so that proper help can be offered.

    Brainiac
    April 14, 2021

    Hello,

     

    Here's an example of what @TᴀW is taking about that I use to zip the Document Fonts folder.....hope this helps!

    var doc = app.documents[0];
    
    myDocFolder = doc.filePath;
    var myDocumentFonts = Folder(myDocFolder.fsName + "/Document fonts/");  
    if (!myDocumentFonts.exists) {
    exit();
    }  
    
    CreateFontsZip();
    function CreateFontsZip(){
    var myFontsZipFolder = myDocumentFonts;
    var myFontsZipDestination = doc.filePath;
     var myDocumentFontsZipName = "Document fonts";
       app.scriptArgs.setValue("zip", myFontsZipFolder)
        app.scriptArgs.setValue("zipName", myDocumentFontsZipName)
         app.scriptArgs.setValue("zipLocation", myFontsZipDestination)
         var myFontsZipScript = ''' tell script args
       set myZip to get value name "zip"
     set theZipName to get value name "zipName"
    set destFold to get value name "zipLocation"
    do shell script ("ditto -c -k --sequesterRsrc " & quoted form of POSIX path of myZip & " " & quoted form of POSIX path of destFold & "" & quoted form of POSIX path of theZipName & "" & ".zip")
    end tell '''
    app.doScript(myFontsZipScript, ScriptLanguage.applescriptLanguage);
    }

    Regards,

    Mike