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

Shell script calling from Javascript

Explorer ,
Sep 21, 2023 Sep 21, 2023
Dear All.
 
Having a doubt with Mac OS Monterey - Mac Book Pro (12.6.9). 
 
We are trying to call a "Sh" file through Javascript. But it is not working
 
//==============================================//
 var fSystUsrName = "harisu"
 
var connetProcessSH_File = new File(Folder(fImageFolderPath) + "/TempProcess/connectURL.sh");


var fImageFolderPath = "/Users/"+fSystUsrName+"/Desktop//Development"

if (File(Folder(fImageFolderPath) + "/TempProcess/connectProcess.sh").exists) {
File(Folder(fImageFolderPath) + "/TempProcess/connectProcess.sh").remove();
}
 
connetProcessSH_File.encoding = "UTF-8";
connetProcessSH_File.open("w");
 
var Str = "cd "+ "\""+ "/Applications/Adobe InDesign 2023/Scripts/Scripts Panel/Workflow_Development/BoardNdSource" + "\""+"\n";
Str += "/usr/local/bin/node index.js "+ "\""+fURLWebSiteName + "\""+ " " + "\""+ fidUserIDInformation +"\""+" ";
Str += "\""+fidPasswordInformation+"\""+" "+"\""+"Page_"+fCurntPageName+".jpg"+"\""+" " + "\""+fImageFolderPath+"\""+" ";
Str += "\""+fidsURL_Image_Page_ScreenShot_Infrms+"\""+" "+"\""+fProcessingInfo+"\"";
 
connetProcessSH_File.write(Str);
connetProcessSH_File.close();
 
scpt = "/Users/"+fSystUsrName+"/Desktop/Development/TempProcess/"+"page_1+"_connectURL.sh";
 
 
shell("sh "+ scpt)

 

function shell(cmd) {
var
rv,
call ='do shell script "'+ cmd.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+'"';

try {
rv = app.doScript(call,ScriptLanguage.APPLESCRIPT_LANGUAGE);
} catch(e0) {
rv = e0+"\n"+(rv?"":rv);
}
return rv;
}
 
//==============================================//
 
Kindly any one can help me will appriciate.... 🙂
 
Thanks & Regards
Harihara Sudhan T R.,
TOPICS
Scripting
658
Translate
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 ,
Sep 21, 2023 Sep 21, 2023

Do you get any errors when executing your script. In the past I create a command file instead of sh file and then executed it directly with the execute method of the File object, you can try that as well

-Manan

Translate
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 ,
Sep 21, 2023 Sep 21, 2023

