AppleScript: How to create and communicate with a singleton Terminal.app window - python

I want to programmatically create one and only one Terminal.app window that's uniquely identified by a literal Terminal.app window title (let's call it 'MyConsole1') and send bash commands to said window for the life of the Terminal.app process.
Can this be done with Terminal.app, and if so, how?
Details
My program probably needs to check if 'MyConsole1' is already open before I go try to create a new one. And of course, start Terminal.app if it's not already running. I also want the commands issued to and over the life of 'MyConsole1' (by my program) to be bash-command-history retrievable by a human user; ie, the user can interact with 'MyConsole1' if desired. I'm not concerned about user-vs-program "conflicts" that might arise for use of the 'MyConsole1'.
I presume I need AppleScript to do this. I will be running the AppleScript as part of a much-larger Python application, possibly via a method like this, in case that matters.
I see how Terminal.app window/tab title(s) can be set like this, but this solution doesn't work for my requirements above. Further, I only want 1 tab in the window, and the title to apply to the entire window, and not just the tab.

I'm not sure if this is what you mean, but you can try this:
tell app "Terminal"
activate
do script ""
end tell

Related

Multiple conemu windows - how to differentiate?

Windows 8.1 , ConEmu 170316 [32] {Preview}
I have multiple Conemu instances running, where each instance equals one "workspace".
I would like to be able to switch to this workspaces with autohotkey or pywinauto. However they require a criterion for selecting the right window, and usually I employ a combination of window title and or window class type.
Is there any setting in conemu that can help me achieve this criterion identifiation for window selection? If there isn't, I will have to write the PID down somewhere when I start the conem windows, then read it to bring up the right window at window activation time.
I can say only about pywinauto. When you call app = Application().start('ConEmu64.exe') pywinauto already remembers process PID and every new WindowSpecification object assumes this PID. Of course, it's true only if you're controlling start of ConEmu (or any other app). More details can be found in the Getting Started Guide.
Method app.connect(title="some unique tab name") will also remember the PID in app object. But if there are few instances with the same title, you need to disambiguate it using found_index=0 criterion, for example. Or right click on the tab and choose "Rename tab..." context menu item which changes the window title.
I took a short look at ConEmu. Toolbars and tabs are visible even to Spy++. So the most of actions can be automated. Just not sure about console inside a tab. If you want to type some commands, it's much better to use standard Python module subprocess because GUI automation for console programs looks very very strange. ;)
As per faq (ty Maximus) - see option 1:
1) Use -title “Window name” switch to explicitly set window title of new ConEmu instance. The example below starts new ConEmu window with title My server and ssh to your.server.com inside. Does not matter if you run another tab, or several tabs from task, the ConEmu window title remains My server. So you may rely on the title for selecting the window with class name VirtualConsoleClass.
Option 2 is also valid. I'd have to (1) create a config, and (2) hardcode the hashed id in ahk / pywinauto. Just using the window title name seems the right thing to do in my case, the app id setup seems overkill
2) Windows 7 introduced AppUserModelID. ConEmu uses executable path name and some switches (like -config, -loadcfgfile, -quake) to create a hash to form AppID, which you may see in the About / SysInfo. Current version shows 1d5372066082f23b41ba6aa278e56e9d::163. The trailing ::163 depicts ConEmu internal protocol version which may (and most probably would) be changed in the future. The hash itself is expected to be unchanged. You may query the ID from running ConEmu process using Windows API function GetApplicationUserModelId.
Thank you Maximus!

How to access the Maya main menu and toolbar?

