last day (15 days later) » 

9:15 AM
2
A: Detect paste on NSTextField

vauxhallCombining 2 answers found here I was able to find a workaround for me, but it was necessary to subclass NSTextField, NSTextFieldCell and NSTextView. This solution consists of 3 steps and it is using Swift, but could be adapted to Objective-C. Swift 5 1. Subclass NSTextView, this is where the actu...

 
Can you please tell me how this works and where can I place this code
 
@MacDeveloper I was under the impression my answer was detailed enough. Could you be more specific on what part of this answer are you having trouble understanding?
 
How can I link my NSTextfield subview I added to my NSTableViewCell to this TextField subclass. I might be asking a dumb question but I couldn't understand how it links my textfield to this
 
@MacDeveloper thank you for clarifying your question, not dumb question at all, maybe I could improve my answer to make it clearer. But answering your specific question: your need to subclass that NSTextField you added to the NSTableViewCell. In my example I subclassed it and named it TextField. Use a name of your choice, but it is important to subclass it so that you can override it's cell class, i.e.: MyNSTextField.cellClass = MyNSTextFieldCell.self.
 
I made below change to my cell class. class MyTableCellView: NSTableCellView { var aTextField: TextField? override init(frame frameRect: NSRect) { super.init(frame: frameRect) aTextField = TextField(frame: frameRect) aTextField?.drawsBackground = false aTextField?.isBordered = false self.addSubview(aTextField!) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } But still I am missing something
 
9:15 AM
@MacDeveloper It seems the problem may be you are using a different init() for TextField. On my example I use init() but your code needs init(frame frameRect: NSRect). I updated my answer addressing this issue, specially point 3, check the new init and try updating your code with it.
 
override func fieldEditor(for: NSView) -> NSTextView? {
textView
} is something missing here?
 
No, it should be enough. Can you put a breakpoint in Xcode on the line textView. Just to make sure it is being called?
Also put a breakpoint on the class TextView, on the line: var content = NSPasteboard.general.string(forType: .string) ?? "". Also to check if it gets called at all.
And finally also a checkpoint on every time you have this line: TextField.cellClass = TextFieldCell.self ... to se if any of those are being used also.
 
 
3 hours later…
12:17 PM
Breakpoints are working as expected
 
 
2 hours later…
2:32 PM
@MacDeveloper all of them? then it should be working now, If you get the breakpoint in var content = NSPaste.... then that is a paste operation. Is that one working?
 
 
2 hours later…
4:51 PM
@vauxhall var content = NSPaste.... , here breakpoint is not working
 
I see, but the one inside .. fieldEditor(for: NSView) ...is working, right?
Another question: the breakpoint in var content... is not working, but is the pasting working, I mean, does it paste something?
 
Yeah breakpoint here override func fieldEditor(for: NSView) -> NSTextView? {
textView
} is working
 
and is something being pasted on the text field even though the breakpoint in var content... is not being called?
 
No its not being pasted, that's the problem
I just copied a text from my notes and tried to do cmd V after I started editing my row
 
5:07 PM
ah ok, yes, this answer is only to intercept a Paste. But not to make it actually paste.
is paste working anywhere else in your app?
in another text field
 
No its not working
 
ok
 
MY idea is to be able to paste in my row
 
is possible, did you make your app from scratch on code, or are you using storyboards too?
 
I made my app from scratch programatically without story boards.
 
5:09 PM
to enable paste you need to add a menu to the app. A NSMenu. It is a bit tricky to do it with code. Because it requires a lot of boiler plate.
but I always do it with code too.
do you already have a menu?
I mean a menu like this, on the status bar, this screenshot is from Safari:
 
Yes I already have NSMenu. My app is a menubar app where when we click on one of the menu I am opening window with tableview
 
Do you have "Edit" > "Paste" in your menu, with the shortcut "CMD V" ?
 
Oh sorry I don't have this menu for my app. I created NSMenuitem which resides in nsmenubar near wifi
 
you will need to add it. Apple will reject your app if you don't have it
and it is needed for all the shortcuts. Like: "CMD V" and "CMD C", etc
 
How can I add it I removed main window
 
5:23 PM
Maybe the best option is if you look for a simple example or tutorial to add a main menu to your app. It is not too complicated, but it is a lot of code. I can show you some of my apps, they are open source so you can look at the code no problem, but it might be a little confusing because it is adding a lots of things and doing many more things than what you need.

But feel free to look at this example too and copy/paste as you need:

The whole project is here:
https://github.com/appthenews/thenews/tree/main/macOS
if you only deleted the window but not the storyboard the original menu should be there still, you might need to connect it only. If the original menu of the storyboard is still there that one is enough
 
I deleted the entire Storyboard
 
maybe start a new project on xcode and copy all your code to the new one
and don't delete the storyboard
those menus are very tricky to code
 
Yeah thinking to do that. I did lot of scratch work like cocoa bindings and all other stuff programatically. But I think UI is much better
 
if you have the patience and the time, then continue doing it all by code. But my first time it took me weeks to understand. So it all depends on how much time you have. Doing it by code is complex but you can customize a lot of the functionality.
But definitely check some small tutorial and a few open source projects. The one I showed you maybe is not very clear, but could help.
 
5:39 PM
As you said I created a new project and copy pasted all my existing code. It works and even copy paste works
But the concern is I don't want icon in launchpad. I mean the icons near Finder,safari
 
I have done it, it is possible
I don't have code to show it, but this example may help you, in general some of these answers might be what you need: stackoverflow.com/a/68057340/4691224don't
"appkit removing app icon from dock"
 

last day (15 days later) »