• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Cocoa from the command line?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa from the command line?


  • Subject: Re: Cocoa from the command line?
  • From: "b.bum" <email@hidden>
  • Date: Sun, 25 Jan 2004 00:01:55 -0800

On Jan 24, 2004, at 3:11 PM, Jens Bauer wrote:
> I've been thinking a lot about this; it keeps coming back to me, as
> I've made a cgi-script, which assembles my Web-sites, and I generate
> printable HTML-pages from the same template, that is used on the main
> Web-site.
> But I want to do more. I want to make RTF and PDF files available to
> the user too.
> So I thought: "Maybe I could just make a WebView, which I load my
> printable HTML-page into, and then I save it in a file" (See
> CocoaDevCentral).
>
> Would this be possible directly from the command-line ? -It'd be
> silly, if I had to write everything from scratch.
> If it's not possible from the command-line, is it possible to use the
> "open" command to generate a connection between the command-line tool
> and the HTML-to-PDF/RTF/RTFD program?
> And last question: would this work from a CGI script too ? (It's not a
> must for me, though).

Totally possible. It is just a matter of starting up Cocoa correctly.
You will likely be better off with a full app wrapper and just
execute the contained executable directly -- that will give you all of
the advantages of the full NSBundleized app runtime.

In any case, enclosed is an example of starting up the AppKit and
displaying a window with a couple of buttons. It is written in Python
and uses the PyObjC bridge, but translation to Obj-C is trivial
(anytime you see something like
initWithContentRect_styleMask_backing_defer_, just substitute ':' for
':' and you'll have the ObjC method name).

Given what you want to do, PyObjC would be particularly applicable. It
gives you full access to Cocoa/Foundation while giving you the full
HTML processing library of Python. As well, Panther includes a python
wrapper for the CoreGraphics API that allows for some very powerful
manipulation of PDF. There is even an example of "take a hunk of
HTML and render it to PDF" in /Developer/Examples/Quartz/Python/.

have fun,
b.bum

import objc
from Foundation import *
from AppKit import *

class AppDelegate (NSObject):
def applicationDidFinishLaunching_(self, aNotification):
print "Hello, World!"

def sayHello_(self, sender):
print "Hello again, World!"

def main():
app = NSApplication.sharedApplication()

# we must keep a reference to the delegate object ourselves,
# NSApp.setDelegate_() doesn't retain it. A local variable is
# enough here.
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)

win = NSWindow.alloc()
frame = ((200.0, 300.0), (250.0, 100.0))
win.initWithContentRect_styleMask_backing_defer_ (frame, 15, 2, 0)
win.setTitle_ ('HelloWorld')
win.setLevel_ (3) # floating window

hel = NSButton.alloc().initWithFrame_ (((10.0, 10.0), (80.0, 80.0)))
win.contentView().addSubview_ (hel)
hel.setBezelStyle_( 4 )
hel.setTitle_( 'Hello!' )
hel.setTarget_( app.delegate() )
hel.setAction_( "sayHello:" )

beep = NSSound.alloc()
beep.initWithContentsOfFile_byReference_(
'/System/Library/Sounds/Tink.Aiff', 1 )
hel.setSound_( beep )

bye = NSButton.alloc().initWithFrame_ (((100.0, 10.0), (80.0,
80.0)))
win.contentView().addSubview_ (bye)
bye.setBezelStyle_( 4 )
bye.setTarget_ (app)
bye.setAction_ ('stop:')
bye.setEnabled_ ( 1 )
bye.setTitle_( 'Goodbye!' )

adios = NSSound.alloc()
adios.initWithContentsOfFile_byReference_(
'/System/Library/Sounds/Basso.aiff', 1 )
bye.setSound_( adios )

win.display()
win.orderFrontRegardless() ## but this one does

app.run()


if __name__ == '__main__' : main()

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Cocoa from the command line?
      • From: "b.bum" <email@hidden>
References: 
 >Cocoa from the command line? (From: Jens Bauer <email@hidden>)

  • Prev by Date: Re: programmatically instantiating and displaying NSWindow
  • Next by Date: Re: Cocoa from the command line?
  • Previous by thread: Re: Cocoa from the command line?
  • Next by thread: Re: Cocoa from the command line?
  • Index(es):
    • Date
    • Thread