Copy and Paste Between Objects

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

:smiley:

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

:lol: I got it :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
:slight_smile:

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] new 1 1 1. Allegro moderato Symphony for strings No.9 in c minor AUDIO 0:07:30 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 I. Allegro moderato 54545454545 Symphony for strings No.9 in c minor Y Y Y Y Y Y Y new 1 2 II. Poco adagio Symphony for strings No.9 in c minor AUDIO 0:08:00 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 II. Poco adagio 54545454545 Symphony for strings No.9 in c minor Y Y Y Y Y Y Y new 1 3 III. Scherzo Symphony for strings No.9 in c minor AUDIO 0:02:45 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 III. Scherzo 54545454545 Symphony for strings No.9 in c minor N Y Y Y Y Y Y new 1 4 IV. Allegro moderato Symphony for strings No.9 in c minor AUDIO 0:06:57 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 IV. Allegro moderato 54545454545 Symphony for strings No.9 in c minor N Y Y Y Y Y Y new 1 5 Symphony No.10 for strings in B minor, Op.posth. AUDIO 0:07:31 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 Symphony No.10 for strings in B minor, Op.posth. 54545454545 Symphony No.10 for strings in B minor, Op.posth. Y Y Y Y Y Y Y new 1 6 I. Grave - Allegro Symphony No.12 for strings in G minor, Op.posth. AUDIO 0:05:15 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 I. Grave - Allegro 54545454545 Symphony No.12 for strings in G minor, Op.posth. N Y Y Y Y Y Y new 1 7 II. Andante Symphony No.12 for strings in G minor, Op.posth. AUDIO 0:05:45 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 II. Andante 54545454545 Symphony No.12 for strings in G minor, Op.posth. N Y Y Y Y Y Y new 1 8 III. Allegro molto Symphony No.12 for strings in G minor, Op.posth. AUDIO 0:06:15 Blah Classical (P) 1966 Blah The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 III. Allegro molto 54545454545 Symphony No.12 for strings in G minor, Op.posth. N Y Y Y Y Y Y new 1 9 Allegro Concerto in A Minor for piano and strings AUDIO 0:12:47 Blah Classical (P) 1970 Blah John Ogdon [Piano] The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] John Ogdon [Piano] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 Allegro 54545454545 Concerto in A Minor for piano and strings Y Y Y Y Y Y Y new 1 10 Adagio Concerto in A Minor for piano and strings AUDIO 0:07:28 Blah Classical (P) 1970 Blah John Ogdon [Piano] The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] John Ogdon [Piano] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 Adagio 54545454545 Concerto in A Minor for piano and strings Y Y Y Y Y Y Y new 1 11 Allegro ma non troppo Concerto in A Minor for piano and strings AUDIO 0:06:19 Blah Classical (P) 1970 Blah John Ogdon [Piano] The Academy of St. Martin in the Fields [Orchestra] Sir Neville Marriner [Conductor] Sir Neville Marriner [Conductor] John Ogdon [Piano] Mendelssohn, Felix [Composer] Mendelssohn The Academy of St. Martin in the Fields [Orchestra] 54545454545 Allegro ma non troppo 54545454545 Concerto in A Minor for piano and strings N Y Y Y Y Y Y

Brian :wink:

Hi Brian,

this should do it, the text to parse in variable a is shortened :wink:

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