Calculate word length using character widths

Good morning,

I am attempting unsuccessfully to calculate the length of a word for use in a Photoshop script by adding the individual character widths of each letter. When I type the word using each individual character as a list this works fine (first example). However, when I try the same exercise using the word as a variable and setting each character as a list the calculation does not work (second example) as I am unable to sum the list.

Thanking you in anticipation!


set A to 1.96
set B to 1.91
set C to 1.96
set D to 2.2
set E to 1.84
set F to 1.71
set G to 2.18
set H to 2.2
set i to 1.22
set J to 1.36
set K to 1.91
set L to 1.63
set M to 2.51
set N to 2.16
set O to 2.31
set P to 1.8
set Q to 2.31
set R to 2.02
set S to 1.82
set T to 2.09
set U to 2.15
set V to 2.15
set W to 2.93
set X to 1.91
set Y to 1.89
set Z to 1.82

set the_list to {P, H, O, T, O, G, R, A, P, H, Y}
set myChar to 0
repeat with this_item in the_list
	set myChar to myChar + this_item
end repeat
return myChar

result ---->22.76

Please help me put this right!


set A to 1.96
set B to 1.91
set C to 1.96
set D to 2.2
set E to 1.84
set F to 1.71
set G to 2.18
set H to 2.2
set i to 1.22
set J to 1.36
set K to 1.91
set L to 1.63
set M to 2.51
set N to 2.16
set O to 2.31
set P to 1.8
set Q to 2.31
set R to 2.02
set S to 1.82
set T to 2.09
set U to 2.15
set V to 2.15
set W to 2.93
set X to 1.91
set Y to 1.89
set Z to 1.82

set myLoc to "PHOTOGRAPHY"

set the_list to every character of myLoc
return the_list
set myChar to 0
repeat with this_item in the_list
	set myChar to myChar + this_item
end repeat
return myChar

the resulting list ----> {“P”, “H”, “O”, “T”, “O”, “G”, “R”, “A”, “P”, “H”, “Y”}

Model: Trash can Mac 10.9.5
Browser: Firefox 35.0
Operating System: Mac OS X (10.8)

Hi,

try something like this
the variable widthList contains all width values. The script calculates the offset to get the proper value
for example “P” == ASCII 80 minus 64 is equal to 16. The value for “P” is the 16th item of the list.


set widthList to {1.96, 1.91, 1.96, 2.2, 1.84, 1.71, 2.18, 2.2, 1.22, 1.36, 1.91, 1.63, 2.51, 2.16, 2.31, 1.8, 2.31, 2.02, 1.82, 2.09, 2.15, 2.15, 2.93, 1.91, 1.89, 1.82}

set myLoc to "PHOTOGRAPHY"

set totalWidth to 0.0
repeat with this_item in (get every character of myLoc)
	set totalWidth to totalWidth + (item ((id of this_item) - 64) of widthList)
end repeat
return totalWidth


@eaglen:
In your first script the letters are variables. In your second script they are text, and the variables A-Z are never used.
Stefan has the solution: it uses the ASCII code of a character as an index into the list of width values.

If you were using Mavericks or Yosemite, you could get a more accurate value by specifying the font and string, and letting the OS calculate the total length for you. That way things like kerning pairs would be accounted for:

use framework "Foundation"
use framework "AppKit"

on lengthOfText:someText
	-- make an NSString
	set theNSString to current application's NSString's stringWithString:someText
	--define font to suit
	set theFont to current application's NSFont's fontWithName:"Helvetica" |size|:14.0
	-- make attributes dictionary
	set theAttributes to current application's NSDictionary's dictionaryWithObject:theFont forKey:(current application's NSFontAttributeName)
	-- get bounding rectangle of string using font
	set theRect to theNSString's boundingRectWithSize:{width:10000, height:50} options:((current application's NSStringDrawingDisableScreenFontSubstitution) + (current application's NSStringDrawingOneShot as integer)) attributes:theAttributes
	return width of |size| of theRect
end lengthOfText:

its lengthOfText:"The time has come"
-->	119.041999816895

Wow.

Now that is a great handler Shane! :slight_smile:

It can probably be simplified a little:

use framework "Foundation"
use framework "AppKit"

on lengthOfText:someText
	-- make an NSString
	set theNSString to current application's NSString's stringWithString:someText
	--define font to suit
	set theFont to current application's NSFont's fontWithName:"Helvetica" |size|:14.0
	-- make attributes dictionary
	set theAttributes to current application's NSDictionary's dictionaryWithObject:theFont forKey:(current application's NSFontAttributeName)
	-- get size of string using font
	set theSize to theNSString's sizeWithAttributes:theAttributes
	return width of theSize
end lengthOfText:

:smiley: Thanks so much for all your help.
Stefan this works wonderfully.

:smiley: Thanks Shane I’ve just put everything on to my Trash Can mac and that works great too. Thanks so much!