-1

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.

Chris Norman
  • 225
  • 2
  • 11
  • When I run the full script (I only posted a snippet) in Script Editor or Script Debugger I get the NSWindow to display. You can call the AppKit framework from within Script Editor or Script Debugger. I can post the full script if you'd like to try it out. – Chris Norman May 27 '19 at 21:10

1 Answers1

3

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|()
red_menace
  • 3,162
  • 2
  • 10
  • 18
  • I know it has been a few weeks, but I have worked on this a bit and have some additional questions. Should I post a new question or just ask it here? – Chris Norman Jun 16 '19 at 20:17
  • Unless you just need clarification or something related to this particular question, you should probably ask a new one. – red_menace Jun 16 '19 at 20:37