Script runs very slow

Hi,

I have written one applescript for Quarkxpress. The script finds each and every number in the currently opened quark file and adds 24 to every number.
The script works very fine but when it goes on and on, it becomes slower and slower in execution.

For example, if the quark file consist 22 page, this script approximately runs for 60-70 minutes.

My script is:


run script index1
display dialog "Done."

script index1
	tell application "QuarkXPress"
		activate
		tell document 1
			tell text box 1
				tell story 1
					set textcount to count of every word
					repeat with i from 1 to textcount
						set numberCheck to true
						set HCheck to false
						set ACheck to false
						set BCheck to false
						set RCheck to false
						try
							set chars to word i as integer
							set chars_f to words (i - 1) through (word i) as string --& chars as string
							--display dialog chars_f
							if (chars_f contains "-") or (chars_f contains "+") or (chars_f contains return) then
								set numberCheck to false
								if (chars_f contains "H-") then
									set HCheck to true
								end if
								if (chars_f contains "A-") then
									set ACheck to true
								end if
								if (chars_f contains "B-") then
									set BCheck to true
								end if
								if (chars_f contains "R-") then
									set RCheck to true
								end if
							end if
						on error
							set numberCheck to true
							set Achars to word i
							set firstchar to item 1 of Achars
							if ((count of Achars) is greater than 3 or (count of Achars) is equal to 2) then
								set Achars to items 2 through last character of Achars
							end if
							try
								set chars to Achars as integer
							on error
								set numberCheck to false
							end try
							if (numberCheck is true and chars is less than 999) then
								set chars to (chars as integer) + 24
								set word i to firstchar & chars
							end if
							set numberCheck to false
						end try
						
						if (numberCheck is true and chars is less than 999) then
							set chars to chars + 24
							set word i to chars
						end if
						
						if (HCheck is true and chars is less than 999) then
							set chars to chars + 24
							set word i to chars
						end if
						
						if (ACheck is true and chars is less than 999) then
							set chars to chars + 24
							set word i to chars
						end if
						
						if (RCheck is true and chars is less than 999) then
							set chars to chars + 24
							set word i to chars
						end if
						
					end repeat
				end tell
			end tell
		end tell
	end tell
end script

Could any one help me how to increase the speed of this script.

Thanks,
Gopal

Try Quark’s ‘do script’ instead of AppleScript’s ‘run script’ to get a faster processing, especially on repeats.

I modified your script a little to illustrate how I reference objects and see what’s going on, and then put the “meat” of your script into a Quark tell block inside the Quark tell block so that it will compile. I think that’s what makes it run faster; it compiles and runs in Quark rather than compiling in AppleScript and then telling Quark what to do.


tell application "QuarkXPress"
	activate
	--this is the script to run on story 1 of text box 1 of document 1
	script index1
		tell application "QuarkXPress"
			--I like to reference the object to keep things clear in my head
			set targetStory to object reference of story 1 of text box 1 of document 1
			
			--then use reference to keep things clear and short in script
			tell targetStory
				set wordcount to count of every word --changed variable name to what it is
				repeat with i from 1 to wordcount --changed variable name to what it is
					set numberCheck to true
					set HCheck to false
					set ACheck to false
					set BCheck to false
					set RCheck to false
					try
						set chars to word i as integer
						set chars_f to words (i - 1) through (word i) as string --& chars as string
						--display dialog chars_f
						if (chars_f contains "-") or (chars_f contains "+") or (chars_f contains return) then
							set numberCheck to false
							if (chars_f contains "H-") then
								set HCheck to true
							end if
							if (chars_f contains "A-") then
								set ACheck to true
							end if
							if (chars_f contains "B-") then
								set BCheck to true
							end if
							if (chars_f contains "R-") then
								set RCheck to true
							end if
						end if
					on error
						set numberCheck to true
						set Achars to word i
						set firstchar to item 1 of Achars
						if ((count of Achars) is greater than 3 or (count of Achars) is equal to 2) then
							set Achars to items 2 through last character of Achars
						end if
						try
							set chars to Achars as integer
						on error
							set numberCheck to false
						end try
						if (numberCheck is true and chars is less than 999) then
							set chars to (chars as integer) + 24
							set word i to firstchar & chars
						end if
						set numberCheck to false
					end try
					
					if (numberCheck is true and chars is less than 999) then
						set chars to chars + 24
						set word i to chars
					end if
					
					if (HCheck is true and chars is less than 999) then
						set chars to chars + 24
						set word i to chars
					end if
					
					if (ACheck is true and chars is less than 999) then
						set chars to chars + 24
						set word i to chars
					end if
					
					if (RCheck is true and chars is less than 999) then
						set chars to chars + 24
						set word i to chars
					end if
					
				end repeat
			end tell
		end tell
	end script
	
	--then have Quark do that script for fastest run
	do script {index1}

end tell