• 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: Need to make a Cocoa Java app email some text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need to make a Cocoa Java app email some text


  • Subject: Re: Need to make a Cocoa Java app email some text
  • From: "Nathan V. Roberts" <email@hidden>
  • Date: Thu, 13 Dec 2001 16:24:41 -0600

OK folks, here is the problem (I am using the Cocoa Java API's and I am a novice): I have an app, which upon clicking a button, I want it to take the text from an NSTextView and open the users default mail program and insert the text in the body of an email message. I have tried using NSWorkspace.sharedWorkspace().openURL() and put in a URL that has "mailto:"; in it. This works, except to get the text in the email you have to encode it and so when it appears in the email it has a "+" between every word. There is an Obj-C class, something like, NSMailMessage, which isn't available for Java. It seems to me I have two options: 1) get sun's java mail classes or 2) bridge the Obj-C NSMail-whatever class. If I do number 1, where do I put the .jar files so that I can import them into my classes in Project Builder? If I do number 2, well, I have no idea how to do number 2... Anyone have any suggestions?


I had a similar goal a while back, and came up with a fairly simple solution. If you run through your string searching for spaces, and replace them with (20 is the hex representation of the ASCII value of space), that takes care of the spaces. For a more general-purpose approach (one that'll escape newlines as well, etc.), I wrote a couple routines. I didn't need them to be fast, so they almost certainly aren't, and they could certainly be extended to cover more of the escapable characters (just add more urlEncoder.put()'s), but here they are.

Nate

import java.util.*;

public String urlEncode(String urlToEncode) {

Hashtable urlEncoder = new Hashtable(7);

urlEncoder.put("%", "%"); //% needs escaping first
urlEncoder.put("\n", " ");
urlEncoder.put(" ", " ");
urlEncoder.put("<", "<");
urlEncoder.put("/", "/");
urlEncoder.put(">", ">");
urlEncoder.put("?", "?");
urlEncoder.put("&", "&");

Enumeration keys = urlEncoder.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = (String) urlEncoder.get(key);
urlToEncode = findAndReplace(urlToEncode, key, value);
}

return urlToEncode;
}

public String findAndReplace(String theString, String find, String replace) {
String newString = "";
int findlength = find.length();
int replacelength = replace.length();
int prevMatch = -findlength;
boolean moreMatches;
if (theString.indexOf(find) != -1) {
moreMatches = true;
} else {
moreMatches = false;
newString = theString;
}
while (moreMatches) {
int matchIndex = theString.indexOf(find, prevMatch + findlength);
if (matchIndex >= 0) {
newString += theString.substring(prevMatch+findlength, matchIndex) + replace;
prevMatch = matchIndex;
} else {
newString += theString.substring(prevMatch+findlength);
moreMatches = false;
}
}
return newString;
}


References: 
 >Need to make a Cocoa Java app email some text (From: "Ian G. Gillespie" <email@hidden>)

  • Prev by Date: Re: buy that new Book?
  • Next by Date: Newbie questions about preferences
  • Previous by thread: Need to make a Cocoa Java app email some text
  • Next by thread: Subclass of TextView in ScrollView
  • Index(es):
    • Date
    • Thread