Split Text Help!

How to split text from one delimiter to another delimiter.

I want to split text from # to Tab.
On the splitted text move the cursor to the 10th character, find " " space in reverse and to insert <\b> and from there go to the 10th character, find " " space in reverse and to insert <\b> repeat until if the character count is 10, likewise throughout the document.

Sample Text
The #quick brownfox jumps in over the lazy dog. The quick #brk own fox jumps over the lazy dog.
Output Text
The #quick <\b>brownfox <\b>jumps in <\b>over the lazy dog. The quick #brk own <\b>fox jumps <\b>over the <\b>lazy dog.

Something like this?

set theText to "The #quick brownfox jumps in over the" & tab & "lazy dog.  The quick #brk own fox jumps over the lazy" & tab & "dog."


set AppleScript's text item delimiters to "#"
set NrOfTis to count of text items of theText
if NrOfTis = 1 then
	return
else
	set prefix to text item 1 of theText
	set textItems to text items 2 thru -1 of theText
end if
set AppleScript's text item delimiters to "<\\b>"

repeat with i from 1 to count textItems
	set tabOffset to offset of tab in (item i of textItems)
	
	if tabOffset > 0 then
		set textParts to {}
		set buffer to text 1 thru tabOffset of item i of textItems
		repeat until length of buffer = 0
			set eo to count buffer
			set so to eo - 9
			--move to space
			repeat until so < 2 or character so of buffer = space
				set so to so - 1
			end repeat
			
			if so < 2 then
				set beginning of textParts to buffer
				set buffer to ""
			else
				set beginning of textParts to text (so + 1) thru eo of buffer
				set buffer to text 1 thru so of buffer
			end if
		end repeat
		set newtextItem to textParts as string
		if tabOffset is not equal to (count (item i of textItems)) then
			set newtextItem to newtextItem & text (tabOffset + 1) thru -1 of item i of textItems
		end if
		set item i of textItems to newtextItem
	end if
end repeat

set AppleScript's text item delimiters to "#"
set newText to ({prefix} & textItems) as string
set AppleScript's text item delimiters to ""
return newText

MacScripter replaces tabs with spaces when posted. therefore the weird " & tab & " instead of typing in the character

Hi.

I think the results from this are more in line with what johnmathew wanted:

-- Assumptions:
-- The text contains both hashes and tabs.
-- These occur alternately.
-- The first hash occurs before the first tab.
-- The script's running in Mac OS 10.6 or later.
-- The "ten characters" exclude the hashes and the tabs.

set theText to "The #quick brownfox jumps in over the" & tab & "lazy dog.  The quick #brk own fox jumps over the lazy" & tab & "dog."

set astid to AppleScript's text item delimiters
-- Break the text at the hashes and the tabs, assuming that they occur alternately and that the first hash occurs before the first tab.
set AppleScript's text item delimiters to {"#", tab}
set textItems to theText's text items

-- Preset a "<\\b>" delimiter.
set AppleScript's text item delimiters to "<\\b>"
-- Edit the even-numbered text items as required.
repeat with i from 2 to (count textItems) by 2
	-- Get a text item to be edited.
	set textBetweenDelims to item i of textItems
	set len to (count textBetweenDelims)
	-- Initialise a collector for the segments of ten or fewer characters.
	set edits to {}
	-- Initialise start and end indices for the segments.
	set j to 1
	set k to 10
	-- Extract the segments from this text item.
	repeat until (k > len)
		-- Reduce k until a space is found.
		repeat until (item k of textBetweenDelims is space)
			set k to k - 1
			-- If k reaches j, abandon the search for a space and use 10 characters anyway.
			if (k = j) then
				set k to j + 9
				exit repeat
			end if
		end repeat
		-- Append each segment to the collector.
		set end of edits to text j thru k of textBetweenDelims
		-- Reset the indices to get the next segment.
		set j to k + 1
		set k to j + 9
	end repeat
	-- Append any remaining text from the text item to the collector.
	if (j ≤ len) then set end of edits to text j thru len of textBetweenDelims
	-- Replace the original text item with text consisting of "#", the segments (coerced to text with the "<\b>" delimiter), and a tab.
	set item i of textItems to "#" & edits & tab
end repeat

-- Coerce the edited text-item list to a new text.
set AppleScript's text item delimiters to ""
set newText to textItems as text
set AppleScript's text item delimiters to astid

return newText
--> "The #quick <\\b>brownfox <\\b>jumps in <\\b>over the	lazy dog.  The quick #brk own <\\b>fox jumps <\\b>over the <\\b>lazy	dog."

Edit: Comment typos corrected.

Edit— update the code above.

I didn’t used multiple text item delimiters because I didn’t know if the hash was always followed by a single tab. Or if an hash could be followed by another hash or if there are multiple tabs between two hashes.

Good thinking. Here’s a single-delimiter version of my effort:

-- The "ten characters" are assumed to exclude the hash and the tab.

set theText to "The #quick brownfox jumps in over the" & tab & "lazy dog.  The quick #brk own fox jumps over the lazy" & tab & "dog."

-- Set your preferred line width here:
set lineWidth to 10

set astid to AppleScript's text item delimiters
-- Break the text at the hashes.
set AppleScript's text item delimiters to "#"
set textItems to theText's text items

