Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: parse punctuation with text

I need to get all text and punctuation from text, 24 words at a time.

This gets 24 words at a time, in 3 separate variables, but not "," and "."

Any ideas.



tell application "Finder"

set y to 1

set sect1 to ""

set sect2 to ""

set sect3 to ""

set x to "Do not lie to one another, seeing that you have put off the old self with its practices and have put on the new self, which is being renewed in knowledge after the image of its creator. Here there is not Greek and Jew, circumcised and uncircumcised, barbarian, Scythian, slave, free; but Christ is all, and in all. Put on then, as God's chosen ones, holy and beloved, compassionate hearts, kindness, humility, meekness, and patience, bearing with one another and, if one has a complaint against another, forgiving each other;"

repeat 24 times

set sect1 to sect1 & word y of x & " "

set y to y + 1

end repeat

set y to 25

repeat 24 times

set sect2 to sect2 & word y of x & " "

set y to y + 1

end repeat

set y to 49

repeat 24 times

set sect3 to sect3 & word y of x & " "

set y to y + 1

end repeat


display dialogsect1 & return & return & sect2 & return & return & sect3

end tell

Posted on

Reply

Page content loaded

Nov 11, 2017 1:44 PM in response to FCPeditor In response to FCPeditor

Because punctuation marks are not “words” in AppleScript.


This will get all words and punctuation items back into a list for you to run your loops on:


set x to "Do not lie to one another, seeing that you have put off the old self with its practices and have put on the new self, which is being renewed in knowledge after the image of its creator. Here there is not Greek and Jew, circumcised and uncircumcised, barbarian, Scythian, slave, free; but Christ is all, and in all. Put on then, as God's chosen ones, holy and beloved, compassionate hearts, kindness, humility, meekness, and patience, bearing with one another and, if one has a complaint against another, forgiving each other;"


set wordpunct to {}

set x to x'squoted form

set wordpunct to my tokenize(x)

-- display dialog (items of wordpunct) as text

return


on tokenize(sarg)

-- Given the input string, return an array containing words and punctuation items,

-- but no spaces, or empty items

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

#!/usr/bin/ruby

print ARGV.first.scan(/\\w+|\\W/) - [\"\", \" \", nil]

EOF"

end tokenize

Nov 11, 2017 1:44 PM

Reply Helpful

Nov 11, 2017 2:00 PM in response to FCPeditor In response to FCPeditor

Hi, FCPeditor


No need to tell the Finder to do anything here, it's for organising files and folders.


This sets AppleScript's text item delimiters to a space character, and then splits up the text items (character runs including punctuation marks.) It also adds the last section of the text, which has fewer than 24 words.



--begin script

set the_text to "Do not lie to one another, seeing that you have put off the old self with its practices and have put on the new self, which is being renewed in knowledge after the image of its creator. Here there is not Greek and Jew, circumcised and uncircumcised, barbarian, Scythian, slave, free; but Christ is all, and in all. Put on then, as God's chosen ones, holy and beloved, compassionate hearts, kindness, humility, meekness, and patience, bearing with one another and, if one has a complaint against another, forgiving each other;"



set AppleScript'stext item delimiters to " "

set word_count to counttext items in the_text

set word_mod to word_count mod 24

set nu_par to ""

repeat with x from 1 to word_count by 24

try

set nu_par to nu_par & text items x thru (x - 1 + 24) of the_text & return & return

on error

set nu_par to nu_par & text items x thru (x - 1 + word_mod) of the_text as text

end try

end repeat

display dialognu_par

--end script



Hope it helps,


--EDIT - the script now counts text items rather than words.


H

Nov 11, 2017 2:00 PM

Reply Helpful
User profile for user: FCPeditor

Question: parse punctuation with text