Return lines of text as List

Hi All,

I have a web based app that send me emails when a client requests an image(s) from our site. I control the subject line so I’m thinking of using a Mail rule to trigger the script. The text looks like this.

—Sample Text
This lightbox contains the following files:
**
FUMC/Job_0701-03_Amy/0701-03_004.jpg
FUMC/Job_0701-03_Amy/0701-03_024.jpg
FUMC/Job_0701-03_Amy/0701-03_079.jpg
FUMC/Job_0701-03_Amy/0701-03_126.jpg
FUMC/Job_0701-03_Amy/0701-03_176.jpg
FUMC/Job_0701-03_Amy/0701-03_204.jpg
FUMC/Job_0701-03_Amy/0701-03_297.jpg
FUMC/Job_0701-03_Amy/0701-03_303.jpg
FUMC/Job_0701-03_Amy/0701-03_412.jpg
FUMC/Job_0701-03_Amy/0701-03_414.jpg
FUMC/Job_0701-03_Amy/0701-03_417.jpg
FUMC/Job_0701-03_Amy/0701-03_439.jpg
FUMC/Job_0701-03_Amy/0701-03_461.jpg
FUMC/Job_0701-03_Amy/0701-03_491.jpg
FUMC/Job_0701-03_Amy/0701-03_507.jpg
FUMC/Job_0701-03_Amy/0701-03_525.jpg
FUMC/Job_0701-03_Amy/0701-03_545.jpg
FUMC/Job_0701-03_Amy/0701-03_557.jpg
FUMC/Job_0701-03_Amy/0701-03_612.jpg
FUMC/Job_0701-03_Amy/0701-03_588.jpg
**
—End Sample Text

What I need to do is:

  1. Extract the text between the “**” from the email. (I have this done)
  2. Use TextWrangler to sort the lines of text. (I have this done)
  3. Extract each line of text to a list so each line is an item in the list.

From there I can get each section of the path by setting text delimiters to “/”. The overall structure will always be the same ClientName/Job_Number/ImageName.jpg

Here is the code I’m testing with.


set theText to "
This lightbox contains the following images:
**
FUMC/Job_0701-03/0701-03_557.jpg
FUMC/Job_0701-03/0701-03_588.jpg
FUMC/Job_0701-03/0701-03_004.jpg
FUMC/Job_0701-03/0701-03_024.jpg
FUMC/Job_0701-03/0701-03_079.jpg
FUMC/Job_0701-03/0701-03_126.jpg
FUMC/Job_0701-03/0701-03_176.jpg
FUMC/Job_0701-03/0701-03_204.jpg
FUMC/Job_0701-03/0701-03_297.jpg
FUMC/Job_0701-03/0701-03_303.jpg
FUMC/Job_0701-03/0701-03_412.jpg
FUMC/Job_0701-03/0701-03_414.jpg
FUMC/Job_0701-03/0701-03_417.jpg
FUMC/Job_0701-03/0701-03_439.jpg
FUMC/Job_0701-03/0701-03_461.jpg
FUMC/Job_0701-03/0701-03_491.jpg
FUMC/Job_0701-03/0701-03_507.jpg
FUMC/Job_0701-03/0701-03_525.jpg
FUMC/Job_0701-03/0701-03_545.jpg
FUMC/Job_0701-03/0701-03_612.jpg
**"

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "**"

set lightboxOrder to text items 2 through -2 of theText

set AppleScript's text item delimiters to ASCII character 13 --Also tried 10

set imageList to every text item of lightboxOrder as list
set AppleScript's text item delimiters to ""
--return imageList
tell application "TextWrangler"
	sort lines imageList
end tell
set sortedList to result

I’m trying to get something like sortedList {FUMC/Job_0701-03/0701-03_004.jpg, FUMC/Job_0701-03/0701-03_024.jpg, etc}
then from there I need to split each line into pieces “FUMC” in this case, the job number without “Job_” 0701-03 and the image name. The number of lines will change but the pattern will always be the same.

Thanks a bunch,
Mark

Model: G5 2.0gHz DualCore Power PC
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Something like this where I’ve sorted by image number:

