 |
 |
NSMutableAttributedString Additions and HTML
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
OK, let's say I have a brand new NSMutableAttributedString...
NSMutableAttributedString *mutAttribString;
Now if you look at NSAttributedString Additions, you'll notice an initWithHTML:documentAttributes: method. Well I want to create an NSMutableAttributedString.. and then later on.. feed it some HTML. But if you notice, there's no setHTML: method in NSMutableAttributedString. So what am I to do? Is this an oversite? Am I looking in the wrong place? Also, I -cannot- use the initWithHTML method because I am trying to get this called from an AppleScript Studio app.. and it doesn't like initializing through the call method command. If you aren't familiar with AS-S, then ignore the last line, it isn't relevant.. except that I can't use the initWithHTML method.
Thanks a lot for any help
E-mail: synotic@mac.com
AIM: Synotic
P.S. Note that all of these things are in AppKit not Foundation if you want to reference the documentation...
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
If the problem is that calling -init doesn't work with that command, why not just create a wrapper method that does it for you? Like so:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">+ (NSAttributedString *)makeAttributedStringWithHTML  NSData *)data documentAttributes  NSDictionary **)docAttributes
{
return [[self alloc] initWithHTML:data documentAttributes:docAttributes];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">And then call makeAttributedStringWithHTML to create your string.
And if you want to feed an attributed string some HTML later on...you could create one (using a method similar to the one above, assuming it works) and then insert the string where you want it.
I'm not really much of an ASS guy, so I really have no idea about its features or limitations, but just guessing from what you described, that sounds like what you want. Hopefully.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Chuckit:
<strong>If the problem is that calling -init doesn't work with that command, why not just create a wrapper method that does it for you? Like so:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">+ (NSAttributedString *)makeAttributedStringWithHTML  NSData *)data documentAttributes  NSDictionary **)docAttributes
{
return [[self alloc] initWithHTML:data documentAttributes:docAttributes];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">And then call makeAttributedStringWithHTML to create your string.
And if you want to feed an attributed string some HTML later on...you could create one (using a method similar to the one above, assuming it works) and then insert the string where you want it.
I'm not really much of an ASS guy, so I really have no idea about its features or limitations, but just guessing from what you described, that sounds like what you want. Hopefully.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Yeah, I was figuring that I had would have to do that, but I was originally hoping against making the user download extra files. Thanks for the code though 
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong>[Yeah, I was figuring that I had would have to do that, but I was originally hoping against making the user download extra files.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Doesn't ASS make bundles? Just stick any "extra files" in there, nice and hidden. I mean, that's what bundles were made to do.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong>...Also, I -cannot- use the initWithHTML method because I am trying to get this called from an AppleScript Studio app.. and it doesn't like initializing through the call method command.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Are you sure? I tried out this piece of code in AS-S:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> set myAttrString to call method "alloc" of class "NSMutableAttributedString"
call method "initWithPath:documentAttributes:&quo t; of object myAttrString ¬
with parameters {"/Users/ibson/Desktop/Sample RTF File.rtf", null}
log (call method "string" of object myAttrString)</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">And it successfully logged the string contents of the RTF file. Could you please post exactly what code AS-S is refusing to use? Also, if you did include that wrapper file Chuckit posted, it would be compiled into your executable for your AS-S application--meaning no extra files would need to be downloaded. Hope this helps  .
[Edit: damn non-wrapping code blocks  ]
<small>[ 07-04-2002, 10:06 PM: Message edited by: Ibson ]</small>
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Ibson:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong>...Also, I -cannot- use the initWithHTML method because I am trying to get this called from an AppleScript Studio app.. and it doesn't like initializing through the call method command.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Are you sure? I tried out this piece of code in AS-S:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> set myAttrString to call method "alloc" of class "NSMutableAttributedString"
call method "initWithPath:documentAttributes:&quo t; of object myAttrString ¬
with parameters {"/Users/ibson/Desktop/Sample RTF File.rtf", null}
log (call method "string" of object myAttrString)</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">And it successfully logged the string contents of the RTF file. Could you please post exactly what code AS-S is refusing to use? Also, if you did include that wrapper file Chuckit posted, it would be compiled into your executable for your AS-S application--meaning no extra files would need to be downloaded. Hope this helps  .
[Edit: damn non-wrapping code blocks  ]</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Sorry, I meant the developer. Since usually AppleScript Studio devs aren't hip to the whole making their own .h/.m type stuff.
Thanks for the suggestions, I'll have to try it out.. I'll get my code soon, my computer was out of commission for like 2 days.. it wouldn't even mount the HD..
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
OK.. it's this.. I can't figure out how to pass NSASCIIStringEncoding to dataUsingEncoding... Some things I've tried:
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter "NSASCIIStringEncoding"
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (class "NSStringEncoding")
etc.. Any ideas?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong>OK.. it's this.. I can't figure out how to pass NSASCIIStringEncoding to dataUsingEncoding... Some things I've tried:
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter "NSASCIIStringEncoding"
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (class "NSStringEncoding")
etc.. Any ideas?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">NSASCIIStringEncoding is only a constant. It represents an integer value. Look it up in the NSString headers, and send the integer value as the parameter.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Ibson:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong>OK.. it's this.. I can't figure out how to pass NSASCIIStringEncoding to dataUsingEncoding... Some things I've tried:
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter "NSASCIIStringEncoding"
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (class "NSStringEncoding")
etc.. Any ideas?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">NSASCIIStringEncoding is only a constant. It represents an integer value. Look it up in the NSString headers, and send the integer value as the parameter.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well it says it's 1...
NSASCIIStringEncoding = 1
but this doesn't work:
set myAttrString to call method "alloc" of class "NSAttributedString"
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter 1
set myHTMLAttr to call method "initWithHTML:documentAttributes:" of object myAttrString with parameters {myData, null}
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Ibson:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Synotic:
<strong>OK.. it's this.. I can't figure out how to pass NSASCIIStringEncoding to dataUsingEncoding... Some things I've tried:
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter "NSASCIIStringEncoding"
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter (class "NSStringEncoding")
etc.. Any ideas?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">NSASCIIStringEncoding is only a constant. It represents an integer value. Look it up in the NSString headers, and send the integer value as the parameter.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well it says it's 1...
NSASCIIStringEncoding = 1
but this doesn't work:
set myAttrString to call method "alloc" of class "NSAttributedString"
set myData to call method "dataUsingEncoding:" of object "<b>test</b>" with parameter 1
set myHTMLAttr to call method "initWithHTML:documentAttributes:" of object myAttrString with parameters {myData, null}</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Try sending myData a description message, ie:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">log (call method "description" of object myData)</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">All your code looks OK, but there might be some funky this happening with the NSData object. If this doesn't work, then you may be forced to write an Obj-C wrapper like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">+ (NSAttributedString *)attributedStringWithHTML  NSString *)string
{
NSAttributedString *newString;
NSData *stringData;
stringData = [string dataUsingEncoding:NSASCIIStringEncoding];
newString = [[self alloc] initWithHTML:stringData documentAttributes:nil];
return [newString autorelease];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">...and add that as a category to NSAttributedString. Hopefully it won't come to this, but it might. This is what happened when I began to program in AS-S: I ended up writing so many Obj-C methods to use toolbars, defaults, bundles, etc. that I just ended up programming in straight Obj-C, and I never turned back  .
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|