I’ve written an Oxygene WPF application that I really want to port over to an OSX desktop app because I have users that would love it (in addition to the Windows app), but also it would be a great project to get my feet wet in OSX/Cocoa/Nougat.
The application is pretty simple overall, but there are some aspects of .NET that are second nature to me, but I have no idea where to look in Cocoa/Nougat, so I would appreciate some tips as to where to get started…
The application itself automates the retrieval and emailing of PDF documents. In addition, the app allows the user to view/print the PDF documents easily if desired.
In .NET, I simply use Process.Start() to both open and/or print the PDF documents to the default printer. Is there an equivalent class/method to do this in Cocoa?
The big question - what should I use for automating the sending of emails in OSX? In .NET, I use SMTPClient to connect/send emails via gmail. I want to do the same in OSX, i.e. I don’t want to script/integrate with an existing email client - I just want to send the emails via SMTP. What is built into OSX to do this?
NSWorkSpace.defaultWorkspace.open() would be the closest equivalent to Process.Start or "shell execute’.
For sending emails, i don’t believe Cocoa has a pre-fab API for that. A qick Google shows that most people reocmmendn using AppleScript to automate Mail.app, or indeed the same open() API fro above, such as
var mailtoAddress := NSString.stringWithFormat('mailto:%@?Subject=%@&body=%@',toAddress,subject,bodyText).stringByReplacingOccurrencesOfString(' ') withString('%20');
NSWorkspace.sharedWorkspace.openURL(NSURL.URLWithString(mailtoAddress));
(though the string replace seems a bit hacky, you’d probably want to call stringByAddingPercentEscapesUsingEncoding() instead, to be cleaner ;).
If you absolutely want to connect straight to SMPT, you *probably& need to look for a third party or open source library to use. This post has a few suggestions: http://vafer.org/blog/20080604120118/ (found via google just no, so i can’t comment on their quality)
Thanks, marc, for those few tips. Lets first focus on viewing/printing.
I am retrieving the PDF files via some existing ROSDK services I’ve written. Obviously the PDF is returned to me as a Binary stream. Can I view and/or print PDFs from a stream, or do I need to save the stream to a physical file first like I do in Windows?
That brings up another question - where is the accepted location on a Mac to store a temporary file?
Can I also print a PDF using the NSWorkspace class? Or would I have to use the NSPrintOperation class? Same question there, do you know if I can print from just a stream?
Once I get these two operations working, i’ll move on to the email functionality.
Good question. OS X has pretty extensive PDF support build in, so id not be surprised if there’s an easy way to display a PDF in your local app — but this is not an area i’m very familiar with, myself, i suggest Googling that.
If you do need to save it to a file, and pen in Preview, you can just call writeToFile() atomically() on the NSData object (binary data received form ROSDK is an NSData.
You can call the NSTemporaryDirectory() API function to get the base folder for temp files. IIRC recommendation is to create a randomly named subfolder (say with a GUID name).
I’ve done zero work with printing, but id expect that the Cocoa printing syste cna probably handle PDF, as iirc thats its native format.
This Stack Overflow might help with all of this:
It looks like PDFView is a standard Cocoa class, the PDFKit might have more useful stuff for you, as well (should all be in Quartz.PDFKit.fx).