Thursday, December 3, 2020

#1 2020-09-20 04:25:46 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 481

First character of every words in new string as uppercaseString

Sometimes it could be useful to have the first character of every words in
new string as uppercaseString.

This is my approach.

ex.

As soon as possible = ASAP

Applescript:

use framework "Foundation"

log (my stringFirstCharacterOfWordsString:"As a matter of fact")
log (my stringFirstCharacterOfWordsString:"As soon as possible")
log (my stringFirstCharacterOfWordsString:"Any Day Now")
log (my stringFirstCharacterOfWordsString:"at this moment")
log (my stringFirstCharacterOfWordsString:"For what it's worth")

(**
* This function return the first character of every words as uppercaseString.
*)

on stringFirstCharacterOfWordsString:theString
   set firstCharacter to current application's NSMutableString's alloc()'s init()
   set anWords to (current application's NSString's stringWithString:theString)'s componentsSeparatedByCharactersInSet:(current application's NSCharacterSet's whitespaceCharacterSet())
   repeat with w in anWords
       if (w's |length|() > 0) then
           set firstLetter to (w's substringToIndex:1)
           (firstCharacter's appendString:(firstLetter's uppercaseString()))
       end if
   end repeat
   return firstCharacter as text
end stringFirstCharacterOfWordsString:

EDIT:
Did some Code refactoring of my original code.
So I will understand it, ex. I do not need current application's with a more simple and clean code.
Sure I could use property with reference to instead of handler.

Applescript:

use framework "Foundation"

log (my stringFirstCharacterOfWordsString:"As a matter of fact")
log (my stringFirstCharacterOfWordsString:"As soon as possible")
log (my stringFirstCharacterOfWordsString:"Any Day Now")
log (my stringFirstCharacterOfWordsString:"at this moment")
log (my stringFirstCharacterOfWordsString:"For what it's worth")

(**
* This function return the first character of every words as uppercaseString.
*)

on stringFirstCharacterOfWordsString:theString
   set firstCharacter to _nsMutableString()
   set anWords to (_nsString(theString))'s componentsSeparatedByCharactersInSet:(_nsCharacterSetWhitespace())
   repeat with w in anWords
       if (w's |length|() > 0) then
           set firstLetter to (w's substringToIndex:1)
           (my _appendStringUppercase:firstLetter toSet:firstCharacter)
       end if
   end repeat
   return firstCharacter as text
end stringFirstCharacterOfWordsString:

on _appendStringUppercase:theString toSet:inputString
   return inputString's appendString:(theString's uppercaseString())
end _appendStringUppercase:toSet:

on _nsCharacterSetWhitespace()
   return current application's NSCharacterSet's whitespaceCharacterSet()
end _nsCharacterSetWhitespace

on _nsString(theString)
   return current application's NSString's stringWithString:theString
end _nsString

on _nsMutableString()
   return current application's NSMutableString's alloc()'s init()
end _nsMutableString

Last edited by Fredrik71 (2020-09-24 02:35:04 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#2 2020-09-22 07:42:46 pm

technomorph
Member
Registered: 2017-12-14
Posts: 173

Re: First character of every words in new string as uppercaseString

Have you tried just simply applying NSString’s uppercaseString
To the whole sentence?

It seems like your going thru a lot of extra code to achieve this.

Offline

 

#3 2020-09-23 02:34:02 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 481

Re: First character of every words in new string as uppercaseString

Hi.

I'm not sure I understand what you mean.

The function take the first character of every word and and set that to a new string
and make that uppercase.

So I need to separate the string with whitespace. Now I have words.
I ask for the character of first index of every word.
I apply uppercase
append to new string.

Show me your code or approach  ??


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#4 2020-09-23 06:19:58 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 481

Re: First character of every words in new string as uppercaseString

This is other approach a more simple version but sure works I believe.

This version use AS to get the character 1 of every words in repeat loop.
Copy it to charList.
ASOC take the list and convert it back to string as uppercaseString.

Applescript:

use framework "Foundation"

set theWords to "hello world!"

set charList to {}
set theWords to words of theWords
repeat with i from 1 to (count theWords)
   set theChar to character 1 of (item i of theWords)
   copy theChar to end of charList
end repeat

set aString to (current application's NSArray's arrayWithArray:charList)'s componentsJoinedByString:""
return (aString's uppercaseString()) as text

Applescript:

on stringFirstCharacterOfWordsString2:theString
   set charList to {}
   set anWords to words of theString
   repeat with i from 1 to (count anWords)
       set theChar to character 1 of (item i of anWords)
       copy theChar to end of charList
   end repeat
   
   set aString to (current application's NSArray's arrayWithArray:charList)'s componentsJoinedByString:""
   return (aString's uppercaseString()) as text
end stringFirstCharacterOfWordsString2:

Last edited by Fredrik71 (2020-09-23 06:26:01 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#5 2020-09-23 06:52:19 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5313

Re: First character of every words in new string as uppercaseString

Hi.

This works if you only want letter characters:

Applescript:

use framework "Foundation"

on acronymOf:theString
   set theString to (current application's NSString's stringWithString:(theString))'s localizedCapitalizedString()
   set nonCaps to current application's NSCharacterSet's uppercaseLetterCharacterSet()'s invertedSet()
   set caps to theString's componentsSeparatedByCharactersInSet:(nonCaps)
   return (caps's componentsJoinedByString:("")) as text
end acronymOf:

my acronymOf:"As soon as possible" --> "ASAP"


NG

Offline

 

#6 2020-09-23 07:37:48 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11700
Website

Re: First character of every words in new string as uppercaseString

This is a O(n) (only one iteration of the sentence) approach. AppleScriptObjC is only used to capitalize the characters.

Applescript:

use AppleScript version "2.5"
use framework "Foundation"

log (my stringFirstCharacterOfWordsString:"As a matter of fact")
log (my stringFirstCharacterOfWordsString:"As soon as possible")
log (my stringFirstCharacterOfWordsString:"Any Day Now")
log (my stringFirstCharacterOfWordsString:"at this moment")
log (my stringFirstCharacterOfWordsString:"For what it's worth")

on stringFirstCharacterOfWordsString:theString
   set isSpace to true
   set theResult to ""
   repeat with aCharacter in theString
       if contents of aCharacter is space then
           set isSpace to true
       else if isSpace then
           set theResult to theResult & (current application's NSString's stringWithString:aCharacter)'s uppercaseString() as text
           set isSpace to false
       end if
   end repeat
   return theResult
end stringFirstCharacterOfWordsString:


regards

Stefan

Offline

 

#7 2020-09-23 10:38:54 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 481

Re: First character of every words in new string as uppercaseString

Thanks for sharing, StefanK and Nigel...

The interesting part about coding is not always the task itself but more the solution we choose
to solve it. In other words different way is always interesting also important to become a good
in anything.

And if we did the same thing tomorrow I guess the code will be different.

Refactoring of Nigels code could look like this, still very readable but less code in the handler.

Applescript:

use framework "Foundation"

log (my acronymOf:"As a matter of fact")
log (my acronymOf:"As soon as possible")

on acronymOf:theString
   set theString to (_nsString(theString))'s localizedCapitalizedString()
   set nonCaps to _nsCharacterSetUppercaseLetterInvert()
   set caps to theString's componentsSeparatedByCharactersInSet:(nonCaps)
   return (caps's componentsJoinedByString:("")) as text
end acronymOf:

on _nsString(theString)
   return current application's NSString's stringWithString:theString
end _nsString

on _nsCharacterSetUppercaseLetterInvert()
   return current application's NSCharacterSet's uppercaseLetterCharacterSet()'s invertedSet()
end _nsCharacterSetUppercaseLetterInvert

Last edited by Fredrik71 (2020-09-24 03:11:11 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#8 2020-09-29 01:29:35 am

technomorph
Member
Registered: 2017-12-14
Posts: 173

Re: First character of every words in new string as uppercaseString

Fredrik71 wrote:

Hi.

I'm not sure I understand what you mean.

The function take the first character of every word and and set that to a new string
and make that uppercase.

So I need to separate the string with whitespace. Now I have words.
I ask for the character of first index of every word.
I apply uppercase
append to new string.

Show me your code or approach  ??



Sorry I was suggesting the wrong function (property)
It should be
-capitalizedString:

https://developer.apple.com/documentati … izedstring

Just try applying the capitalizedString function to your
Whole string.  It should upperCase the first character of each
Word in your string.   


If you don’t want to upperCase words like “and” “the”
You may want to try:

- capitalizedStringWithLocale:

Offline

 

#9 2020-10-04 02:12:13 pm

twistit
Member
Registered: 2006-11-27
Posts: 8

Re: First character of every words in new string as uppercaseString

Hi, there.
I am using this regex handler, which works fine. It respects apostrophed words...

Applescript:

use scripting additions
use framework "Foundation"

set txt to (the clipboard as text)
set {pascalClip, compareList, inputString} to (my pascalizeStrings:txt)

on pascalizeString:inputText
   set outputString to current application's NSMutableString's |string|()
   outputString's appendString:inputText
   set thePats to {}
   set thePattern to "(^|\b)([a-z])(\w*)(\)?'?[a-z]?[a-z]?)" -- to replace lower word bounds only
   # set thePattern to "(\\w)(\\w*)(\\)?'?[a-z]?e?)" -- to replace all word bound
   set regexExpr to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:(16 + 1024) |error|:(missing value)
   set theMatches to regexExpr's matchesInString:(outputString) options:(0) range:({0, outputString's |length|()})
   
   repeat with i in theMatches
       set thisRange to i's range()
       set old to (outputString's substringWithRange:thisRange)
       set f to (old's substringToIndex:1)'s uppercaseString()
       set nw to (f's stringByAppendingString:(old's substringFromIndex:1))
       set end of thePats to {thisRange, old's |length|(), current application's NSMaxRange(thisRange), old as text, nw as text}
       (outputString's replaceCharactersInRange:thisRange withString:nw)
   end repeat
   
   return {(outputString as text), thePats, inputText as text}
end pascalizeString:

# The result would be:

-->Use Scripting Additions
Use Framework "Foundation"
Set Txt To (The Clipboard As Text)
Set PascalClip To (My PascalizeString:Txt)

On PascalizeString:InputText
    Set OutputString To Current Application's NSMutableString's |String|()
    -- et cetera...
End PascalizeString:

Model: iMac
AppleScript: 2.5
Browser: Safari 605.1.15
Operating System: macOS 10.11

Last edited by twistit (2020-10-23 09:22:28 am)


Filed under: regex, Strings

Offline

 

#10 2020-11-12 01:07:32 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1457

Re: First character of every words in new string as uppercaseString

As I understand it, the topic concerns the creation of an acronym for some text. And when several solutions are proposed at once, I am always interested in determining the most effective one.

@technomorph correctly noted that it is more efficient to use the capitalizedString function to get the acronym. I tested all suggested options using the same sentence: "As soon as possible". The tests are done with the excellent Script Geek app (wroten by Shane Stanley).

So, going from least effective to most effective:

1) @twisti's script - doesn't work (some problem with pattern)
2) @Fredrik71's scripts 1 and 2 - 19 mks
3) @StefanK's script - 10 mks
4) @Fredrik71's  script 4 and Nigel's script - 7 mks
5) @Fredrik71's  script 3 - 5 mks
6) suggested by @technomorph and implemented  by me - 3 mks. Here it is:

Applescript:


use framework "Foundation"

set sourceString to current application's NSString's stringWithString:"As soon as possible"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set theItems to (sourceString's capitalizedString() as text)'s text items
set AppleScript's text item delimiters to ATID

set acronim to {}
repeat with theItem in theItems
   set acronim's end to theItem's character 1
end repeat
set acronim to acronim as text

Last edited by KniazidisR (2020-11-12 01:09:46 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#11 2020-11-15 02:45:55 pm

twistit
Member
Registered: 2006-11-27
Posts: 8

Re: First character of every words in new string as uppercaseString

I might have overdone it a bit...
Sorry for that.

This should work solidly:

Applescript:

on pascalizeString:inputText
   set outputString to current application's NSMutableString's |string|()
   outputString's appendString:inputText
   set thePattern to "(^|\\b)([a-z])([\\w]*[']?[stdrv]?[\\w]?)"
   set theOptions to (16 + 1024)
   set regexExpr to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:(theOptions) |error|:(missing value)
   set theMatches to regexExpr's matchesInString:outputString options:0 range:({0, outputString's |length|()})
   repeat with i in theMatches
       set thisRange to (range() of i)
       set thisWord to (outputString's substringWithRange:thisRange)
       set newWord to (((thisWord's substringToIndex:1)'s uppercaseString())'s stringByAppendingString:(thisWord's substringFromIndex:1))
       (outputString's replaceCharactersInRange:thisRange withString:newWord)
   end repeat
   return (outputString as text)
end pascalizeString:

Offline

 

#12 2020-11-22 09:02:14 pm

technomorph
Member
Registered: 2017-12-14
Posts: 173

Re: First character of every words in new string as uppercaseString

KniazidisR wrote:



Applescript:


use framework "Foundation"

set sourceString to current application's NSString's stringWithString:"As soon as possible"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set theItems to (sourceString's capitalizedString() as text)'s text items
set AppleScript's text item delimiters to ATID



You can also use on a NSString

set theItems to (sourceString’s componentsSeparatedByString:@“ “)

Which will return you a NSArray (which you can then coerce using as list)

https://developer.apple.com/documentati … ge=objcThe

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)