Looking for example of how to display text in an NSWindow programmatically? - nswindow

I am trying to display text in a HUD style semi-transparent window. I have got the code for the window all set up, but I have been scouring the documents for two days now and haven't come up with a way that works to actually display text in the window. Since I am using AppleScriptObjC in Script Debugger, and not Xcode, I'd rather do this programmatically and not have to switch to Xcode and use IB. (I did spend some time messing around with IB, but it is not very intuitive to be honest. So I thought I would check this form before going through the guides on how to get started using IB).
So I was given some advice to "create an NSTextField and add it to your window's contentView". So I have tried many different set ups of trying to init a NSTextField (And NSTextView), and I may have been able to get that part correct, but getting the text to actually display in the window has been a bigger challenge than I expected. I have included a code snippit of the code I am using to generate the window.
tell (NSWindow's alloc()'s ¬
initWithContentRect:{{theWidth, theHeight}, {640, 480}} ¬
styleMask:NSBorderlessWindowMask ¬
backing:NSBackingStoreBuffered ¬
defer:true)
setOpaque_(yes)
setAlphaValue_(0.5)
setBackgroundColor_(NSColor's grayColor())
setReleasedWhenClosed_(yes)
setExcludedFromWindowsMenu_(yes)
orderFrontRegardless()
delay 1
|close|()
end tell
My hope is to be able to get an NSText View in that Window in order to display some text in it. So far I haven't come close. The errors I generally get are about "unrecognized selector sent to instance". So it is pretty obvious that I am doing something wrong. I hope there is an easy way to accomplish this that I haven't yet come across.

It sounds like there isn't a target for some of the methods. In a tell statement, usually the target is implied, but sometimes AppleScript can't figure it out. You also don't get the newer method syntax, so I've had better luck just specifying the target for everything - also note that object properties can usually be set directly instead of using their setter method.
I can't tell from your snippet, but you also need to add the textView to the window's contentView, for example:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
# create a text view
tell (current application's NSTextView's alloc's initWithFrame:{{20, 20}, {600, 440}})
set theTextView to it
end tell
# create a window
tell (current application's NSWindow's alloc()'s ¬
initWithContentRect:{{200, 600}, {640, 480}} ¬
styleMask:(current application's NSBorderlessWindowMask) ¬
backing:(current application's NSBackingStoreBuffered) ¬
defer:true)
set theWindow to it
set its opaque to true
set its alphaValue to 0.5
set its backgroundColor to (current application's NSColor's grayColor)
set its releasedWhenClosed to true
set its excludedFromWindowsMenu to true
end tell
theWindow's contentView's addSubview:theTextView
theTextView's setString:"this is a test"
theWindow's orderFrontRegardless()
delay 5
theWindow's |close|()

Related

Random Godot beginner-question: en-/disable CheckButton

how can I enable or disable a CheckButton via code? Is there any simple way like "input pickable true/false" I use with collision shapes? I played around with control focus modes but didn't succeed.
For example: I'd like to have a timer starting when button down / holding down and after a certain time the button shouldn't react at release anymore. Just as if it was never put down in the first place...
There must be a simple command for that, right?
Edit:
Trying
my_button.disabled = true
leads to the error
Invalid set index 'disabled' (on base: 'GDScriptNativeClass') with value of type 'bool'.
It sounds like you're looking for the pressed property.
my_button.pressed = true
The documentation isn't very clear on the usage but it does what you're describing. https://docs.godotengine.org/en/stable/classes/class_checkbutton.html

Vimperator: Follow hint in new tab and switch to it

I am trying to find a strategy in the Vimperator-logic for opening a link to a new tab and switch to it immediately.
I guess this might be a TMTOWTDI.
My first approach would be to start off with a :command sequence. Unfortunately F - Follow hint in background tab - has no equivalent in the command mode. The best solution for me would be without any change in default behaviour whatsoever.
Another approach might be to combine the default F with switching to next tag gt. This would have to include to pass the parameter for F, say 10 for the tenth link of the document. Unfortunately F10gt does open the tenth link but without switching to the newly opened tab.
A third approach does come to mind when using focus elements: ;y10 yank the destination link, :tabopen + paste clipboard content.
Any ideas of what is doable and the easiest?
This functionality exists as a mode of the hint function. Pressing ;t
will produce the desired result. If you want to map it you can also access the javascript directly.
:js hints.show("t")

AvalonEdit :: How do I preserve current state in the UndoStack?

I am currently working on implementing AvalonEdit in an HTML WYSIWYG/"Source Code" side-by-side editor. When the user switches from Design Mode (a RichTextBox) to Source Mode (AvalonEdit TextEditor) the XAML from the RTB is converted to HTML and written to the TextEditor.Document.Text property.
This apparently wipes out the undo stack.
How can I push the state of the Document to the UndoStack so the user can "go back"? I tried wrapping the write operation in a RunUpdate() IDisposable, but that didn't work:
using (var _ = TextEditor.Document.RunUpdate())
{
TextEditor.Document.Text = html;
}
Any assistance would be greatly appreciated.
Since this is a couple years late, I'm not sure if it answers the question directly. However, the current release of AvalonEdit functions such that setting TextEditor.Text will clear the undo stack, but modifying TextEditor.Document.Text will not. (This runs counter to the behavior observed by the asker, so perhaps it has changed in the couple years since). Looking at the source code, TextEditor.Document.Text appears to execute code equivalent to
this.Replace(0, this.TextLength, value);
so perhaps a similar call would work even on older versions of the library.

see variable definition by tooltips in vim

I need to frequently check definitions of variables/functions. I can jump to definitions of variables/functions by ctags with gd,gD, ctrl ] etc.
But by it, I jump from my current position and lose context of current position. Is there any way I can check definitions in tool tips like we see in ctrl p ctrl n in insert mode. It would really help in quickly understand and browsing code.
Thanks,
Supposing you already have an up-to-date and reachable tags file, you can see the definition of the variable or method under your cursor in a preview window with:
<C-w>}
See :h preview-window.

How to view Session Variables in Visual Studio 2008 Debugger?

Usually using Visual Studio's debugger is a breeze. Scanning through Locals quickly shows the values of variables, etc. However, I'm at a loss how to find out the values contained in session state variables? Can anyone give me a hand?
Let's say I put a breakpoint right after:
Session["first_name"] = "Rob Roy";
How do I view the value contained in Session["first_name"] from locals?
It's pretty simple to inspect the session during debug. Just put a breakpoint in your code and just highlight your Session code (eg. highlight: Session["first_name"]) then right click and select QuickWatch....
This will setup up a watch on your Session for the value you had defined. You can also inspect other Session elements by adjusting the Expression field in the QuickWatch window and clicking the Reevaluate button.
In VS you can just put 'Session["first_name"]' in the Immediate Window and execute while the code is running. That will return the value that it holds.
If you can't find it go to: View > Other Windows > Command Window, or press Ctrl+W, A
It will look like this:
I know its a bit of a late reply but for anyone else who is interested, I hope this helps!
Isn't it HttpContext.Current.Session("..."), I ask as I haven't used ASP.NET for a long time.

Resources