Re: tabs to spaces
Re: tabs to spaces
- Subject: Re: tabs to spaces
- From: Skeeve <email@hidden>
- Date: Mon, 01 Dec 2008 21:28:43 +0100
I didn't know you wanted the maximum length of all words. I calculated,
in my perl solution, the maximum length in each column.
Nevertheless: Here is my AS-only solution with the overall length you
wanted.
-- the_clipboard is my clipboard surrogate ;-)
set the_clipboard to "dasj dhsahdwe dhasdhajks ewqhehwq
dsajkdhas
e das dsaw das daswf
fjdk ewf jken dsajkw dskdw
hklt ewq vn1 daskcn daskw"
-- you wanted the longest of all words, not the longest word in a
column. So here we go for that
set overall_length to 0
-- My first trick is to collect uniq words
set uniq_words to {}
-- we will split "words" at tabs
set old_text_item_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
-- but first wee need to split into lines (=paragraphs)
repeat with a_line in every paragraph of the_clipboard
-- then we can get the words
set all_words to text items of a_line
-- but we discard the last word of each line.
-- I assume we don't need to pad there.
-- except, if left-padding, but
-- I noticed too late ;-)
repeat with i from -1 + (count of all_words) to 1 by -1
-- we fetch the word
set a_word to item i of all_words
-- check whether it's a new word
if uniq_words does not contain a_word then
-- if it's new we store it
copy a_word to the end of uniq_words
-- get it's length
set word_length to length of a_word
-- calculate the current maximum
if word_length > overall_length then set overall_length to
word_length
end if
end repeat
end repeat
set AppleScript's text item delimiters to old_text_item_delimiters
-- now we create a filler, meaning
-- a string of blanks, one blank longer than the
-- longest word
set filler to " "
repeat with i from overall_length to 1 by -1
set filler to filler & " "
end repeat
-- Now we take each of our words
repeat with a_word in uniq_words
-- and pad it
set the_clipboard to pad_right(the_clipboard, a_word, filler)
end repeat
return the_clipboard
to pad_right(a_text, a_word, filler)
set old_text_item_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to a_word & tab
-- Trick 2: we split at the word, followed by a tab
set splitted to text items of a_text
set AppleScript's text item delimiters to text 1 thru (length of
filler) of (a_word & filler)
-- and put together with enough spaces instead of the tab
set a_text to splitted as text
set AppleScript's text item delimiters to old_text_item_delimiters
return a_text
end pad_right
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden