Page 1 of 1

interactive console app

Posted: Sat Dec 09, 2023 6:46 am
by keliko
I'm currently creating a console app in LiveCode, but I'm not sure how to make it interact with the command line.
I want to make it like this

Code: Select all

@echo off
echo Hello, please introduce yourself.

set /p name=Your name: 

echo Are you human?

set /p human=y/n: 

echo What is your favorite programming language?

set /p lang=Your answer: 

echo.
echo Answers:
echo 1. %name%
echo 2. %human%
echo 3. %lang%

pause

this is what i did but i didn't succeed

Code: Select all

on preOpenStack
   if the environment is "command line"  then
      write return to stdout
      write "What is your favorite programming language?" to stdout
      write return to stdout
      read from stdin until EOF
      write it to stdout
   else
      break
   end if
end preOpenStack

./mylivecodeapp -ui
I use Windows 10

Re: interactive console app

Posted: Thu Dec 14, 2023 3:55 am
by keliko
can someone help me?

thanks

Re: interactive console app

Posted: Thu Dec 14, 2023 9:06 am
by LCMark
@keliko: Windows doesn't allow an executable to be both a gui application and a console application: you can either have an app which only has a gui and that detaches immediately from any console it is launched from (thus loosing the connection to stdin/stdout); or you can have an app which always has a console window (but can inherit that console from where it is launched).

Windows executables have a flag in their header which indicates the 'subsystem' the executable should be launched under - LiveCode standalones are built as GUI subsystem apps (as most people don't want a console window appearing when they are launched - there isn't actually any difference between a GUI and CONSOLE subsystem windows executable, they can do identical things both can create and show windows, the only difference is whether they talk to a calling console or not!).

In order to have you standalone act as a console app you need to install the visual studio build tools https://aka.ms/vs/17/release/vs_BuildTools.exe and then you should be able to launch a 'developer prompt' from the start menu.

You can then use the EDITBIN.EXE tool to change the subsystem:

Code: Select all

EDITBIN /SUBSYSTEM:CONSOLE mylivecodeapp.exe
When you do

Code: Select all

mylivecodeapp -ui
From a windows console you should then get interactive input and output as you desire.

Note: Before anyone asks, it is *not* possible to have an executable which decides at runtime whether it should bind to a console or not - Console and GUI subsystem executables are run in different ways, and the OS needs to know which an executable is *before* it is launched.

Re: interactive console app

Posted: Thu Dec 14, 2023 1:58 pm
by keliko
Thank you for your help Mark.
I will try it