Create new document programmatically in Cocoa - objective-c

I'm wondering if it is possible to send a message (programmatically) to a document-based application to create a new document with some special content inside it. I'm creating two applications and the first has to call the second one sending it some data.
I've tried with the Apple Events but it doesn't seem to work.

There are a number of ways to do this. If you control both applications, probably the simplest is to post a notification to the global notification center from one application, and have the other app listen for it. AppleScript is another way, but requires a bit more work to set up.

Related

How to create a file in a mac app

I am new to programming mac apps in Objective-C and need help with this. When I control click and drag to first responder from a button it brings up a box from which you can select some predefined functions that run when the button is clicked. On of the functions says newDocument. I was wondering if this means that it creates a new document (file) and if so how to actually make it create the file. I tried selecting newDocument but when I ran the app it said this: 2016-03-16 15:39:57.889 Test[26619:4382721] The (null) type doesn't map to any NSDocumentClass. My button is simply named create file and has no code attached to it, only the predefined function.
The method you are seeing, newDocument, is part of the Document Architecture. If you wish to use it you need to study the documentation, the particular function newDocument creates a "document" within your app along with an associated "window" rather than a "file" per se.
It sounds like you really need to read the Cocoa Event Handling Guide to learn how to handle a mouse event, and the File System Programming Guide to learn how to read & write files.
HTH

Reveal (or Snap) Windows 8 App when system receives push notification

Can anyone enlighten me as to how one would be able to trigger a "non standard" realtime notification on a Windows 8 machine? My objective here was simply to find a slick method through which the end user is presented with an enhanced notification slide displayed via a slide in/5 second fade out dialog box.
Best example I can think of to illustrate what I am trying to achieve would be thinking of it like when the charms bar is presented which can automatically fade away. I simply require that this enhanced notification layer is somewhat customizable in order for me to provide summary content along with 3-4 buttons a user can press directly. This notifications needs to be triggered automatically upon the system receiving a push notification.
I have loomed into various "notification" methods such as default windows notifications, offline Google Apps notifications,... problem however is that they all seem not to allow me to customize the notification window to meet my needs.
Any suggestions / examples would be very helpful.
BTW - as an alternative solution - is there simply a way that I might be able to have Windows automatically load and "snap" into view the contents of a specific Windows app containing my content to the left or right side of the screen (upon receiving a push notification vs displaying the standard toast bubble?
Are you looking for something like growl?

How can I make a copy of an already created window?

So I am making an xcode application for OS X, and so far things have gone well. However, right now I am at a roadblock. My app is mostly one window, and I need that window to be created multiple times, in the way that apps like Safari, TextEdit, and all those ones have the File>New function. I am new at Xcode, and I was wondering if there was an easy way (hopefully 3-4 lines of code) to do this. If someone could explain that to me that would be great.
Thanks!
Start a new OS X project and choose "Create Document Based Application" . This will create a NSDocument subclass.
If you want to use Core Data , choose that also. This will create a NSPersistentDocument subclass.
This will give you the basis of your application with all the functionality you ask for.
Have a look at Document Based App Programming guide and NSPersistentDocument Class Reference
Whatever you do, dont try to bake your own. There lies madness.
The project template sets up the info.plist correctly to make this work.
Whatever I tell you, it won't involve only "3-4 lines of code," unfortunately.
If you're working from a non-document-based application, you'll want to factor out the code that creates your one window. If you're not using a window controller in concert with your window, you should implement one. Then you can implement an action that will create a new instance of your window controller, and so a new window. Of course, you'll need to add more infrastructure if you're interested in saving information associated with a window to a file.
Which leads me to your other option, which is to create a document-based application. The big advantage here is that, if you're interested in saving the window contents to a file, the infrastructure for doing so is provided. This involves starting over to some extent, but since you've already got the guts of your application working, you'd simply transfer those guts to the document subclass that is provided for you. You'd also need to provide to your project meta information describing the kind of document it is, including the file extension describing it.
Best wishes to you in your endeavors.

Getting process list and hiding a specific app

How do I get a process list (in a popup button), and then when the user selects the application, can I hide/kill/minimize/quit the application?
-[NSWorkspace runningApplications] will give you an NSArray of NSRunningApplication instances representing currently-running processes. I'm not exactly sure what causes a program to be excluded from that list, but it does include any application that the user has launched from the Finder. It also includes a couple of things (the Finder itself, and the loginwindow process) that you don't want to mess with, as well as faceless applications.
You can filter those out using filteredArrayWithPredicate:; the objects you want to keep have an activationPolicy of NSApplicationActivationPolicyRegular.
Once the array is filtered, you can search it, using bundleIdentifier, bundleURL, or localizedName to find the app you want. Then send hide or terminate as you like to that NSRunningApplication instance.
Apple has a sample project that demos all this, called AppList.

Locking a screen in 10.6

How would I go about locking a screen like Keychain does, meaning preventing all access to Dock, menubar, desktop, etc. Basically just a black screen that I can add a password field to, for the user to return to the desktop? I am well aware of the Carbon method, but I want the NSApplication method because this is an all Cocoa application.
Thanks~
If you can get away with not writing this code yourself, all for the better. It is usually a terrible idea to write your own code to lock the screen, considering the number of vulnerabilities that have been found in screen locking code over the years. If you have a Carbon call that can do it, go ahead and use that... don't worry about the "purity" of your Cocoa code.
However, if you decide to write this yourself, here's what you do:
First, capture all the screens using CoreGraphics. See: http://developer.apple.com/mac/library/documentation/GraphicsImaging/Conceptual/QuartzDisplayServicesConceptual/Articles/DisplayCapture.html
Next, create a new NSWindow and put it in front of the window that's used for capturing the screens. You'll have to call a CG function to get the "order" of the black window covering each screen, and order the new window in front of that. Normally, the black window has an order so far forward that everything is behind it. Put a password field in the window. Do NOT use an ordinary text field or write your own code for password input. The password input field has a ton of special code in it so you can't copy text out of it, and other programs can't listen to keystrokes while you're typing into a password field. So use the one that Apple provides.
Last, put the computer in "kiosk mode". This mode allows you to disable alt-tab, user switching, the menubar and dock, and even the ability to force quit. See: http://developer.apple.com/mac/library/technotes/KioskMode/Introduction/Introduction.html
It's not a lot of code, it just uses a few different APIs so you'll spend most of your time bouncing between API docs. I suggest writing the screen lock code as its own application (just add a new application target to your Xcode project) and then put the screen locker inside your application bundle. This used to be (as of 10.4) how Apple Remote Desktop implemented the "Lock Screen" functionality, but I can't find the app anymore.
I believe the Cocoa replacement to the SetSystemUIMode API was not introduced until 10.6.
If you can live with Snow-Leopard-only code, the answer is - setPresentationOptions: on NSApplication.