Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AppleScripting creating XML and PDFs

AppleScripting creating XML and PDFs
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Aug 8, 2002, 12:35 AM
 
Ohkay, so i want to create an XML file with certain properties, that have already been defined, and give it a custom file extension and icon customized for my application

By the way, I made the hit Gurgel Doll Production Factory

So, I want to make an XML file so when people move 'em around, the program can read them and then be like :"Oh, ohkay, I can read this!"

AND: Also, I want to create a PDF that is formatted in a weird way, like.
Two Columns: On the left side is a list of Doll attributes and on the right side is the picture of the doll. The thing is, the Doll picture is actually made up of three differnet pictures laid on top of each other.


Can anyone help? Remember, I can only use AppleScript because that is the only language I know.

A
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Aug 8, 2002, 12:55 AM
 
To create an XML file, just use a repeat loop or whatever, add XML tags to different attributes. You don't need any special library. As for creating PDFs from AppleScript, it can't be done without some extension. And besides, you don't actually write directly to a PDF, for example in Cocoa, you would draw this in an NSView, then get the PDF representation from that. I could whip up a nice hot cup of Cocoa if you need it...
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Aug 8, 2002, 08:32 PM
 
Can you give me some sample code, i would gladly give you credits in the program. Also, can you use Cocoa in a AppleScript app?
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Aug 9, 2002, 01:38 AM
 
I'm not too sure exactly what you want to do with your XML code; if it can be done using a simple loop there are others like Synotic who are better at AppleScript than I. So, I'll talk about your PDF/Cocoa issue.

You can use Cocoa in an AppleScript studio app, by either calling AppKit/Foundation methods directly (like 'set myAttrString to call method "alloc" of class "NSAttributedString"') or calling methods you have added to your application.

Because you want to have a funky two-column layout for your document, the easiest solution that comes to my mind is to create an HTML document (which I can do if neccessary), create an NSAttributedString from that, draw it in an NSTextView, then use the dataWithPDFInsideRect: to get the PDF data and write it to a file.

You could easily create the layered image by drawing each image in an NSView. Let's say you have different images in your application bundle, and wanted to draw them (I'm assuming they're all the same size, tell me if otherwise.) You could call this from your code:
Code:
- (NSString *)pathToDollImageWithFirstImageName:(NSString *)firstImageName secondImageName:(NSString *)secondImageName thirdImageName:(NSString *)thirdImageName { NSImage *firstImage; NSImage *secondImage; NSImage *thirdImage; NSRect dollViewFrame; NSView *dollView; NSData *dollPDFData; NSString * dollPath; firstImage = [NSImage imageNamed:firstImageName]; secondImage = [NSImage imageNamed:secondImageName]; thirdImage = [NSImage imageNamed:thirdImageName]; // Let's assume the first image is the largest dollViewFrame = (NSRect){0, 0, [firstImage size]}; dollView = [[[NSView alloc] initWithFrame:dollViewFrame] autorelease]; // Draw the images into the view [firstImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver]; [secondImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver]; [thirdImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver]; // Write the view's data to a temporary file. // It should really look for previous images and generate an individual name, but that can happen later. dollPath = [NSTemporaryDirectory() stringByAppendingString:@"DollImage.pdf"]; dollPDFData = [dollView dataWithPDFInsideRect:[dollView frame]]; [dollPDFData writeToFile:dollPath atomically:YES]; return dollPath; }
Then in your HTML file you could have:
Code:
<table width="100%%"> <tr> <td width="40%%"> <ul> %@ </ul> </td> <td width="60%%"> <img src="%@"> </td> </tr> </table>
Then generate the final HTML code in this method:
Code:
- (NSString *)HTMLCodeForDollAtPath:(NSString *)path withAttributes:(NSDictionary *)attributes { NSString *finalHTML; NSString *baseHTML; NSString *baseHTMLPath; NSString *attributeList; baseHTMLPath = [[NSBundle mainBundle] pathForResource:@"BaseHTML" ofType:@"html"]; baseHTML = [NSString stringWithContentsOfFile:baseHTMLPath]; // Generate the attribute list by enumerating through the dictionary, creating a <li> for each value, // with its key being the first item in bold. Not written because I don't know how you want the attributes set out. finalHTML = [NSString stringWithFormat:baseHTML, attributeList, path]; return finalHTML; }
And generate your PDF using this:
Code:
- (void)createDollPDFFileAtPath:(NSString *)filePath usingHTML:(NSString *)HTML { NSPrintInfo *printInfo; NSTextView *displayView; NSAttributedString *HTMLAttributedString; NSData *HTMLData; NSData *PDFData; NSRect rect; float margin; // This is so the PDF is the size of the default page printInfo = [NSPrintInfo sharedPrintInfo]; margin = 50.0; rect = NSZeroRect; rect.size = [printInfo paperSize]; rect.size.width -= margin * 2; rect.size.height -= margin * 2; displayView = [[[NSTextView alloc] initWithFrame:rect] autorelease]; // Generate the HTML attributed string HTMLData = [HTML dataUsingEncoding:NSUTF8StringEncoding]; HTMLAttributedString = [[[NSAttributedString alloc] initWithHTML:HTMLData documentAttributes:nil] autorelease]; // Draw the string in the text view [[displayView textStorage] setAttributedString:HTMLAttributedString]; // Get the PDF data from the view PDFData = [displayView dataWithPDFInsideRect:rect]; // Write the PDF to a file [PDFData writeToFile:filePath atomically:YES]; }
Hope this helps.
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Aug 9, 2002, 08:14 AM
 
WHOAW! That really does help, and i understand it better, if you want to be a part of helping me, as Synotic already has, just e-mail me and I can give you the source code, I am going out of town, though, so in five days, maybe..hmmmm, but I have uploaded what I want the PDF to look like,basically:

Gurgel Doll Production Factory Gurgel Doll Summary Page
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Aug 12, 2002, 06:50 AM
 
After re-reading the code, I've realized how bad the doll-drawing method is. I've no idea why I'm creating an NSView then converting it to a PDF; what I should be doing is initializing an NSImage, lock its focus, draw, unlock its focus and save its TIFF representation. Late nights and Cocoa don't go well together .
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:08 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2