Spreading out a string to 40 characters

Hi folks!

I have this text file:

Name:John Smith Address:North Road, 123

Need to insert spaces in between “Name:” and “Address:”, so that the total of caractheres is 40.

The script must count the number of caractheres that exist in between “Name:” and “Address:” and calculate the spaces needed to reach the value of 40 caractheres allways, names change from file to file.

How can it be done?

Thanks!

I’m sure there are much faster ways of doing this and it doesn’t account for strings that are longer than 40 characters but this should get you started:

set the_string to "Name:John Smith Address:North Road, 123"
set target_length to 40
set the_delim to "Address:"
set the_string to my pad_the_string(the_string, target_length, the_delim)

on pad_the_string(the_string, target_length, the_delim)
	set the_count to (count the_string)
	if the_count = target_length then
		return the_string
	else
		set the_pad to (target_length - the_count)
		set old_delim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to the_delim
		set the_items to text items of the_string
		set AppleScript's text item delimiters to old_delim
		set item_one to item 1 of the_items
		repeat the_pad times
			set item_one to item_one & " "
		end repeat
		return item_one & the_delim & (items 2 thru -1 of the_items) as string
	end if
end pad_the_string

Jon

Dear Jon,

I think I did not explain myself fully.

I need to send text files to my Banks Database, so they will process my drafts…

The text files differ on the name and addresses of my customers. But the words “Name:” and “Address:” are always present…

The banks software will identify the records by its position in the text. To tell you the truth, the marks “Name:” and “Address:” will be removed by my script, once the text in arranged into the right position, by simply “look for “Name:” and replace with “””.

So, I need to place spaces in the example, so that I get 40 caractheres in between the first letter “J” of “John Smith” and the letter “A” of “Address”.

Please keep in mind that the script has to process other files with other different names within…

Thanks again!