Categories: Misc / DotNet / Java / Coder / Linux / PHP Ask - La ask - La Answer

String assistance

I have been doing a lot of applescript since my last post, but right now I am kind of stuck on a certain thing.

I have this script:

tell application "iTunes"
return database ID of (every track of current playlist)
end tell

Which I use to get the songs of a current playlists, obviously. I want to turn this into a string that I can send back to my cocoa app...but I am unsure as to how to get the number of items in the playlist and then how to format the string. Writing it in sudo code, here is what I would do...

A = 0
set a to numberOfItems
set returnString to ""
Repeat Until A = numberOfItems
returnString = returnString + item[A]
End Repeat
Return returnString

How can I do that in applescript? I will let Cocoa do all the parsing on the other side.

Can anyone help?

Mat
[874 byte] By [mj_1903] at [2007-11-9 14:21:20]
# 1 Re: String assistance
hi mat,

set x to database ID of (every track of current playlist)

returns the id of all tracks as a list , does it need to be a string that is returned since the number of items in the list is also the count of tracks in the playlist...

if it must be a string then you could loop through the list returned using

set to_return to ""
repeat with an_item in x
set to_return to (to_return & " " & an_item) as string
end repeat

if you need to find the number of tracks then..

set y to count of tracks of current playlist

will give you that..

you could then concatenate y + " " + to_return and then the count of tracks would be the first word of the string... or you could just count the words of to_return to get the number of tracks..
deeg at 2007-11-15 17:25:02 >
# 2 Re: String assistance
but I am unsure as to how to get the number of items in the playlist and then how to format the string.

Do something like:

tell application "iTunes"
set theIDs to database ID of (every track of current playlist)
set listLength to the count of items in theIDs
set theIDs to theIDs as string
end tell

Now, I'm not sure how you're passing data to Cocoa, since I don't know Cocoa. But your variables, listLength, and theIDs will contain the info you want, the length of the array, and the array as a string.

I've not tested the script, so it may need tweaking. Plus deeg or Doug or DeltaTee might have a more efficient means of accomplishing the same.
eustacescrubb at 2007-11-15 17:26:02 >
# 3 Re: String assistance
hi E,

how is the family coming along ? getting much sleep yet ?:D

one thing with the

set theids to theids as string

is that you end up with a single string of continuous digits rather than space deliminated items..so would need some parsing code at the receiving end.. does itunes adjust the db id of all the tracks as the number of characters in the id increases e.g...

if you have 999 songs then the id's could be 3 characters, at 1000 do all the id's become 4 characters or just the thousandth song.. not something i looked into...
deeg at 2007-11-15 17:27:08 >
# 4 Re: String assistance
I think he wanted the database IDs to get spit out as one long string. I assumed he was going to manipulate the string in his Cocoa application.

The family is coming along nicely, and the sleep is getting beter. Don't have much time for applescripting though. Not that I mind. :)
eustacescrubb at 2007-11-15 17:28:03 >
# 5 Re: String assistance
Yup, just wanted one long string, and thats what I got. Thanks for the code.
mj_1903 at 2007-11-15 17:29:13 >
[an error occurred while processing this directive]
[an error occurred while processing this directive]