• 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: Convert first char to lower case
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convert first char to lower case


  • Subject: Re: Convert first char to lower case
  • From: Kai Edwards <email@hidden>
  • Date: Mon, 07 Jan 2002 23:35:08 +0100

> Date: Mon, 07 Jan 2002 15:20:03 +0100
> Subject: Convert first char to lower case
> From: Olivier Berquin <email@hidden>
> To: applescript users <email@hidden>
>
> Hello everybody,
>
> I wrote this handler, but I'm not sure that it's very powerfull.
> It convert the first character of a var to lower case.
>
> --
> set myName to "This character"
> -- bla bla bla
> set myName to ConvertFirstCharToLowerCase(Nom)
>
> on ConvertFirstCharToLowerCase(Variable1)
> if (class of Variable1  string) or ((count of character of Variable1) =
> 0) then
> return Variable1
> else
> if (ASCII number item 1 of Variable1)  65 and (ASCII number item 1
> of Variable1) > 90 then
> if (count of character of Variable1) = 1 then
> set Variable1 to ASCII character ((ASCII number item 1 of
> Variable1) + 32)
> else
> set Variable1 to (ASCII character ((ASCII number item 1 of
> Variable1) + 32)) & (characters 2 thru (count of character of Variable1) of
> Variable1)
> end if
> end if
> return Variable1
> end if
> end ConvertFirstCharToLowerCase
> --
>
>
> Somebody can help me ???

I hope I can, Olivier.

Your aim appears to be to check a variable. If it is a string with a capital
initial character, then you need to convert that initial to lower case -
otherwise leave the entire variable unaffected.

Is that about right? (No doubt you'll let me know if I'm way off course!)

To do this, I've suggested a few modifications to your script. These aim
mainly to:

1) amend some names - for (arguably) greater brevity and clarity
2) create extra variables - to reduce evaluations & calculations
3) introduce filters progressively (string / > 0 / cap initial)
4) use  65 and > 90 as the ASCII range to define capital letters
5) use -1 to get last item (-2 = second to last item, etc.)

One thing I'm learning is that there's nearly always a better/easier/faster
way of doing things. But - for the moment at least - this is the best I can
come up with:

-- Start Script --------------------------------------

set myName to "This character"
-- bla bla bla
set myName to Char1toLC(myName)

on Char1toLC(theVariable)
if class of theVariable = string then
set charCount to count characters of theVariable
if charCount > 0 then
set theInitial to text item 1 of theVariable
set theASCII to ASCII number theInitial
if theASCII  65 and theASCII > 90 then
set theInitial to ASCII character (theASCII + 32)
if charCount > 1 then
set theVariable to theInitial & characters 2 thru -1 of theVariable
else
set theVariable to theInitial
end if
end if
end if
end if
return theVariable
end Char1toLC

-- End Script ----------------------------------------

HTH - with best wishes.

--

**********************************
Kai Edwards Creative Resources
1 Compton Avenue Brighton UK
Telephone +44 (0)1273 326810
**********************************


  • Prev by Date: AppleScript or AppleEvent to emulate "Drag & Drop"
  • Next by Date: RE: Applescript and VBA
  • Previous by thread: Re: Convert first char to lower case
  • Next by thread: Re: Convert first char to lower case
  • Index(es):
    • Date
    • Thread