Embedding a .app file in a Cocoa application - cocoa

Is it possible to "embed" an application (like Preview, Pages etc) into a Cocoa application? I would like to allow a user to view a PDF in my app with options to show/hide annotations, change the zoom level between page width and full page etc.
Is it possible to embed the Preview app within my Cocoa application? Couldn't find anything in the document or on the Internet which would suggest I could, so not sure.
Thanks.

You can embed an other application in the bundle of your application.
You will usually do this to embed an other application you also created, like an helper, or a daemon. It sometimes is helpful, but doesn't seem to be what you want.
If you want to let the user play with a pdf inside your application, read the PDF Kit Programming Guide.
Especially look into the class PDFView. You can drag and drop an instance into your xib, and load content into it with this code (from Apple documentation):
PDFDocument *pdfDoc;
pdfDoc = [[[PDFDocument alloc] initWithURL: [NSURL fileURLWithPath: [self fileName]]] autorelease];
[pdfView setDocument: pdfDoc];

You cannot embed an application into another application. You can bundle it into your bundle, but it remains a separate application that will run separately. (And in the case of Preview, you'd be violating Apple's copyright, plus it'd be pointless since it comes with the OS anyway.)
When you want to do something that another application does, you have several choices:
Use the framework(s) that that application is based on, where applicable. Only works for apps like Preview and TextEdit that are little more than application hosts for OS frameworks. Preview = ImageKit + PDFKit; TextEdit = AppKit text-rendering classes; AppleScript Editor = OSAKit.
Farm out the job to the application. For example, you could generate the PDF as a file somewhere and open it in Preview (the one that came with the OS).
Reinvent the wheel. Can be undesirable for obvious reasons, but can also be preferable if the existing wheel is overkill or not really a good fit for your application.
In this case, I'd go with PDFKit.

Related

Nesting an application inside OS X subview

I'm looking for a way to embed another application into my own view.
The business reason is that the company has many small Electron apps (basically a small portable web program with a self-contained browser) that the company wants to embed inside an OS X program. These Electron apps would ideally integrate and display inside a subview seamlessly, so they look like little web frames inside our larger program.
I think programatically it would be easiest to open another program as a subview, but I'll take whatever I can get. Maybe even capturing it's NSWindow somehow. (Electron source is available so it is easily discoverable.) Maybe a way to dock the other program inside mine, or (getting more desperate) finding its view and sending commands to constrain it's size and location on top of mine.
So far all I've found says it is not really possible. I've found I can take the more desperate course. I can launch a process, find its view, and position it inside a spot on my display; when the window is moved or the content is scrolled send messages to move the other window. But that isn't really integrated, the menu stays separate, etc., but I cannot incorporate it.
Any ideas or helpful implementation details?
EDIT 1: Thanks for those responses. How about if we could have the electron apps expose their NSWindow somehow? Could that be leveraged? I'm thinking the application could send messages and (somehow, not sure exactly) to set the parent window inside this one. In Windows API it is much easier since you can call SetParent on anything, even items inside different processes. But Cocoa seems more difficult.
This isn't really a thing you can do in Mac OS X. Applications are not "composable" in the way you're hoping for - while it is possible to share a view with a subprocess under certain very specific circumstances (e.g, Safari or Chrome tab renderers), this requires the subapplication to be written in a very specific way to permit that. It's not something that would be feasible in the situation you're describing.
If you have access to the source of these Electron apps, consider combining them into a single overarching Electron application. Alternatively, if it's not possible for these applications to coexist within a single Electron app, you may want to consider using something like Chromium Embedded Framework to build your wrapper application; note, however, that this may require you to implement parts of the Electron framework yourself.
You cannot do that. Cocoa requires you to have only one NSApplication instance per UI app. So you will to fork/exec out new process and launch your applications.
If you can recompile the source code then you can create custom subclass of NSApplication and use that custom class in all the applications or you can create NSthread of other applications without NSApplication instance and go from there.

Incorporate document features into shoebox app

I'm working on a shoebox (library style) app for Mac OS X which will incorporate a page view like in Apple's text edit sample code.
Since this is a shoebox app, I cannot simply generate a document-based Xcode project because I want the user to generate a new "canvas" to work on from within the app. Rather than having the user open a new document through an open command they create a new object in the navigation sidebar, or simply open an already created document also from the sidebar. I'm looking at a UI similar to Albums in iPhoto.
Is there an easy way to incorporate the features of document-based applications (i.e. page setup, print setup etc) into a non-document-based app without having to implement each method individually? Would anyone have any suggestions as to how to implement this otherwise?

How to make a Cocoa Document-Based App launch with multiple different windows?

I have a document based app in cocoa with three xibs for different toolbox windows. At app launch I would like the toolboxes to be visible very similar to Adobe Illustrator where the toolboxes are visible on either side. To do this would I need to implement my own NSApplication?
No, when the document spawns, in its controller class (or the app delegate, dependent on who owns the toolbox windows) call makeKeyAndOrderFront: on each of the windows that you want to bring forward.

Finding the origin (URL) of a dragged and dropped image/text on a cocoa application dock tile on 10.6.8

I have just started coding an application in cocoa on OSX 10.6.8. It is well underway mostly thanks to threads I have found here. For the subtle functionality I am about to explain I've looked everywhere and really have given it my best without success. Here is the situation:
There is a browser (chrome for now but the solution should be adaptable to FF and Safari at least)
There is my chrome extension
There is my desktop app
Text/images can be dragged on my desktop app which then organises these items by source.
For a file/text from finder, except a web browser, tagging the file with its file path is the desired behaviour, for a text I simply put clipboard as the source. Now the issue is that files/text dragged from a web browser should have the website they were dragged from as the source. There is an easy fix for files: I have an extension which sets a buffer with the last active url so that all I need to check when a file is dragged is whether it comes from the chrome cache to tag it with the content of the said buffer. My problem is for text. The way text dragging is handled is by using a service which I declared in the .plist, There is a similar unanswered question on SO and a technique that seems as if it could work but it is only available for 10.7 on. I also found a link where someone managed to get the url for itunes songs dragged on his application using http://www.mail-archive.com/cocoa-dev#lists.apple.com/msg17452.html
Is there a solution to see the URL of a dragged image/text that could be handled using only cocoa?
The only other option I see is using the extension to inject javascript code into all webpages for handling the 'dragenter' event and send the content of what is being dragged together with the source URL to the desktop app via a custom protocol. I'm not too keen on script injection and I sense some complication down that path as well.
In any case thank you for your help!
Chocozor.

Which controls to use for a Mac OS X application?

I developed some app for iPhone/iPad, so I know the basic concepts of Cocoa programming. Now I need to write a Mac OS X application. If you help me to choose the right controls to use, I'll study the Apple documentation reference for them.
I need:
a control to show a grid of thumbnails (png images); I'll be able to change the order of the thumbnails by dragging and dropping them;
a control to show a single image (e.g. UIImageView of Cocoa Touch) with the possibility to drag and drop another image over the first one; besides I need to move and resize the second image by dragging its corners (as if corners were "anchors");
a control to browse the media files on the Mac (possibly iPhoto images, iTunes DRM-free songs, or simply the content of a given folder), like the media browser of iMovie.
I use Xcode 4.2.
You can use NSCollectionView and co. for this. Xcode has a sample application somewhere in documentation (browsing various images) as well as tutorial on how to setup NSCollectionView yourself.
Check NSImageView. You can basically find any Cocoa control alternative to Cocoa Touch just by changing UI prefix to NS.
There is no such control per-se (unless IKImageBrowser for backgrounds), but using aforementioned NSCollectionView and some API for media files you should get around pretty quickly.
Also check ImageKit framework for images (for browsing, viewing and modifying).

Resources