Calculating string lenght

Hello,

I am new to AppleScripting and am having some trouble figuring out how to calculate the number of characters in a given string. Could anyone help me with this?

i.e.


tell application "iTunes"
	set sel to selection
	repeat with aTrack in sel
		set Tname to aTrack's name as string

		set cCount to ....... 

		display dialog "Track name contains " & cCount & " characters" buttons {"Thanks"} default button 1 with icon 2 giving up after 15
	end repeat
end tell

As you can see I need help setting cCount to the number of characters in the Tname.
Thanks

I got this one… :slight_smile:

You were pretty darn close to getting it right.

I added a tiny bit of code in your display dialog statement to let the user know which songs that you calculated the length of the title.

I also removed the coercion (“as string”) at the end of the line that gets the name of the track. It’s already a string – so this will make your script microscopically faster. :slight_smile:

Hope that helps,
Tom

Try this:

tell application "iTunes"
	set sel to selection
	repeat with aTrack in sel
		set Tname to aTrack's name
		set cCount to length of Tname
		
		display dialog Tname & " contains " & cCount & " characters" buttons {"Thanks"} default button 1 with icon 2 giving up after 15
	end repeat
end tell

Thanks :smiley: That was what I was looking for

It’s believed that using “count str” is faster than retrieving “str’s length” (only a note :stuck_out_tongue: )