View Full Version : String assistance
mj_1903
10-02-2003, 09:04 PM
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
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..
eustacescrubb
10-03-2003, 12:49 PM
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.
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...
eustacescrubb
10-04-2003, 06:07 PM
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. :)
mj_1903
10-04-2003, 06:34 PM
Yup, just wanted one long string, and thats what I got. Thanks for the code.