I use Autodesk Maya 2016 + Python.
I had a problem. I need to replace the functions of standard menu items and toolbars. For example, you must add the function to save a recording function. And because different people use different functionality, you have to change everything at once. Someone presses Ctrl+S, others go to the menu File->Save, and some press icon on the toolbar. In all areas it is necessary to replace the functionality. I understand that the problem boils down to in order to access the menus, toolbars and keyboard shortcuts. Next, find the associates item on the estate to know what function is now called. Then replace it with their cause and at the end of the function that invoked earlier in this menu.
how to get Maya main menu (like File and other)?
how to get Maya main toolbar?
how to get Maya hotkeys?
Maya is based on QT. So just need to get the objects, and then have the standard means of QT can do everything.
How to do it?
All three ways of doing this call the runTimeCommand named SaveScene. Override that command and you have hijacked all of the three ways you describe. This is a bit tricky since the command is marked with the flag -default which makes it impossible to change them on the fly. You can hook them where they are first defined. They are defined in the file:
Mayadir/scripts/startup/defaultRunTimeCommands.mel
Copy this file to your user profile maya script directory or studio script directry. These will get precedence of factory script due to resolution order. Do not overwrite the factory file. Then change -command in the lines,
runTimeCommand -default true
-annotation (uiRes("m_defaultRunTimeCommands.kSaveSceneAnnot"))
-category ("File")
-command ("checkForUnknownNodes(); FileMenu_SaveItem")
SaveScene;
to something else. Best would be just to add a hook here.
Obviously, you can also change the button/menuitem and hotkey too. So what you do is run following mel (it's easier to do this in mel since the bulk of the commands are built that way, port to python or c++ if you must):
runTimeCommand
-annotation "Print the word \"Hello\""
-command ("print \"foo\"")
MySave;
nameCommand
-annotation "Print the word \"Hello\""
-command ("print \"foo\"")
MySaveNamed;
buildFileMenu();
menuItem -e -c "MySave" "MayaWindow|mainFileMenu|saveItem";
iconTextButton -e -c "MySave2" openSceneButton;
hotkey -keyShortcut "s" -ctl -name ("MySaveNamed");
Warning: The hotkey will be permanent in prefs, until you change it back or reset prefs.
Please note: There are at least two other ways user could save and you could not have full control of those even if you wanted to.

extend Pyinstaller executable visible range

I had a python code and I used Pyinstaller to make it a stand-alone .exe executable.
In my code, I use "print" function to output result.
However, if the result is really long, (several screen page long) the result is cut short because the limitation of the console, I can scroll up but I guess the total length of the viewable text region is limited. (I am running my .exe in Windows)
Is there a way to extend the visible range so I can see all my output?
Thanks!
================
update:
I agree with #supremefist that it is the shell that is limit the visible range.
Is there a way to pass parameters to the shell so that when I double click it in Windows, the view range is extended.
Also, if it is possible, I would like to have my executable robust across different OSs. I am trying to write a small program and my target users maybe inexperienced computer users.
================
update2:
Now, I understand that Pyinstaller is only for Windows, previous update for different Oss is completely wrong.
The good news is that I switch to Qt and this problem goes away as I am now displaying my result in a window rather than a shell console.
I don't think the limit is related to PyInstaller, but to the constraints being set on your windows shell. You could try changing your shell settings by doing the following:
Open a windows shell by either running 'cmd' or by finding it under Accessories in your Start Menu.
Right click on the title of the shell window and select Properties.
Increase the screen buffer height setting under the 'Layout' tab to a large number. Something like 9999.
Re-run your program, you should see a much longer history of text.

Get content from open window in Linux

I want to collect data and parse it eventually from an open window in linux.
An example- Suppose a terminal window is open. I need to retrieve all the data that appears on that window. After retrieval, I would parse it to get specific commands entered.
So is it possible to do that? If so, how? I would prefer to use python to code this entire thing.
I am making a guess that first I would have to get some sort of ID for the open window and then use some kind of library to get the content from the window whose ID I have got.
Please help. I am quite a newbie.
You can (ab)use the assistive technologies support (for screen readers and such) that exist in the toolkit libraries. Whether it will work is toolkit specific—Gtk and Qt have this support, but others (like Tk, Fltk, etc.) may or may not.
The Linux Desktop Testing Project is a python toolkit for abusing these interfaces for testing GUI applications, so you can either use it or look how it works and do similar thing.
I think the correct answer may be "with some difficulty". Essentially, the contents of a window is a bitmap. This bitmap is drawn on by a whole slew of primitives (including "display this octet-string, using that encoding and a specific font"), but the window contents is still "just pixels".
Getting the "just pixels" is pretty straight-forward, as these things go. You open a session to the X server and say "given me the contents of window W" and it hands it over.
Doing something useful with it is, unfortunately, a completely different matter, as you'd potentially have to (essentially) OCR the bitmap for what you want.
If you decide to take that route, have a look at the source of xwd, as that does, essentially, that.
Do you have some sort of control over the execution of the terminal? In that case, you can use the script command in the terminal session to log all interaction to a file and then read and parse the file.
$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile
If the terminal is running inside of screen, you have other options as well. Screen has logging built in, screen -X sends commands to a running screen session (man screen).

How to interact through vim?

I am writing an editor which has lot of parameters that could be easily interacted with through text. I find it inconvenient to implement a separate text-editor or lots of UI code for every little parameter. Usual buttons, boxes and gadgets would be burdensome and clumsy. I'd much rather let user interact with those parameters through vim.
The preferable way for me would be to get editor open vim with my text buffer. Then, when one would save the text buffer in vim, my editor would get notified from that and update it's view.
Write your intermediate results (what you want the user to edit) to a temp file. Then use the $EDITOR environment variable in a system call to make the user edit the temp file, and read the results when the process finishes.
This lets users configure which editor they want to use in a pseudo-standard fashion.
Check out It's All Text!. It's a Firefox add-in that does something similar for textareas on web pages, except the editor in question is configurable.
You can also think about integrating VIM in to your app. Pida does this

Categories