Textedit Copy Text thats "starts with" and "ends with"

I am 100% new… I tried to do this in automator and found out there not enough tools to use for me to do what i want.
What I am doing is getting applescript to copy the source of a webpage from safari and put into into TextEdit. I have accomplished this, thanks to a script I found.

Now once the the HTML is in Textedit I need to be able to search for a URL that will change. Example the URL I want to find is http://home.com/file123.mp3 however “home.com/file123” Changes depending on the page I am on. So I need applescript to search with “starts with” http:// and “ends with” .mp3 then copy the whole URL, close the Textedit doc and open the URL in safari as a new Tab. Any One want to show me how to do this?

Thanks

I don’t think you’ll be able to do that with TextEdit. However, this seems to work:

tell application "Safari"
	launch
	get source of front document
end tell

-- Have the grep UNIX tool find the URLs
do shell script "/bin/echo " & quoted form of result & " | /usr/bin/grep -o 'http://.\\+mp3'"
-- Turn the result into a list that AppleScript can use
set URLs to paragraphs of result

repeat with thisURL in URLs
	--This will use the default browser
	open location thisURL
end repeat

That is exactly what I needed thank you so much!

Now that I played with it there is one annoyance. When the audio file opens in safari it opens as Tab in the forground I would much prefer it open behind my other tabs? Is there a solution for that as well?

Not that I’m aware of.

Bruce is correct - the tab has to be open to do something with it. If you want to do this in the background, you’ll have to use curl. You can just close that tab (the front document) as the next step after the “grab”. Then you’ll be back where you were.

If you want to download the file, there would be a few ways of doing it.

If you’re certain that you’ll only find one link, then you could do something like this:

property downloadFolder : path to desktop

tell application "Safari"
	launch
	get source of front document
end tell

do shell script "/bin/echo " & quoted form of result & " | /usr/bin/grep -o 'http://.\\+mp3'"
get first item of paragraphs of result

do shell script "cd " & quoted form of POSIX path of downloadFolder & "; /usr/bin/curl -sSO " & quoted form of result

This is work around for my job so I can work from home without a PC. My job is to listen to recorded MP3 and evaluate the content. However, since my work uses embedded media player files I can not listen to the audio file in safari. Thanks to the help i have gotten here, I have been able to open the audio file from the information found in the HTML and open it in a new tab and since Quicktime is setup to automatically play the files it does. However that requires me to click back to my first page to fill it out, but after doing a hundred and so that is a real slow down. So I was hoping there was a way to open the audio file in a new tab behind the first page were it got the address for the audio file. Any thoughts?

Perhaps you could use QuickTime Player to listen.

tell application "QuickTime Player"
	launch
	close every window
end tell

tell application "Safari"
	activate
	get source of front document
end tell

try
	do shell script "/bin/echo " & quoted form of result & " | /usr/bin/grep -o 'http://.\\+mp3'"
	set theURL to first item of paragraphs of result
on error
	display dialog "No URLs found." buttons {"Cancel"} default button 1
end try

tell application "QuickTime Player"
	open location theURL
	play front movie
end tell

Even though it does not open in in safari. This will work nicely. Thanks to everyone who helped me with this.