StrokesPlus Forum
                       
StrokesPlus Forum
Home | Profile | Register | Active Topics
Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Lua Scripts
 General Action Scripts
 Running a shell script without batch file
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

boombyte

Croatia
18 Posts

Posted - 01/09/2013 :  22:48:32  Show Profile  Reply with Quote
I have a simple script which uses cmdow tool to do this:

1. Read the output of cmdow /t which lists task names,
wHandles and captions of all windows currently shown on the taskbar

2. Search for a wHandle of a window belonging to a task named "pidgin",
which has a caption other than "Buddy List". That way it can identify
a chat window.

3. Fire cmdow /res /act command with the proper wHandle to restore
and activate the window

for /f "tokens=1,8,9" %%a in ('cmdow /t') 
do (if /i "%%b"=="pidgin" if "%%c" NEQ "Buddy" cmdow %%a /res /act)


Is there a way to directly run this script using Lua, without previously storing
it in some batch file and then executing that file via acShellExecute?

Edited by - boombyte on 01/09/2013 22:54:13

Rob

USA
2516 Posts

Posted - 01/10/2013 :  00:04:51  Show Profile  Visit Rob's Homepage  Reply with Quote
You can use Lua's io.popen to pipe the output of cmdow /t into your script and let S+ do the rest:
--return the output from cmdow as a file handle from which the Lua script can read the lines
f = assert (io.popen ("c:\\users\\rob\\downloads\\cmdow\\cmdow.exe /t")) 
local iLine = 1 --set the line count to 1
for line in f:lines() do --for each line returned by cmdow
	if iLine > 1 then --skip the first line, since it's just the table headers
		for token in string.gmatch(line, "[^%s]+") do --split the current line on space characters
			--the first token is the window handle, which is all we really need here
			local sEXE = acGetExecutableName(token, nil, nil) 
			local sTitle = acGetWindowTitle(token, nil, nil) 
			if sEXE == "notepad.exe" and sTitle ~= "Untitled - Notepad" then
				--only restore/activate the window if it belongs to notepad.exe
				--and the title does NOT match "Untitled - Notepad"
				acRestoreWindow(token, nil, nil)
				acActivateWindow(token, nil, nil)
			end
			break --don't bother looping through the rest of the tokens for the current line
		end 
	end
	iLine = iLine + 1 --increase the line count (really only serves to skip the first line)
end
f:close()

All you should have to do is change is the path to cmdow in the first line, "notepad.exe" to "pidgin.exe", and "Untitled - Notepad" to "Buddy List".
Go to Top of Page

Rob

USA
2516 Posts

Posted - 01/10/2013 :  00:07:24  Show Profile  Visit Rob's Homepage  Reply with Quote
To answer your question, no you can't do what you were asking without creating a batch file (at least that I found in my brief search). But with popen, you can bring things into Lua so I imagine this would cover most situations.

I just learned about popen now and I'm constantly amazed by what you can do with Lua!

Google is your friend
Go to Top of Page

Rob

USA
2516 Posts

Posted - 01/10/2013 :  00:20:20  Show Profile  Visit Rob's Homepage  Reply with Quote
By the way, the Lua Programming Reference is always great to checkout what's available (scroll down a little to the index and you'll see a table of everything).
Go to Top of Page

boombyte

Croatia
18 Posts

Posted - 01/10/2013 :  15:18:32  Show Profile  Reply with Quote
Yes, but Google is a much better friend to those who know what to search for. That is why I asked you. ;)

I must apologize because I forgot to stress some things regarding my question (I tried solving this before but didn't write anything down).
The thing is, I already checked the Lua Prog Reference and you actually can run a shell script without bat file by using os.execute. However, I want to run this silently (without having it open a console window), so I ended up with

acShellExecute("", "script.bat", "", "D:\\Scripts", 0)


Your solution is great because I was also wondering how to read a program output with Lua so I appreciate that. Thanks!

Unfortunately, io.popen command as well as os.execute doesn't take a "run hidden" flag, so it opens and closes the console window first. I want to avoid this for the sake of speed.

Do you think we could somehow force Lua to run the shell script hidden, like with style set to "0" in acShellExecute?

I simply cannot believe they designed the OS without a function to run a shell script without it's damn window popping up.
Go to Top of Page

Rob

USA
2516 Posts

Posted - 01/10/2013 :  15:29:05  Show Profile  Visit Rob's Homepage  Reply with Quote
Not that I'm aware of, the Lua process functions do nothing but call the system() or _popen() function as per the C implementation, which for Windows happens in a command window.
Go to Top of Page

Rob

USA
2516 Posts

Posted - 01/10/2013 :  15:38:52  Show Profile  Visit Rob's Homepage  Reply with Quote
..it seems others have this issue, and encounter the inherent limitations:

http://stackoverflow.com/questions/1597289/hide-console-in-c-system-function-win
Go to Top of Page

boombyte

Croatia
18 Posts

Posted - 01/10/2013 :  18:29:40  Show Profile  Reply with Quote
I've tried nearly everything. It seems the only way for directly injecting commands into shell is to create a process using C++ code and set it to run without window using creationflags=0x08000000. I am surprised how MS Windows is this stupid. In GNU/Linux (and every other system, I bet) you could do this thing fast in the windowless background, but Windows just has to "shell flash" you and slow the whole thing up. Unless you shove it all into a file and then go and slowly read it's contents from a disk every time, but even then you can't avoid automatically creating a whole new command line interpreter process.

Sorry for the rant. I guess it's all because of how Windows is built. UNIX sits on top its shell while Windows has to make an appointment with its own CL interpreter before you try to do anything.

If I could only know how to get the handle of a hidden window..
Go to Top of Page

Rob

USA
2516 Posts

Posted - 01/29/2013 :  06:46:51  Show Profile  Visit Rob's Homepage  Reply with Quote
Hey boombyte,

Check out this new function; your request inspired me to add it =)

http://www.strokesplus.com/forum/topic/611/added-acgetallwindows
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
StrokesPlus Forum © 2011-2018 Rob Yapchanyk Go To Top Of Page
Snitz Forums 2000