making a list, checking it twice...

I’m running into a little wall with a script I am writing…

I have a database which we use for our sales staff to view ads appearing in our magazines. We recently moved the db over from Cumulus to MySQL and I am now doing some spring cleaning on the folders. I have a table I use for maintenance which contains the magazine code, issue date, jpeg count, pdf count and a count of duplicate jpegs (these are in the issue folder, not the jpeg folder, has to do with how we had to set things up for Cumulus). The JPEGs and PDFs are ads that appear in that issue of the magazine - jpeg is for browser viewing, pdf for downloading/emailing to clients.

So I am trying to write a script that cleans out these duplicate jpegs. I make a SQL call and get the list of all magCodes. I am then do another SQL call that returns all issues of all mags whose duplicate jpeg field > 0. I have about 27 mag codes. What I want to do is loop through each mag code one at a time, going through each mags issues and delete the wayward JPEGs. I’ll then have the app zip me an email to let me know it’s done XX mag. What I have for this part of my script is this:

repeat with thisMag in magList
set workList to every paragraph of theResult that starts with thisMag
return workList
end repeat

but when I go to compile it I get this syntax error:

Expected expression but found “starts with”.

Why won’t that work? I know I can compile the workList by setting a variable and checking to see if it contains the magCode and then copying it to the end of a list but I was looking to simplify the code.

Here’s an example of what I am working with…thanks for any help.

set magList to {“AA”, “AC”}
set theResult to {“AA 08292005 25”, “AA 08282006 31”, “AA 06272005 15”, “AA 12262005 19”, “AA 10312005 29”, “AA 10302006 34”, “AC 02142005 9”}


some notes:

¢ paragraphs don’t exist in lists :wink:
¢ that is no AppleScript’s expression

But this doesn’t matter actually. AppleScript can’t parse lists by filtering the items
because list items aren’t elements with properties.

The only way is to check every single item as you described

I mistyped - the results come back as a string delimited by ASCII Char 13, hence the use of paragraphs:

“magCode issueDate dupJPEGCount
AA 08292005 25
AA 08282006 31
AA 06272005 15
AA 12262005 19
AA 10312005 29
AA 10302006 34…”

doesn’t work either, same reason

Perhaps something like this:

set myList to "magCode    issueDate    dupJPEGCount
AA    08292005    25
AC    08282006    31
AA    06272005    15
AA    12262005    19
AB    10312005    29
AA    10302006    34"
set P to paragraphs 2 thru -1 of myList
set ML to {"AA", "AB", "AC"}
set C to count ML
set SL to {}
repeat C times
	set end of SL to {}
end repeat
--> {{},{},{}}

repeat with k from 1 to count ML
	repeat with aP in P
		if word 1 of contents of aP is item k of ML then set end of item k of SL to contents of aP
	end repeat
end repeat
--> {{"AA    08292005    25", "AA    06272005    15", "AA    12262005    19", "AA    10302006    34"}, {"AB    10312005    29"}, {"AC    08282006    31"}}

I have been pondering a question similar to this, where I’ve got the following code block (assuming a dropped text file called drop):

set futurelines to {}
set pastlines to {}
repeat with oneline in (get every paragraph of (read drop))
	if text of oneline contains "Future" then
		set end of futurelines to date oneline
	else if text of oneline contains "Past" then
		set end of pastlines to date oneline
	end if
end repeat

This works perfectly well, but I was hoping for a formulation like this (pseudocode):

set futurelines to (every paragraph of (read drop) which contains text "Future")

If I understand Stefan’s reply correctly, that won’t work because each paragraph is a list item, and this line of Applescript by itself is unable to access the text (ie the text property) of each paragraph, is that correct? Is there any other way to slim down this code (not that it’s horrible as-is, but just for the sake of comprehension)?


Hi Eric,

paragraph in the Standard Text Suite has no property contents,
neither its property quoted form nor the following form works to filter single paragraphs.

set futurelines to (every paragraph of (read drop) whose it contains "Future")

But you can use TextEdit to filter the information, because TextEdit understands whose it

tell application "TextEdit"
    open alias drop
    tell document 1
        set futurelines to (every paragraph whose it contains "Future")
    end tell
end tell

Okay, that’s very good to know about the differences between TextEdit and Standard Text, though it’s going to take a while longer before the usage of ‘it’ fully sinks in, I think. Thanks for your help!



An alternative to ‘whose’ in a filter is ‘where’. That can sometimes make things easier to understand:

tell application "TextEdit"
	open alias drop
	tell document 1
		set futurelines to (every paragraph where it contains "Future")
	end tell
end tell