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 > NSMutableAttributedString Additions and HTML

NSMutableAttributedString Additions and HTML
Thread Tools
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 4, 2002, 03:00 AM
 
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
Reply With Quote
Jul 4, 2002, 03:55 AM
 
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 *)makeAttributedStringWithHTMLNSData *)data documentAttributesNSDictionary **)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'."
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 4, 2002, 11:32 AM
 
</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 *)makeAttributedStringWithHTMLNSData *)data documentAttributesNSDictionary **)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
Reply With Quote
Jul 4, 2002, 06:24 PM
 
</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
Reply With Quote
Jul 4, 2002, 09:04 PM
 
</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 &quot;alloc&quot; of class &quot;NSMutableAttributedString&quot;
call method &quot;initWithPath:documentAttributes:&quo t; of object myAttrString ¬
with parameters {&quot;/Users/ibson/Desktop/Sample RTF File.rtf&quot;, null}
log (call method &quot;string&quot; 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>
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 5, 2002, 03:54 PM
 
</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 &quot;alloc&quot; of class &quot;NSMutableAttributedString&quot;
call method &quot;initWithPath:documentAttributes:&quo t; of object myAttrString ¬
with parameters {&quot;/Users/ibson/Desktop/Sample RTF File.rtf&quot;, null}
log (call method &quot;string&quot; 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..
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 5, 2002, 08:10 PM
 
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 "&lt;b&gt;test&lt;/b&gt;" with parameter "NSASCIIStringEncoding"

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" with parameter (class "NSStringEncoding")

etc.. Any ideas?
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jul 5, 2002, 08:18 PM
 
</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 "&lt;b&gt;test&lt;/b&gt;" with parameter "NSASCIIStringEncoding"

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" 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.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jul 5, 2002, 08:33 PM
 
</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 "&lt;b&gt;test&lt;/b&gt;" with parameter "NSASCIIStringEncoding"

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" 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 "&lt;b&gt;test&lt;/b&gt;" 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
Reply With Quote
Jul 6, 2002, 12:03 AM
 
</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 "&lt;b&gt;test&lt;/b&gt;" with parameter "NSASCIIStringEncoding"

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" with parameter (call method "NSASCIIStringEncoding" of class "NSStringEncoding")

set myData to call method "dataUsingEncoding:" of object "&lt;b&gt;test&lt;/b&gt;" 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 "&lt;b&gt;test&lt;/b&gt;" 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 &quot;description&quot; 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 *)attributedStringWithHTMLNSString *)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 .
     
   
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 09:57 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