Organising text in column

I have a list of words of different length, and I want “fill” each word with empty spaces until reach the length if 15 characters. So basically I want to add a give number of empty spaces after my word depending in its initial length.

I have made this little script. But I believe that there must be a better way for doing this.

set myList to {"Luciano", "Marc", "a", "supercalifra"}
set myText to ""

repeat with i from 1 to length of myList
	count of (myList's item i)
	length of (myList's item i) -- are those two command equivalent ???
	set MissingSpaces to (15 - (length of myList's item i)) -- this is the total number of spaces I want to add after the word 
	set myText to myText & return & myList's item i
	repeat with i from 1 to MissingSpaces
		set myText to myText & " "
	end repeat
end repeat

I am worried since is it a loop nested into a loop, and usually this slow things down …

(a side question: is there any difference between “count of” or “length of” ?)




This way’s fairly simple simple. It may need modification if any of your words contain more than fifteen characters:

set myList to {"Luciano", "Marc", "a", "supercalifra"}
set newList to {}
set fifteenSpaces to "               " -- This WILL expand to fifteen spaces in your editor if you click the "Open this Scriplet" link!

repeat with i from 1 to length of myList
	set end of newList to text 1 thru 15 of ((item i of myList) & fifteenSpaces)
end repeat

-- {"Luciano        ", "Marc           ", "a              ", "supercalifra   "}

If I understand correctly, count is a command while length is a property but they seem to do the same thing.

A while back I did a google search to see if one was faster than the other. I couldn’t find a lot but I did find one test where count was faster, although this test involved lists rather than a string.

Anyways, I’ve noticed that some of the more knowledgeable members of this forum use count while others use length. I not in the more-knowledgeable group but, FWIW, I prefer count just because it seems a bit clearer to me.

The thread I mention is at:

Back in days of yore, I found count to be very slightly faster than length of with AppleScript lists, and both were faster than number of. So I habitually use count now rather than either of the other two. But the difference was minimal back then and is practically nonexistent now with today’s faster machines. It’s possible that length, being a property, is slightly the fastest of the three when the list variable is referenced — as in length of my myList — but I haven’t tested this for years.

As peavine says, count is a command, so technically the “of” in count of myList is incorrect (as you’ll see if you try changing this to myList’s count), but it appears to be harmless — at the moment, anyway.

count is both a language command and an application command, whereas I believe length and number are only officially properties of AppleScript lists and records. So you’ll find they don’t all work in all situations.

Thanks a lot to both for the discussion and for the feedback on the code.

Maybe, maybe not… :confused:

The ASLG says:

And later:

But the original ASLG gave separate syntaxes for for AS itself and applications, and gave this option for the former:

So it’s not too great a leap to assume that leaving out the each|every parameter might be treated as being equivalent to every item. And if that is what’s happening, it might explain why your myList’s example doesn’t work.

it’s also interesting (well, that might be too strong a word) that count in compiles to count of.

Hi Shane.

Sorry for the slow response. I was out most of yesterday and had difficulty understanding the point you were making when I got back. This morning, I’ve been researching ….

And in between, it says:

Taken together with the bits you quoted, this is clearly saying that if the direct parameter of count is a compound object — an AppleScript list, text, or record, or a container object in an application — it’s the default items of that object which are counted. If the direct parameter’s an expression which evaluates to a list — such as every integer of aList — it’s the items of the specified class which are counted. This is notionally the same as:

set integerList to every integer of aList
count integerList

… although it’s not clear if the intermediate list is actually created and counted or if count goes through the original list counting items of the required class.

myList’s count errors during compilation because the command keyword is being explicitly used as a property keyword. Interestingly, it’s not because count has no direct parameter here. If it’s used without a parameter, it returns 0. This is possibly something to do with the implied target of the command then being the script itself, or the application running it, as suggested by the application syntax given in the 1999 ASLG.

The each keyword isn’t advertised nowadays, but still works. It’s an optional parameter of count, not an alternative to every in AppleScript generally. It seems to be equally happy masquerading as every or with values which are either class names, lists, or expressions evaluating to lists!

set myList to {1, 2, "dog", 3, path to desktop, "aardvark", 4}
count each integer of myList --> 4
count myList each integer --> 4
count each myList --> 7
count each myList's integers --> 4

If both the direct and each parameters are (or evaluate to) lists, the direct one’s ignored:

set myList to {1, 2, "dog", 3, path to desktop, "aardvark", 4}
count myList's integers each myList's aliases --> 1

All these behaviours are the same on my Tiger system.

Note that ( in | of ) is within the optional part between count and compoundValue, not after it. The AS syntax given in the 1999 ASLG is:

The syntaxt which actually works in Tiger thru to Mojave is:

For the application syntax, just substitute referenceToObject for compositeValue. This is also optional if the object’s implied by a tell statement.