Trying to return the line number in a field

Working on making the switch to SuperCard? Many here have done it... ask your questions here.

Trying to return the line number in a field

Postby elsdonp » Sat Apr 25, 2009 6:26 am

Hi everyone
I've been going through all the scripts in the HyperCard stacks I've converted to SuperCard and fixing the lines that fail, but I'm stuck with trying to convert a line that I've used a lot to return the line number of the field just clicked on. Here is the script ---

on mouseUp
put trunc(1 + (the clickV - top of bkgnd fld "Yes/No") / textheight ¬
of bkgnd fld "Yes/No") into lineNo

if line lineNo of bkgnd fld "Yes/No" = "√" then
put " " into line lineNo of bkgnd fld "Yes/No"
else
if line lineNo of bkgnd fld "Names" is empty then
if line lineNo of bkgnd fld "Amounts" is empty then
exit mouseUp
end if
end if
put "√" into line lineNo of bkgnd fld "Yes/No"
end if
end mouseUp

Sorry, the line tabbing has been lost after pasting here.
It's the first line that fails (in red). I've attached the error message dialog that occurs.
The error seems to be caused because the field is empty. It works fine once I unlock the text and add a 'newline' in the field and then lock the text again.

I look forward to any suggestions.
Thank You
Regards Paul
Attachments
Picture 4.png
Error dialog
Picture 4.png (19.17 KiB) Viewed 3225 times
elsdonp
 
Posts: 11
Joined: Thu Apr 09, 2009 7:27 am

Re: Trying to return the line number in a field

Postby Scott » Wed Apr 29, 2009 9:25 am

Check the value of the textHeight property. If you have more than one style run in your field it will return "auto" and not a numeric value.

BTW... if this is a locked text field you can simply use the clickLine function.
Code: Select all
on mouseup
get the clickline
if value(it) = "√" then beep
end mouseup
User avatar
Scott
Site Admin
 
Posts: 1217
Joined: Sat Jul 05, 2008 1:37 pm
Location: Northern Sierra Nevada

Re: Trying to return the line number in a field

Postby elsdonp » Wed Apr 29, 2009 11:18 am

Thanks for the info. Yes it is a locked field and I've tried clickline but I'm not clear how to update the contents of the line I click on. Taking your script as an example, instead of 'beep' I need to update the clicked on line to " ". Any suggestions?

Also, I'm not sure how to check textHeight property !!

Regards
Paul
elsdonp
 
Posts: 11
Joined: Thu Apr 09, 2009 7:27 am

Re: Trying to return the line number in a field

Postby Scott » Wed Apr 29, 2009 11:48 am

elsdonp wrote:Thanks for the info. Yes it is a locked field and I've tried clickline but I'm not clear how to update the contents of the line I click on. Taking your script as an example, instead of 'beep' I need to update the clicked on line to " ". Any suggestions?


Yes... just replace the line throwing the error in your script to:
Code: Select all
put word 2 of the clickline into lineNo


Also, I'm not sure how to check textHeight property !!


Code: Select all
put the textHeight of bkgnd fld "Yes/No"

and check the message box.
User avatar
Scott
Site Admin
 
Posts: 1217
Joined: Sat Jul 05, 2008 1:37 pm
Location: Northern Sierra Nevada

Re: Trying to return the line number in a field

Postby elsdonp » Thu Apr 30, 2009 8:34 am

I'm still having problems which seems to be related to whether anything has been written to the field. If I copy a new card, ie nothing written to the bg fld, then the cmd you suggested (put word 2 of the clickline into lineNo) always returns 1, regardless of the line clicked on. However, this writes to line 1 of the fld and if I then revert to the original script, it works without error. I then put back the clickline cmd and it returns the correct line each time and therefore also works. I'm confused !!
elsdonp
 
Posts: 11
Joined: Thu Apr 09, 2009 7:27 am

Re: Trying to return the line number in a field

Postby Lisa » Thu Apr 30, 2009 10:21 am

elsdonp wrote:If I copy a new card, ie nothing written to the bg fld, then the cmd you suggested (put word 2 of the clickline into lineNo) always returns 1, regardless of the line clicked on.

The clickLine() function is based on the actual physical lines in the field, so it doesn't work quite the same as the original mathematical formula -- as you've seen, clickLine() returns 1 if there's no text in the field; moreover, if the field contains only, say, 5 lines of actual text, it will return 5 even if you click on what appears to be line 8 or line 12, etc. This should be easy to work around though, here's some ideas:

Is it possible to keep a constant number of carriage returns in the field, even if the lines are blank? If your field has a maximum of, say, 8 lines, you could fill it with 8 carriage returns when you create a new card. This should allow clickLine() to work, returning the correct line that was clicked on.

Is the user allowed to change the font/size of the field text? If not, you could return to your original formula but hard-code the textHeight of the line:
Code: Select all
put trunc(1 + (the clickV - top of bkgnd fld "Yes/No") / 15) into lineNo    -- 15 is textHeight for Lucida Grande 12

If they are allowed to change the font/size, then you could get the textHeight each time they do so and store it in a global var or a user prop. Then plug that into your formula.

Or you might try expicitly setting the textFont and textSize of the field when you create a new card. That might (I'm not 100% sure) get the field to return a number for textHeight instead of "Auto" even when it's blank.

In any event, if you use the formula with the textHeight property, you should probably build error checking into your script to make sure you're not trying to do math with "Auto" instead of a number:
Code: Select all
on mouseUp
    get textHeight of bg fld "Yes/No"
    if NOT isNumber(it) then
        -- abort or do something to correct
    end if
    -- continue with usual script
    ...
end mouseUp


HTH,
Lisa
iMac 24" 3.06 GHz Intel Core 2 Duo, 4 GB RAM, Mac OS X 10.6.8, SC 4.7.3
User avatar
Lisa
 
Posts: 303
Joined: Sun Jul 06, 2008 12:33 pm
Location: San Diego, CA

Re: Trying to return the line number in a field

Postby elsdonp » Thu Apr 30, 2009 11:00 am

I'm the only one using this 'Stack' and the textheight will be fixed to 11, so I chose the simplest solution -

put trunc(1 + (the clickV - top of bkgnd fld "Yes/No 2") / 11) into lineNo

This works great

Thanks to everyone for their help with this, probably seemingly simple, problem :oops:

Regards
Paul
elsdonp
 
Posts: 11
Joined: Thu Apr 09, 2009 7:27 am


Return to Upgrading From HyperCard

Who is online

Users browsing this forum: No registered users and 1 guest