Hello,
I would like to create a script that copies information from between objects…for example, <track_title>1. Allegro moderato</track_title>
I would like the script to copy the “1. Allegro moderato” from between the <track_title> and the <track_title>.
Any Suggestions?
Thanks,
Brian
Hi Brian,
three options to parse the text:
¢ AppleScript’s text item delimiters
¢ Scripting Addition XMLToolsor the built-in XML capabilities of System Events
¢ the shell with filtering commands like e.g. sed, awk, grep
Where does offset fall?
set incoming_title to "<track_title>1. Allegro moderato</track_title>"
tell (offset of ">" in incoming_title) to set the_title to text (it + 1) through -(it + 2) of incoming_title

This works for copying this information between <track_title> and <track_title>, but what about the ability to copy any given text that is between <track_title> and <track_title>? I get many different types of text between <track_title> and <track_title>, and would like the ability to copy anything that is there…any more suggestions?
Thanks…Oh, and thanks for the script that you sent!
Brian
Well my version will work with anything that falls between “<track_title>” & “</track_title>”… (that I can think of anyways) Why did you test it with something and it didn’t work?
As for text item delimiters that’s pretty asy to do as well. XML parsing is a little more annoying albeit more flexible. Like I said though what string did you test that failed because it shouldn’t.
Hi James,
Your version works great in removing this given text, my problem is that I never have the same text between “<track_title>” & “<track_title>”, so, my actual script should include some kind of variable inside the <track_title>'s. Somthing like this:
set x to given text
set incoming_title to “<track_title>x</track_title>”
tell (offset of “>” in incoming_title) to set the_title to text (it + 1) through -(it + 2) of incoming_title
I know that this example wont work, but hopefully this shows what im after…
Cheers!
Brian
I’m totally confused here… you say
set x to given text
Doesn’t that include the “<track_title>” & “</track_title>”? If it doesn’t why add it and then remove it?
Incoming title is just a name I made up… using x is fine…
set x to given text
tell (offset of ">" in x) to set the_title to text (it + 1) through -(it + 2) of x
James, he wants to use x as the variable for the text, like this
set x to "given text"
set incoming_title to "<track_title>" & x & "</track_title>"
tell (offset of ">" in incoming_title) to set the_title to text (it + 1) through -(it + 2) of incoming_title
Where Im confused though Stefan is why? I thought the original point of the post was to remove the track title from the tags? If it’s already stripped (variable x or whatever) why insert it between tags just to remove it again?
Or am I totally having a brain cramp and not following LOL
It is I who is the confused one…or maybe Im not clear…also, AppleScript is not my Forte…So, I get tracks for CDs that are always different. They all come like this: <track_title>The track name is listed here</track_title>. What I eventually want to do is find the first track, copy the track name, paste it somewhere, then move on to the next track that will have a different name. I hope that clears up some of the confusion that Ive caused…
Brian
Brian,
Okay so so far you have what you need.
set x to given text
tell (offset of ">" in x) to set the_title to text (it + 1) through -(it + 2) of x
does exactly what your looking for… not sure how else to help you. Maybe if you post the beginning part of your script (where you are getting your track title strings WITH <track_title> & </track_title> we can help you parse that out, but as of the moment you should have what you need.
Thanks for all of your help…I will work on it. If I run into more issues, I will post again…
Brian
No worries Brian, I feel bad I can’t be of more service… Where are you getting the track_titles from anyways? iTunes? Where would you like to end up pasting them to?
I am getting the track titles from record lables that need to be edited for the iTunes store. Here is a better example of what I get:
<track_title>1. Allegro moderato</track_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>
What I have to do is copy the track name from the track_hierarchy_title (in this case Symphony for strings No.9 in c minor), then paste it to the beginning of the track_title. So, what I should end up with is:
<track_title>Symphony for strings No.9 in c minor1. Allegro moderato</track_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>
Brian

Hi Brian,
your information about the structure of the lines is very simple,
this is a also a simple example assuming there are only consecutive lines with track_title and track_hierarchy_title lines.
set a to "<track_title>1. Allegro moderato</track_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>
<track_title>2. Andante</track_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>
<track_title>3. Scherzo</track_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>
<track_title>4. Allegro vivace</track_title>
<track_hierarchy_title>Symphony for strings No.9 in c minor</track_hierarchy_title>"
set b to paragraphs of a
repeat with i from 1 to count b by 2
tell (offset of ">" in (item (i + 1) of b)) to set the_title to (text (it + 1) through -(it + 2) of item (i + 1) of b)
set o to (offset of ">" in (item i of b))
set item i of b to (text 1 thru o of (item i of b)) & the_title & " - " & (text (o + 1) thru -1 of item i of b)
end repeat
Hi Stephan,
Wow! This is actually really super awesome…The example you just gave is the most helpful one yet, by far. Now, the lines for the tracks that I get do not come one after another, they come to me in different places in the document. So, I am going to need a way to search for the lines, to be dealt with in the way that you just displayed. Anhy ideas?
Brian
Can you post a full document for us to look through… if need be change all the names so we can’t see them, but really without a sample of the data coming up with a solution is going to be difficult… at least one that is efficent.
Hi James,
Here is an example of a full document from BBEdit:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
new
AU
MEND: String Symphonies Nos. 9, 10, 12; Concerto for Piano & Strings in A
Universal Music Australia Pty. Ltd.
1
11
Y
1:16:32
20-May-2006
Classical
N
(P) 2006 Blah
(C) 2006 Blah
XX
17-May-2006
John Ogdon [Artist]
Sir Neville Marriner [Artist]
Hi Brian,
this should do it, the text to parse in variable a is shortened 
set a to "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<product upc=\"71717171717171\">
<type>new</type>
...
...
<associated_tracks/>
</track>
</tracks>
</product>"
set hierarchyList to {}
set {TID, text item delimiters} to {text item delimiters, "<track_hierarchy_title>"}
set b to text items of a
set text item delimiters to "<"
repeat with i in b
set end of hierarchyList to text item 1 of contents of i
end repeat
set text item delimiters to "<track_title>"
set b to text items of a
repeat with i from 2 to count b
if item i of hierarchyList is not "" then
set item i of b to item i of hierarchyList & " - " & item i of b
end if
end repeat
set b to b as string
set text item delimiters to TID
b