How to make this coding shortest?

Hi guys,

The following code is used to find text at the end of the para and to delete the entire para. It works fine.
It takes more time to loop from start to end of the document for every find string.
Is there a way to make the coding in an array or list. Because there are more than 30 text to find.


set delstring to ("(xxxxxxxxx)" & return)
set delstring1 to ("(Xxxxxxxxx)" & return)
set delstring2 to ("yyyyyyyyy" & return)
set delstring3 to ("Yyyyyyyyy" & return)
set delstring4 to ("ZZZZZZ" & return)
set delstring5 to ("AAAAA" & return)


tell document 1 of application "QuarkXPress"
    activate
    tell current box
        set XYZ to count of paragraphs in story 1
        tell story 1
            considering diacriticals
                considering case
                    repeat with i from 1 to XYZ
                        tell paragraph i
                            try
                                set last_word to characters -1 thru -12
                                if last_word is equal to delstring then
                                    delete every text
                                else if last_word is equal to delstring1 then
                                    delete every text
                                end if
                            end try
                        end tell
                    end repeat
                    repeat with i from 1 to XYZ
                        tell paragraph i
                            try
                                set last_word to characters -1 thru -10
                                if last_word is equal to delstring2 then
                                    delete every text
                                else if last_word is equal to delstring3 then
                                    delete every text
                                end if
                            end try
                        end tell
                    end repeat
                    
                    repeat with i from 1 to XYZ
                        tell paragraph i
                            try
                                set last_word to characters -1 thru -7
                                if last_word is equal to delstring4 then
                                    delete every text
                                end if
                            end try
                        end tell
                    end repeat
                    repeat with i from 1 to XYZ
                        tell paragraph i
                            try
                                set last_word to characters -1 thru -6
                                if last_word is equal to delstring5 then
                                    delete every text
                                end if
                            end try
                        end tell
                    end repeat
                    
                end considering
            end considering
        end tell
    end tell
end tell

Thanks
Mathews

Something like this?

set matches to {}
set end of matches to "(xxxxxxxxx)" & return
set end of matches to "(Xxxxxxxxx)" & return
set end of matches to "yyyyyyyyy" & return
set end of matches to "Yyyyyyyyy" & return
set end of matches to "ZZZZZZ" & return
set end of matches to "AAAAA" & return


tell document 1 of application "QuarkXPress"
	activate
	tell current box
		set XYZ to count of paragraphs in story 1
		tell story 1
			considering diacriticals
				considering case
					-- easier to go in reverse order
					repeat with i from XYZ to 1 by -1
						-- store text in variable for optimization
						set theText to paragraph i
						repeat with match in matches
							if theText ends with match then
								delete every text of paragraph i
								exit repeat
							end if
						end repeat
					end repeat
				end considering
			end considering
		end tell
	end tell
end tell

Hi. I don’t use XPress. There may be a more advisable way to use its internal find to speed up your search; you should investigate its scripting guide for that. Below is an untested option. Minimally, it’s easier for inputting multiple search terms, and it could be combined with the optimization aspect from DJ’s code, if that ends up being more efficient.

set deletions to {"xxxxxxxxx", "Xxxxxxxxx", "yyyyyyyyy", "Yyyyyyyyy", "ZZZZZZ", "AAAAA"}

tell document 1 of application "QuarkXPress"
   activate
   tell current box	--it's unclear how this is determined, and it may not be necessary
	tell story 1
		considering case and diacriticals
			repeat with counter from (count paragraphs) to 1 by -1
				repeat with listItem in deletions
					if exists paragraph counter then
						tell paragraph counter to if ((get characters -((count listItem) + 1) thru -1) as string) is (listItem & return) then delete
					end if
				end repeat
			end repeat
		end considering
	end tell
end tell
end
end

QuarkXPress doesn’t apply considering blocks in it’s own object accessor methods. Therefore simple object specifiers cannot be used for find and replace.

Thank You Guys for your kind reply.

DJ Bazzie Wazzie Your code working fine.

Marc Anthony Your code throws the below error.

“QuarkXPress got an error: Can’t get characters -12 thru -1 of paragraph 26 of story 1 of document 1.”

Thanks

You’re welcome.

I’ve updated the post once more. After deleting I added the line ‘exit repeat’. It can cause an error if the last paragraph is deleted (it no longer exists after it’s deleted so the next iteration it will cause an error) and every check after deleting any other paragraph is redundant.

Had a little play around using Quark object references and this seems to work too.


set deletions to {"ligula.", "nisl.", "orci.", "", "nisi."}

tell document 1 of application "QuarkXPress"
	activate
	
	set objRefs to object reference of every paragraph of every story of every text box
	
	repeat with i from (count of objRefs) to 1 by -1
		repeat with listItem in deletions
			try
				if (contents of item i of objRefs as text) ends with (listItem & return) then delete item i of objRefs
			end try
		end repeat
	end repeat

end tell

HTH

Make sure when working with text boxes that you only pick stories from text boxes without overflow, otherwise you have double references in the object reference list.

Hi DJ,
Not sure what you mean there, think I must be missing something. :confused:

I’ve tried creating a text box with 6 paragraphs and executed the following:


tell document 1 of application "QuarkXPress"
	activate
	set objRefs to object reference of every paragraph of every story of every text box
end tell

I then tried reducing the size of the text box to create an overflow.
Executing the same code seemed to generate the same result?

I’d tested the deletion code and that seemed to work ok on a text box with and without overflow.

An document can contain 1 story but runs in many different text boxes (A markup/design some of my clients uses). When using object references I prefer to work with stories multiple text boxes can contain the same story and therefore the list of object references can point to the same object. In practice the first run you can have paragraph 2 of story 1 of text box 1 and delete it, if the story contains only 2 paragraphs and you access it by text box 2 it will return in an error because paragraph 2 no longer exists.

To know if the text box overflows it’s text to another text box you can read the overflow property. The last text box in linked text boxes will never have overflow to true, just like a text box that is not linked. When you get stories from text boxes that doesn’t overflow you’re sure each story is unique.