Text field help...

Is there a way for me to find out where my cursor is in the text. I don’t know what it’s called, the “line that shows you where your going to type.” (Now I feel stupid :slight_smile: ) At least is there a way to get a number telling me that information. Example:

“Hi.| How are you?”
That pipe shows me were my cursor is. That cursor is 4 characters away from the beginning. This action [that would help me find out where my cursor is in the text] would return 4.

Hope my problem is not to confusing. :stuck_out_tongue:

-dylanweber


I spilled apple juice on my mac. It doesn’t break it, it just makes it more “appley.”

selectedRange will return a list of two numbers: the first is the (zero-based) offset of the selection, and the second is the number of characters selected. But it only works on an NSTextView, not an NSTextField, AFAIK.

That’s it? None for NSTextField? All I want is that but with NSTextField. Can you tell me how to make NSTextView in replace of a text field?

Whoops, sorry – you can use selectedRanges (with an “s”) on an NSTextField. It returns a list of ranges.

NSText *textEditor = [myTextField currentEditor];
NSRange selRange = [textEditor selectedRange];

selectedRanges is a method of NSTextView.

You can use selectedRanges by getting the current editor of the text field but it doesn’t make much sense here due to the fact that you cannot select multiple ranges in an NSTextField. You will still only get an array of one range. :slight_smile:

Got it! I used “location of (textfield’s selectedRange())” to give me the cursor location as a number and changed the text field to text view. Is there a way to when you press enter like on a text field, text view does your actions? Please help!

Oops – NSTextField/NSTextView dislexia :frowning:

You can change it back to a text field and use Craig’s construction.

What is Craig’s construction? I tried everything and nothing works.

I know how you feel! Yesterday I was doing a video demonstrating text view and must have said
text field twenty times! Lots of re-takes. :stuck_out_tongue:

I was getting sporadic results using AppleScriptObjC so I moved the method into Objective-C.
You can download the project at AllanCraig.net.

If you have any questions about this example, don’t hesitate to ask. :wink:

The file will not download. Help! :smiley:

Ok, it is fixed now.

It keeps telling me

Location: 0
Length: 0

HELPZ!

The reason I want this service is because I am making a URL shortener. Instead of putting it on the end automatically, it will get the offset of the cursor and then put the shortened URL were the cursor is or replace the selected characters.

EDIT:
Oh, I have to press the button. I keep pressing enter, like normal people. Now it works.

I need help… How do I implement this into my app? I need help. I added the object to the Nib and I add those two files into my projects class. I changed the line that gets the information to work with my variables, but it says:

Why can’t it set it to a variable! AGH!
Help.

Help?

You will need to provide more information.

OK, here is my project (Unavailable)

My problem is I need to make the panel that shortens URLs to put the URL where the cursor is. See what I did wrong…

To get the selected range of the tweet box you need to put the code in a new handler and attach it to the TinyURL! button. Getting the range of a text field requires the text field have focus.

I will look at it more this evening.