Using Javascript, I want to prompt a text / variable

Using Javascript, I want to prompt a text / variable

Antonin_JubaultSRF25
Participant Participant
636 Views
7 Replies
Message 1 of 8

Using Javascript, I want to prompt a text / variable

Antonin_JubaultSRF25
Participant
Participant

Hello team,

 

To be faire I've spent 5 hours trying to simply prompt a text in my Autocad Console..

In LISP I can just use (prompt "whatever") but in javascript.. There is a tutorial where they use console.log("whatever") but I cannot get anything to show up.

I am starting to desesparate haha !

0 Likes
Accepted solutions (1)
637 Views
7 Replies
Replies (7)
Message 2 of 8

paullimapa
Mentor
Mentor

Doing a search online on JavaScript creating an alert message window on a web page vs Autolisp in Autocad is similar with the use of the alert function:

https://tracemonitorusa.life/avs/en/mob/global-bb.php?c=5uz6be978z7oz1&k=f2b4ceceab6302232bab474f1b6...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 8

Sea-Haven
Mentor
Mentor

This is lisp but you should be able to use the core part of the code maybe in JavaScript.  "mshta.exe"

 

(defun MsgBox (title options message time / WshShell)
   (setq WshShell (vlax-create-object "WScript.Shell"))
    (vlax-invoke WshShell 'Run
	 (strcat "mshta.exe vbscript:close(CreateObject(\"WScript.Shell\").Popup(\""
		  message "\"," (itoa time) ",\"" title "\"," (itoa options)"))"
         )
    )
   (vlax-release-object WshShell)
)
(MsgBox "this is me" 0 "have a good day" 1)

 

0 Likes
Message 4 of 8

Antonin_JubaultSRF25
Participant
Participant

Thank you for the answer,

 

So far I've only seen alert() as working but if I want to read lists.. Alert is going to be a hell

0 Likes
Message 5 of 8

CodeDing
Advisor
Advisor

EDIT:

Ignore. I misunderstood the question.

Message 6 of 8

CodeDing
Advisor
Advisor
Accepted solution

@Antonin_JubaultSRF25 ,

 

The Javascript API needs a LOT of work. I don't like using it.

Try this dumb work-around instead:

 

Acad.Editor.executeCommand('(prompt "\\nRegistered ZEN command.")');

 

 

Good luck figuring out how to post content with strings or quotes. Would probably have to create your own function to edit string content.

 

Another approach you could do it just open a ToolPalette that hosts a local web page. This would allow you to at least see your console.logs...

Would look like this...

1) Create simple HTML file "test.html":

 

<!DOCTYPE html>
<html lang="en">
   <head>
       <meta charset="utf-8">
       <script type="text/javascript" src="https://df-prod.autocad360.com/jsapi/v4/Autodesk.AutoCAD.js"></script>
       <title>Sample App</title>
   </head>
   <body>
       <h1>Hello World</h1>
       <script>
           console.log("I'm in the console!");
       </script>
   </body>
</html>

 

 

2) Create simple palette via JS API "test.js":

 

Acad.Application.addPalette("My Test Palette", "c:/users/me/folder/test.html");

 

 

3) Create command via AutoLISP:

 

;; Opens my test tool palette
(defun c:TEST ( / filePath)
  (setq filePath "c:\\users\\me\\folder\\test.js")
  (if (findfile filePath)
    (command "_.WEBLOAD" "_l" filePath)
  ;else
    (prompt (strcat "\nCould not locate JavaScript file:\n" filePath))
  );if
  (princ)
);defun

 

 

4) Run TEST command. Palette will open:

image.png

 

5) click inside palette somewhere to set it active. Then press F12 to open developer screen:

image.png

 

Best,

~DD

0 Likes
Message 7 of 8

Antonin_JubaultSRF25
Participant
Participant

Yes I am at the point where either I learn LISP or I stick to JavaScript and it's more complicated.

I think your work around for the command prompt might be a good idea. I should have thought about it.

Agreed for the web page. Currently I don't really need it but I might iin the future so always good to have !

0 Likes
Message 8 of 8

Antonin_JubaultSRF25
Participant
Participant

The "Dumb" work around works really nicely haha