Applescript to get specific word position from a sentence

Hi.


I am new to Applescript. I want to create a Numbers functions that can get a specific word position from a sentence in a cell and return that words. Ex: =getWord(cell#, position) that return a word.


Also, a function to separate a long word (underTheRain / UnderTheRain) into multiple words base on upper case like =separateWord(cell#) that return all words with space or underscore..


Thank you.

Posted on Jun 11, 2018 11:14 PM

Reply
7 replies

Jun 12, 2018 7:27 AM in response to cccloud

Numbers doesn't support UDFs (user defined functions). So you can't define a getWord() function.


You can, however, easily extract a word using AppleScript, like this:


set sentenceText to "The quick brown fox underTheRain jumps over the lazy dog"

return sentenceText'sword 4



I didn't follow what you are trying to do with long words.


SG

Jun 15, 2018 6:14 AM in response to cccloud

Here is a modern scripting language example of splitting a camelCase string into individual words based on encountered capital letters.


-- break a camelCase string into words based on capital letters

set s to "nowIsTheTimeToGetSerious"


set ws to split_camelCase(s)

display dialogws

return


on split_camelCase(astr)

-- use Regex positive lookahead to split string into words beginning with capital letters

return do shell script "ruby -e 'puts ARGV.first.split(/(?=[A-Z])/).join(\" \");' " & astr's quoted form

end split_camelCase


User uploaded file

Jun 13, 2018 3:32 AM in response to cccloud

Hi cccloud,


Here is a method using formulas in Numbers. We look for the ASCII code to determine upper and lower case.

My brain works best in small steps, hence the number of rows in the Calculations table.

User uploaded file

In the screen shot, I included a cut-down table of upper case and lower case letters, just for reference. Courtesy of https://en.wikipedia.org/wiki/ASCII


In the 'Calculations' table, A2 copies the entered string with this formula =Enter a string in the blue cell::A2

Row 1 contains numbers to denote the position of each character.

Formula in B2 (and Fill Right) =MID($A2,B$1,1)

Formula in B3 (and Fill Right) =IF(LEN(B2)>0,CODE(B2),"")

Blank cells in B become "" (NULL).

Formula in B4 (and Fill Right) =IF(AND(B3≥65,B3≤90),"Yes","No")

Don't worry about the blue warning triangle.

Formula in B5 (and Fill Right) =IF(B4="Yes"," "&B2,B2)

That adds a space before an upper case letter.

Formula in B6 (and Fill Right) =LEN(B5)

That is just me holding up my pants with belt and braces. Not really needed.


Formula in the second table, B2

=CONCATENATE(Calculations::B5,Calculations::C5,Calculations::D5,Calculations::E5 ,Calculations::F5,Calculations::G5,Calculations::H5,Calculations::I5,Calculation s::J5,Calculations::K5,Calculations::L5,Calculations::M5,Calculations::N5,Calcul ations::O5,Calculations::P5,Calculations::Q5,Calculations::R5)


When you get it all to work, Move (Cut and Paste) the 'Enter a string in the blue cell' table to another sheet. Save as Template.


Enter (type or Paste) other strings into the blue cell.

User uploaded file

I don't know if you want the first character to have a preceding space if it is upper case.

User uploaded file

Regards,

Ian.

Jun 14, 2018 2:19 PM in response to cccloud

cccloud wrote:


Thank you SG.

For the long word, I mean if I have theQuickBrownFox/TheQuickBrownFox, I want to add space or underscore before upper case like the_Quick_Brown_Fox/The Quick Brown Fox. Basically, just separate each word by upper case.


Here's a simple way to do that in AppleScript:


property uppers : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


set wordText to "theQuickBrownFox"


set separatedWord to wordText'stext 1

repeat with ltrNum from 2 to (get wordText's length)

set aLtr to wordText'stextltrNum

if not isUpper(aLtr) then

set separatedWord to separatedWord & aLtr

else

set separatedWord to separatedWord & " " & aLtr

end if

end repeat

return separatedWord

-- ==> "the Quick Brown Fox"


isUpper("A")

to isUpper(l)

considering case

return l is in uppers

end considering

end isUpper



"theQuickBrownFox" becomes ==> "the Quick Brown Fox"


This could target text you already have in Numbers cells but it's hard to guess exactly what you are trying to do.


SG

Jun 12, 2018 10:10 PM in response to cccloud

I don't know if what you want is attainable using Applescript. SGIII may, but, like me is puzzled by your description of what you want.


EDIT: Your response to SG's post clarifies the situation.


Is this an accurate description?


You are requesting two separate situations and a resut for each.


1

"that can get a specific word position from a sentence in a cell and return that words"

You will input:

a cell location

a 'position' within that cell.


You want this result:

the word that is in that location.


SG's response above fits this request and result for a text string written into the script (The quick brown…"), but not for a text string in a specified cell. Replacing "The quick brown…lazy dog." with a cell reference might be enough to fully fit your request.


2

"a function to separate a long word (underTheRain / UnderTheRain) into multiple words base on upper case like =separateWord(cell#) that return all words with space or underscore."


Here you give two examples of "a long word": 'underTheRain' and 'UnderTheRain'

For these, you want the results: 'under the rain' and 'Under The Rain' (or ' Under The Rain')

OR the results: 'under_The_Rain' and 'Under_The_Rain' (or '_Under_The_Rain')

Each of the 'long words' is the content of, or included in the content of a specified cell.


To do this, the script must be able to test a string two characters at a time, recognize whether a character is a space or is a lower case letter, AND whether the character following that one is an upper case character, then IF the first character is a lower case letter AND the second is an upper case character, to insert ether a space or an underscore character between the two; otherwise to make no change.


Another way to express the test(s):

The script must check each character of the string. If character n is anything but an upper case letter, the test passes on to the next character. If character n is an upper case letter, then the script checks the n-1 character. If that character is a lower case letter, the script inserts a space (or an underscore) between character n-1 and character n, and continues the tests, starting at character n+1 (original count).


On writing the previous paragraph, these thoughts arose:


The script probably needs to build a new string as it goes through the test of the existing string, adding letter by letter where the 'lower case letter followed by upper case letter' test returns false, and adding the space (or underscore) where the test returns true, then, when the test is finished for that cell, eplacinf the existing string with the newly constructed one.


Interesting problem. Can AppleScript, as applied to Numbers, handle it?


Regards,

Barry

Jun 15, 2018 8:25 AM in response to VikingOSX

Given the string in a cell, and a specified index into that string, the following code will return the word associated with the index position.


-- we pretend this is a cells string contents from which a word is

-- to be returned.

set s to "See Spot run circles around Jane."


set indx to 14

set args to s'squoted form & space & indx

set iw to getword_from_index(args)

display dialogiw

return


on getword_from_index(vargs)

-- return word in string that is associated with its index position

return do shell script "ruby <<'EOF' - " & vargs & "

#!/usr/bin/ruby


# index must not exceed string size

if ARGV.last.to_i > ARGV.first.length

raise 'index value greater than string length'

end


astr = ARGV.first

ndx = ARGV.last.to_i


# Ruby strings and arrays are zero based, so adjust ndx by 1

# and get all text of string from ndx position to end of string

# scan creates an array of words from the string and the first word is the index word

puts astr[(ndx - 1) .. -1].scan(/[\\w+]*/)[0]

EOF"


end getword_from_index


User uploaded file

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to get specific word position from a sentence

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.