Where can I find a good Applescript/shell text manipulation tutorial?

Does anyone know of a good Applescript text manipulation tutorial, not using TextEdit?

The subject has received ample treatment in these wonderful forums, but some scripting solutions refer to TextEdit, some to Grep, some to Awk, etc. I don’t know which way to turn. Awk seems economical and elegant, yet very cryptic
I have a shell script to put a Mail message into a text file, and a shell script to download a file with Curl.
I need to be able to do at least 2 things:

  1. search the text for a URL, and use the URL with the Curl script.
  2. delete specific paragraphs and words, and add words to specific paragraphs

I appreciate the wealth of knowledge here at Macscripter.

For my money Text Wrangler is the best… um, and it’s FREE!!!

You might be able to do it directly in AppleScript using text item delimiters:

set theFile to choose file
set TheText to read theFile
set OldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set TheText to every text item of TheText
set AppleScript's text item delimiters to ""
set TheText to TheText as string
set AppleScript's text item delimiters to "http"
set x to count of text items of TheText
set TheURLs to text items 2 through x of TheText
set AppleScript's text item delimiters to " "
set the last item of TheURLs to the first text item of (the last item of TheURLs)
set AppleScript's text item delimiters to OldDelim
repeat with i from 1 to count of TheURLs
	set item i of TheURLs to "http:" & item i of TheURLs
end repeat
return TheURLs

This could probably be cleaned up a bit but it appears to work for me and does not require another application.

Hi molinus…

Sometime ago I needed to extract specific urls from a file, and I found this on the forum, I don’t remember where or who made it, but it might do what you are looking for…


--set theFile to choose file
--set TheText to read theFile

set TheText to "http://zombo.com This is a bunch of ugly text with URLs littered in it
http://bbs.applescript.net/viewtopic.php?id=13333 yadda yadda
yadda http://www.macscripter.net/ yadda yadda yadda mailto:John@doe.com yadda yadda 
yadda yadda http://www.yahoo.com yadda yadda 
yadda yadda http://apple.com/applescript
ftp://ftp.gimp.org/
aim:JohnDoe"

getURLs(TheText)

on getURLs(sourceText)
	set endDelim to " " (* change this value if you're looking for
	 something other than a space at the end of your URLs *)
	set URLList to {}
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "
" -- strip carriage returns
	set sourceText to text items of sourceText
	set AppleScript's text item delimiters to endDelim
	set sourceText to sourceText & endDelim as string
	set URISchemes to {"http:", "https:", "file:", "ftp:", "mailto:", "aim:", "telnet:", "news:", "rtsp:", "afp:", "eppc:", "rss:"}
	repeat with delim from 1 to length of URISchemes
		set delim to item delim of URISchemes as string
		set AppleScript's text item delimiters to delim
		set trimText to {}
		repeat with x from 2 to length of sourceText's text items
			set trimText to trimText & (text item x of sourceText) as list
		end repeat
		set AppleScript's text item delimiters to endDelim
		repeat with x from 1 to length of trimText
			set URLList to URLList & (delim & text item 1 of item x of trimText) as list
		end repeat
	end repeat
	set AppleScript's text item delimiters to oldDelims
	return URLList
end getURLs

-- {"http://zombo.com", "http://bbs.applescript.net/viewtopic.php?id=13333", "http://www.macscripter.net/", "http://www.yahoo.com", "http://apple.com/applescript", "ftp://ftp.gimp.org/", "mailto:John@doe.com", "aim:JohnDoe"}


About the search/replace thing, Bruce Phillips created this nice script http://bbs.macscripter.net/viewtopic.php?id=18551

/mkh

Thanks much for all the great responses.
Scott - TextWrangler is fantastic, and I will use it for database administration.

mkh and Jerome - the scripts you posted are very helpful, and I will be using them.

Please forgive my ignorance - I am new to Applescript, and while it seems more robust and powerful than Visual Basic is for PC, I am used to simple Find, Replace, and Select commands to parse and manipulate text.
I was hoping to find a link or PDF that has comprehensive illustration of the reasons why you write code certain ways in given situations.
For instance, my questions based on other search and replace scripts I have seen posted:

  1. Why would one need to strip out carriage returns?
  2. Why does one script refer to “paragraphs” and another to “text items” to accomplish the same task?

It looks as though text manupulation in Applescript is all about how delimiters are defined.

Sorry to post again before any responses, but I am looking for a guide that can illustrate how to treat the following when scripting.
How would someone treat the following sample tasks:

Read a text file, find a sentence with the word “goober” in it, and delete all the sentences after it.

Read a text file, find a sentence with the word “goober” in it, and delete all the sentences after it, except the last 2 sentences?

Read a text file, find a sentence the starts with the word “goober”, and make the sentence a variable to be used in another part of the script.

Read a text file and sort the sentences alphabetically by first word.

I am hoping there is a guide that shows the approach to take with scripting tasks like these.

Thanks

Eureka! :smiley:

Right here in MacScripters, courtesy of Administrator Adam Bell, is the tutorial I was seeking. I just didn’t look in the right places before.

For anyone having issues with understanding text manipulation (Search, Replace, etc), visit:
the MacScripter

for the juicy details.

I have read a half a dozen Applescipt guides, and none of them illustrate the utility of text item delimiters as well as this.

Thanks Adam!