set theText to "
This lightbox contains the following images:
**
FUMC/Job_0701-03/0701-03_557.jpg
FUMC/Job_0701-03/0701-03_588.jpg
FUMC/Job_0701-03/0701-03_004.jpg
FUMC/Job_0701-03/0701-03_024.jpg
FUMC/Job_0701-03/0701-03_079.jpg
FUMC/Job_0701-03/0701-03_126.jpg
FUMC/Job_0701-03/0701-03_176.jpg
FUMC/Job_0701-03/0701-03_204.jpg
FUMC/Job_0701-03/0701-03_297.jpg
FUMC/Job_0701-03/0701-03_303.jpg
FUMC/Job_0701-03/0701-03_412.jpg
FUMC/Job_0701-03/0701-03_414.jpg
FUMC/Job_0701-03/0701-03_417.jpg
FUMC/Job_0701-03/0701-03_439.jpg
FUMC/Job_0701-03/0701-03_461.jpg
FUMC/Job_0701-03/0701-03_491.jpg
FUMC/Job_0701-03/0701-03_507.jpg
FUMC/Job_0701-03/0701-03_525.jpg
FUMC/Job_0701-03/0701-03_545.jpg
FUMC/Job_0701-03/0701-03_612.jpg
**"

set P to paragraphs 4 thru -2 of theText
set N to {}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
repeat with aP in P
	set N's end to last text item of aP
end repeat
set AppleScript's text item delimiters to tid

set {N, P} to sort_items(N, P) -- sorting by the number before jpg.
P

to sort_items(sortList, SecondList)
	tell (count sortList) to repeat with i from (it - 1) to 1 by -1
		set s to sortList's item i
		set r to SecondList's item i
		repeat with i from (i + 1) to it
			tell sortList's item i to if s > it then
				set sortList's item (i - 1) to it
				set SecondList's item (i - 1) to SecondList's item i
			else
				set sortList's item (i - 1) to s
				set SecondList's item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > sortList's end then
			set sortList's item it to s
			set SecondList's item it to r
		end if
	end repeat
	return {sortList, SecondList}
end sort_items

EDIT: I should add that you can create a text file directly from AppleScript and print it as well. To put the sorted file back into paragraph form:


set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set P to P as text
set AppleScript's text item delimiters to tid
-- now P is one line per paragraph again

set tFile to ((path to desktop as text) & "Lightbox.txt")
set tDoc to open for access tFile with write permission
try
	set eof of tDoc to 0 -- erases it
	write P to tDoc
	close access tDoc
on error e
	close access tDoc
	display dialog e -- what went wrong
end try
tell application "Finder" to print tFile

Hi Adam,

That works however I may not have been clear about the message contents. The message will often contain other info depending on what the user enters in the form. The entire message will look more like this.

—Start Sample Text
Message: Please let me know as soon as the images are ready.

Order Date: Wednesday, March 7, 2007 at 16:44:53

Requested Delivery Date: ASAP

Requested Size & Resolution: @ 300dpi,JPEG

This lightbox was sent on Wednesday, March 7, 2007 at 16:44:53 and will remain available for viewing for the next 365 days.


This lightbox contains the following files:
**
FUMC/Job_0701-03/0701-03_004.jpg
FUMC/Job_0701-03/0701-03_024.jpg
FUMC/Job_0701-03/0701-03_079.jpg
FUMC/Job_0701-03/0701-03_126.jpg
FUMC/Job_0701-03/0701-03_176.jpg
FUMC/Job_0701-03/0701-03_204.jpg
FUMC/Job_0701-03/0701-03_297.jpg
FUMC/Job_0701-03/0701-03_303.jpg
FUMC/Job_0701-03/0701-03_412.jpg
FUMC/Job_0701-03/0701-03_414.jpg
FUMC/Job_0701-03/0701-03_417.jpg
FUMC/Job_0701-03/0701-03_439.jpg
FUMC/Job_0701-03/0701-03_461.jpg
FUMC/Job_0701-03/0701-03_491.jpg
FUMC/Job_0701-03/0701-03_507.jpg
FUMC/Job_0701-03/0701-03_525.jpg
FUMC/Job_0701-03/0701-03_545.jpg
FUMC/Job_0701-03/0701-03_557.jpg
FUMC/Job_0701-03/0701-03_612.jpg
FUMC/Job_0701-03/0701-03_588.jpg
**

BW Studios Client Area
----End Sample Text

I have the “**” in the message so it can be used to isolate just that section. Since the user can enter various other info in the form the overall message can change from one to the next but there will always be a section like:
**
FUMC/Job_0701-03/0701-03_612.jpg
FUMC/Job_0701-03/0701-03_588.jpg
**
The user has no ability to effect this section since I determine the format in the PERL script that sends the message.

Any Ideas?

Thanks So Much,
Mark

Model: G5 2.0gHz DualCore Power PC
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)