Hi Harihara, there were quite a few problems with your script. For example in one case you try to use a variable before you have declared it. In another case the you did not escape quote characters in your paths, or maybe just got the string quoting wrong—it was hard to tell—or it could have been ruined when you pasted it into the forum (it's best to use the code </> button to format code).

 

Also I suggest you do not store your resources (see "var Str =") inside the Adobe Indesign app folder, because this will change when a new version is installed, and may even be deleted!

 

Here is a version of your code that I fixed so that the script file gets saved successfully, but your code was missing a bunch of variables so I just filled them with "test" strings. Have a careful look and see what I changed.

- Mark

 

// I added these vars just for this demo
// but you need to make sure they exist
var fURLWebSiteName = "test1",
    fidUserIDInformation = "test2",
    fidPasswordInformation = "test3",
    fCurntPageName = "test4",
    fidsURL_Image_Page_ScreenShot_Infrms = "test5",
    fProcessingInfo = "test6";

//==============================================//
var fImageFolderPath = "~/Desktop/Development"
var connetProcessSH_File = new File(Folder(fImageFolderPath) + "/TempProcess/connectURL.sh");

if (File(Folder(fImageFolderPath) + "/TempProcess/connectProcess.sh").exists) {
    File(Folder(fImageFolderPath) + "/TempProcess/connectProcess.sh").remove();
}

var Str = "cd " + "\"" + "/Applications/Adobe InDesign 2023/Scripts/Scripts Panel/Workflow_Development/BoardNdSource" + "\"" + "\n";
Str += "/usr/local/bin/node index.js " + "\"" + fURLWebSiteName + "\"" + " " + "\"" + fidUserIDInformation + "\"" + " ";
Str += "\"" + fidPasswordInformation + "\"" + " " + "\"" + "Page_" + fCurntPageName + ".jpg" + "\"" + " " + "\"" + fImageFolderPath + "\"" + " ";
Str += "\"" + fidsURL_Image_Page_ScreenShot_Infrms + "\"" + " " + "\"" + fProcessingInfo + "\"";

connetProcessSH_File.encoding = "UTF-8";
connetProcessSH_File.open("w");
connetProcessSH_File.write(Str);
connetProcessSH_File.close();

scpt = connetProcessSH_File.fsName;

shell("sh " + scpt)

function shell(cmd) {
    var
        rv,
        call = 'do shell script "' + cmd.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';

    try {
        rv = app.doScript(call, ScriptLanguage.APPLESCRIPT_LANGUAGE);
    } catch (e0) {
        rv = e0 + "\n" + (rv ? "" : rv);
    }
    return rv;
}

//==============================================//

 

I did not evaluate the shell script itself, but this is the output I got with the above script:

cd "/Applications/Adobe InDesign 2023/Scripts/Scripts Panel/Workflow_Development/BoardNdSource"
/usr/local/bin/node index.js "test1" "test2" "test3" "Page_test4.jpg" "~/Desktop/Development" "test5" "test6"

 I think you may need to put semi-colon ; between commands (eg. after BoardNdSource"), but I'm not a shell script expert.

Translate
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 ,
Sep 22, 2023 Sep 22, 2023

Dear Mark,

 

Thank you for your comments. 

We got another way and completed my script.

 

Yes. I created the below step

1. First script will gather the informaiton (Main Script)

2. Second script will create a ".sh" file in the particular folder ( through UXP script -- running perfectly)

3. Thrid script will execute the ".sh" file ( through UXP script -- running perfectly)

 

So this above method I done and working fine the same shell script. 

 

Again the Question to @mark and @Mannan Joshi :

1. Why we are unable to run the Single script it self ( Shell file)

2. I created the UXP script, I tried to calling from JSX.  But some times not working. means, I have to Stop the main script, then the UXP script will show the alert message or running other functionalities.

 

Question :  How to handle this issues. between JSX to UXP script without the breaking issues.

 

Kindly help me will appriciate.... ðŸ™‚
 
Thanks & Regards
Harihara Sudhan T R.,

 

 

 

Translate
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 ,
Sep 22, 2023 Sep 22, 2023

Hi @Harihara28692478zxj6, well done for getting it working!

 

1. Why we are unable to run the Single script it self ( Shell file)

What happens? What error do you see? It's impossible for me to know where the error lies at the moment. In your code above there were numerous errors, each that would stop the code working correctly. Maybe your shell script has errors too?

 

2. I created the UXP script, I tried to calling from JSX.  But some times not working. means, I have to Stop the main script, then the UXP script will show the alert message or running other functionalities.

Sorry but, again, it is impossible for me to know what's wrong. It is like you are saying your car won't drive so you drove your truck, but then you go back to your car and it sometimes works and please tell me what's wrong?

 

You see the difficulty? You will need to break the problem down into small steps, and make sure each step is always working before adding the next step. This is how programming is done—there is really no way around it.

 

Is it possible to put it all in UXP? Perhaps there is functionality that UXP cannot handle yet?

- Mark

Translate
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 ,
Sep 23, 2023 Sep 23, 2023

Hi @m1b  :

 

Thank you!,

 

Actually, we are using the below code in IDJS (UXP ) Script

 

//====================================//

const script = require("uxp").script;
alert(script.args);

let folder = await ufs.getEntryWithUrl("file:"+"Users/harisu/Desktop/Workflow/Development/TempProcess");

try {
filePath = await folder.getEntry("connectURL.sh");
}
catch (error){
filePath = await folder.createEntry("connectURL.sh");
}

filePath = folder.createEntry("connectURL.sh");

const storage = await window.require("uxp").storage;
const files = await folder.createEntry("connectURL.sh", {overwrite: true});

 

//===========================================//

 

So in this case, we will pass the arguments from JSX script to UXP Script.

But the above code in UXP script, unable to receive the Arguments from JSX script. 

 

//==============================

app.doScript("/Applications/Adobe InDesign 2023/Scripts/Scripts Panel/Workflow_Development/InDesignSource/Generations_Process.idjs", ScriptLanguage.UXPSCRIPT, Arguments);
//=====================================//

 

Actually, we come to know that the "await" method using in our UXP so that this is not working.

 

without "await" unable to write with the folder and file. 

 

Kindly check and let me know any solutions are there. Becasue this is not working.

 

Thanks & Regards

Harihara Sudhan T R.,

Translate
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 ,
Sep 24, 2023 Sep 24, 2023

Hi @Harihara28692478zxj6 , I’m not sure if this helps with UXP, but with ExtendScript you could easily call an AppleScipt containing the shell script. Here I have an Applescript saved to the desktop as text (.applescript extension), with a simple date shell script:

 

tell application id "com.adobe.indesign"
	activate
	set dt to do shell script "date +%D"
	display dialog dt
end tell

 

Then a JS calls the AppleScript:

 

//applescript saved as text
var as = readFile(Folder.desktop + "/shellScript.applescript")
app.doScript(as, ScriptLanguage.applescriptLanguage);


/**
* Read a text file 
* @ param p the path to the text file 
* @ return the file‘s text 
*/

function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read(); 
	f.close();
	return x;
}

 

 

Result:

Screen Shot.pngexpand image

Translate
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 ,
Sep 25, 2023 Sep 25, 2023
LATEST

Dear @rob day ,

 

Thank you, but I'm trying that it from UXP script... 

 

Thanks & Regards,

Harihara Sudhan T R

Translate
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