-- Preset a "<\\b>" delimiter.
set AppleScript's text item delimiters to "<\\b>"
-- Edit the text items as required.
repeat with i from 2 to (count textItems)
	-- Get a text item to be edited.
	set textBetweenHashes to item i of textItems
	set len to (offset of tab in textBetweenHashes) - 1
	if (len > 0) then
		-- Initialise a collector for the segments of ten or fewer characters.
		set edits to {}
		-- Initialise start and end indices for the segments.
		set j to 1
		set k to lineWidth
		-- Extract the segments from this text item.
		repeat until (k > len)
			-- Reduce k until a space is found.
			repeat until (item k of textBetweenHashes is space)
				set k to k - 1
				-- If k reaches j, abandon the search for a space and use 10 characters anyway.
				if (k = j) then
					set k to j + lineWidth - 1
					exit repeat
				end if
			end repeat
			-- Append each segment to the collector.
			set end of edits to text j thru k of textBetweenHashes
			-- Reset the indices to get the next segment.
			set j to k + 1
			set k to k + lineWidth
		end repeat
		-- Append any remaining text before the tab to the collector.
		if (j ≤ len) then set end of edits to text j thru len of textBetweenHashes
		-- Replace the original text item with text consisting of "#", the segments (coerced to text with the "<\b>" delimiter), and the text from the tab to the end of the text item.
		set item i of textItems to "#" & edits & text (len + 1) thru -1 of textBetweenHashes
	end if
end repeat

-- Coerce the edited text-item list to a new text.
set AppleScript's text item delimiters to ""
set newText to textItems as text
set AppleScript's text item delimiters to astid

return newText
--> "The #quick <\\b>brownfox <\\b>jumps in <\\b>over the    lazy dog.  The quick #brk own <\\b>fox jumps <\\b>over the <\\b>lazy    dog."

Edits:

  1. Same typo corrected!
  2. New variable ‘lineWidth’ set at the top of the script to allow the convenient use of values other than 10.

I Thank All of you for your kind reply,

As per my sample, The hash was always followed by a single tab.

I failed to mention that the above text was an paragraph and the script to loop all the paragraphs in the document.

I apologize for that.

I would like to run the script in MS Word. So kindly provide me the final script.

Thanks in advance,
Mathew

If the paragraphs in the text already contain the hashes and tabs, there’s no need to loop through them. The scripts above can handle the entire text. (But as I write, DJ’s script still isn’t producing the right results.) You could put the code into a handler and use it at various points, if you liked. For instance, with my second script:

-- Set your preferred line width here:
set lineWidth to 10

set theText to "The #quick brownfox jumps in over the" & tab & "lazy dog.  The quick #brk own fox jumps over the lazy" & tab & "dog.

The #quick brownfox jumps in over the" & tab & "lazy dog.  The quick #brk own fox jumps over the lazy" & tab & "dog.

The #quick brown fox jumps over the" & tab & "lazy dog.  The quick #brk own fox jumps over the lazy" & tab & "dog."

insertBreaks(theText, lineWidth)

(* Or:
repeat with thisPara in theText's paragraphs
	insertBreaks(thisPara, lineWidth)
end repeat
*)

on insertBreaks(theText, lineWidth)
	set astid to AppleScript's text item delimiters
	-- Break the text at the hashes.
	set AppleScript's text item delimiters to "#"
	set textItems to theText's text items
	
	-- Preset a "<\\b>" delimiter.
	set AppleScript's text item delimiters to "<\\b>"
	-- Edit the text items as required.
	repeat with i from 2 to (count textItems)
		-- Get a text item to be edited.
		set textBetweenHashes to item i of textItems
		set len to (offset of tab in textBetweenHashes) - 1
		if (len > 0) then
			-- Initialise a collector for the segments of ten or fewer characters.
			set edits to {}
			-- Initialise start and end indices for the segments.
			set j to 1
			set k to lineWidth
			-- Extract the segments from this text item.
			repeat until (k > len)
				-- Reduce k until a space is found.
				repeat until (item k of textBetweenHashes is space)
					set k to k - 1
					-- If k reaches j, abandon the search for a space and use 10 characters anyway.
					if (k = j) then
						set k to j + lineWidth - 1
						exit repeat
					end if
				end repeat
				-- Append each segment to the collector.
				set end of edits to text j thru k of textBetweenHashes
				-- Reset the indices to get the next segment.
				set j to k + 1
				set k to k + lineWidth
			end repeat
			-- Append any remaining text before the tab to the collector.
			if (j ≤ len) then set end of edits to text j thru len of textBetweenHashes
			-- Replace the original text item with text consisting of "#", the segments (coerced to text with the "<\b>" delimiter), and the text from the tab to the end of the text item.
			set item i of textItems to "#" & edits & text (len + 1) thru -1 of textBetweenHashes
		end if
	end repeat
	
	-- Coerce the edited text-item list to a new text.
	set AppleScript's text item delimiters to ""
	set newText to textItems as text
	set AppleScript's text item delimiters to astid
	
	return newText
end insertBreaks

I’m afraid I don’t know anything about scripting MS Word.

It’s just occurred to me to point out that it’s possible to replace literal tabs in AppleScript strings with “\t”. How the result appears in Script Editor after the code’s compiled depends on the checkbox setting at the bottom of the “Editing” pane in Script Editor’s preferences. The actual content of the strings isn’t affected, only how any tabs (or returns or linefeeds) they contain are displayed in Script Editor.

set theText to "The #quick brownfox jumps in over the\tlazy dog.  The quick #brk own fox jumps over the lazy\tdog."