Using grep function in applescript

Hi.

I need to find a command that delet some part of text. I can use BBEdit for this, wich have a wanderfull grep ability, but best for me is to rest in AppleScript, or use “do shell script” command

set remotesource to "http://www.siteofmusic.com/company.php"  -- this site is an example
set the_html to (do shell script "curl " & remotesource)

(*
example of remotesource can be

<song artista="MARTIN SOLVEIG" titolo="SOMETHING BETTER" direzione="+" preview="MARTIN SOLVEIG - Something better.mp3" suoneria="" />
<song artista="BOB SINCLAR" titolo="TENNESSEE" direzione="+" preview="BOB SINCLAR - Tennesse (Fuzzy Hair RMX).mp3" suoneria="" />
(...)

I need this result:
MARTIN SOLVEIG - Something better
BOB SINCLAR - Tennesse (Fuzzy Hair RMX)

*)

I need to eliminate averything exept name of the artist and title or song.
It’s easy to eliminate

but problem is that the artist name and the title name needs a grep function.
Any ideas?

Hi,

it would be easier to have a real website, but try the following

set remotesource to "http://www.siteofmusic.com/company.php" -- this site is an example
set the_html to paragraphs of (do shell script "curl " & remotesource & " | grep 'song artista'")
set titleList to ""
repeat with i in the_html
	set {TID, text item delimiters} to {text item delimiters, quote}
	set ti to text item 8 of i
	set text item delimiters to TID
	set titleList to titleList & text 1 thru -5 of ti & return
end repeat

Yes, you are right!

http://www.radiocompany.com/charts/company_hits/company_hits_xml.php
(page appears blank on browser, but has lot of codes :slight_smile:

Your code is lot complex. i have to ceck and learn all parts…
Who’s whos said that “AppleScript is a EASY way developer?”.

Matteo,

title #3 has no “preview=” value so adding a try block reads all lines with a non empty preview-value

set remotesource to "http://www.radiocompany.com/charts/company_hits/company_hits_xml.php"
set the_html to paragraphs of (do shell script "curl " & remotesource & " | grep 'song artista'")

set titleList to ""
repeat with i in the_html
	set {TID, text item delimiters} to {text item delimiters, quote}
	set ti to text item 8 of i
	set text item delimiters to TID
	try
		set titleList to titleList & text 1 thru -5 of ti & return
	end try
end repeat

Yes.
No problem about this!

Uhm.
Dear Stefank, I’m glad to see that you think I’m able to undestand this script!
Yeah, very glad!

My work, today, is to googling about “text item delimiters” and “thru” and learn everyt

Your script works fine: the final question is: in wich variable is my final text and haw can I save this in a txt file?

(lot of) Regards

Allora, vorrei spiegartelo (ma preferisco l’inglese) :wink:

All values are quoted e.g “MARTIN SOLVEIG” in your example

<song artista="MARTIN SOLVEIG" titolo="SOMETHING BETTER" direzione="+" preview="MARTIN SOLVEIG - Something better.mp3" suoneria="" />

With the commands set text item delimiters to quote and text items
you get a list of strings elements which were separated by the quotes:

{"<song artista=", "MARTIN SOLVEIG", "  titolo=", "SOMETHING BETTER", " direzione=", "+", " preview=", "MARTIN SOLVEIG - Something better.mp3", " suoneria=", "", "/>"}

The 8th list element is the desired value, and finally I cut off the extenion

the result is a string titleList(not a list), which can be stored directly in a text file

Saluti a Milano

We live in a (very) little world :slight_smile:

Yes, I’ve googled a lot, and a lot and a lot.
Now I’ve undestand what you mean: but I can’t undestant only a think (in true, a lots, but only one in this script)

In the top of repeat circle

set {TID, text item delimiters} to {text item delimiters, quote}
-- do domething
set text item delimiters to TID

Wht you set the TID variable? And why return TID to text item delimiters? text item delimiters are not ALWAYS quote?

OK. You mean that I’m able to do this. Yeah!
So… uhm… let me think…
“do shell…”
no,
“tell applica…”
no…

OK.
Google, I’m coming!

text item delimiters (or within a tell block Applescript’s text item delimiters is a internal constant which is a list of an empty string {“”} by default.
To set it, put the current value into a variable (that’s TID), set the delimiter, proceed your code and restore the old value to avoid unexpected behavior.
Saving the delimiter into TID and setting it to quote is performed in one line.

Yes, I guessed that :wink:

here’s the whole script, which creates a text file “radiocompany.txt” on desktop

set remotesource to "http://www.radiocompany.com/charts/company_hits/company_hits_xml.php"
set the_html to paragraphs of (do shell script "curl " & remotesource & " | grep 'song artista'")

set titleList to ""
repeat with i in the_html
	set {TID, text item delimiters} to {text item delimiters, quote}
	set ti to text item 8 of i
	set text item delimiters to TID
	try
		set titleList to titleList & text 1 thru -5 of ti & return
	end try
end repeat
set textfile to (path to desktop as Unicode text) & "radiocompany.txt"
set datastream to open for access file textfile with write permission
write titleList to datastream
close access file textfile