Variable resets rather than incrementing

Hi,

It’s a large script, but I would appreciate any help. I have a photoNum variable which increments by one for each file handled and should tell the script to use one subroutine rather than another if the photoNum number is higher than 1. It goes through the first set of downloaded files and when it checks the FTP site the second time, any new files are being treated as the first rather than continuing where the photoNum variable left off and using the appropriate subroutine. How do I fix this?

~buzzwig

The code:

my UIscript_check() --makes sure that UI scripting is enabled
set theServerAddress to "oooooo.oooooo.com"
set theUserName to "oooooo"
set thePassword to "oooooo"
set photoNum to 0
tell application "Finder"
	if not (exists folder "make gallery" of desktop) then make new folder at desktop with properties {name:"make gallery"} --checks for and creates the local folder
end tell

delay 3

my setupNewstool()
my setupCyberduck(theServerAddress, theUserName, thePassword, photoNum)

on setupCyberduck(theServerAddress, theUserName, thePassword, photoNum)
	--code to open connection
	tell application "Cyberduck"
		activate
		set theBrowser to make new browser
		with timeout of 360 seconds
			tell theBrowser
				set the encoding to "UTF-8"
				set show hidden to false
				connect to theServerAddress as user theUserName with password thePassword
				delay 2
				if not (exists file "make gallery") then create folder "make gallery"
				delay 1
				change folder to "make gallery"
				delay 1
				tell application "Finder" to set fileList to name of (items of folder "make gallery" of desktop)
				repeat
					my galCheck(fileList, photoNum) --actives Cyberduck's sychronize function
					delay 45
					tell application "Finder"
						if exists file "end.jpg" of folder "make gallery" of desktop then exit repeat --initiates end sequence if "end.jpg" is present
					end tell
				end repeat
				my galCheck(fileList, photoNum) --One more time to be sure all files are in.
			end tell
		end timeout
	end tell
	tell application "Finder"
		activate
		display dialog "All gallery files are in!"
	end tell
end setupCyberduck

on galCheck(fileList, photoNum)
	tell application "System Events"
		tell process "Cyberduck"
			activate
			set frontmost to true
			delay 0.3
			keystroke "r" using command down --refresh file list
			delay 2
			my menu_click({"Cyberduck", "File", "Synchronize."})
			keystroke "make gallery"
			delay 0.2
			click button "Choose" of sheet 1 of window "ftp:oooooo@oooooo.oooooo.com"
			delay 0.5
			click button "Continue" of sheet 1 of window "ftp:oooooo@oooooo.oooooo.com"
			repeat while exists button "Stop" of tool bar 1 of window "ftp:oooooo@oooooo.oooooo.com"
			end repeat
		end tell
	end tell
	my gotFiles(fileList, photoNum)
end galCheck

on gotFiles(fileList, photoNum)
	tell application "Finder"
		set listCount to length of fileList
		set newFileList to name of (items of folder "make gallery" of desktop)
		if length of newFileList > 0 then
			if length of newFileList > listCount then
				repeat with thisFile in newFileList
					if thisFile is in fileList then
					else
						set fileList to fileList & thisFile
						set photoNum to photoNum + 1
						my PSInfo(thisFile, photoNum)
					end if
				end repeat
			end if
		end if
	end tell
end gotFiles

on PSInfo(thisFile, photoNum)
	tell application "Adobe Photoshop CS3"
		activate
		set filePath to (path to desktop as string) & "make gallery:" & thisFile as alias
		open filePath showing dialogs never
		set theCredit to credit of info of current document
		if length of theCredit = 0 then set theCredit to "No info"
		set theHeadline to "Photo gallery: " & instructions of info of current document
		if length of theHeadline = 0 then set theHeadline to "No info"
		set theBody to transmission reference of info of current document
		if length of theBody = 0 then set theBody to "No info"
		set iPosition to keywords of info of current document as string
		do action "72dpi" from "Photo gallery set"
		save document 1
		close document 1
	end tell
	if photoNum < 2 then
		my galAdd(thisFile, filePath, theCredit, theHeadline, theBody, photoNum, iPosition)
	else
		my myGal2AndUp(photoNum, thisFile, theBody, theCredit, iPosition)
	end if
end PSInfo

on galAdd(thisFile, filePath, theCredit, theHeadline, theBody, photoNum, iPosition)
	tell application "Finder"
		activate
		display dialog "This is the galAdd routine
for the first photo only." giving up after 4
	end tell
	tell application "System Events"
		tell process "Safari"
			activate
			set frontmost to true
			delay 1
			set value of text area 1 of group 31 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theHeadline --Headline
			delay 0.5
			keystroke theBody
			delay 1.5
			set updateAbstract to position of button 1 of group 84 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" -- Update Abstract button
			set x to (item 1 of updateAbstract) + 5
			set y to (item 2 of updateAbstract) + 5
			click mouse {x, y} -- Update Abstract
			delay 1
			key code 121 --page down
			delay 0.3
			set addNew to position of button 1 of group 93 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			set x to (item 1 of addNew) + 5
			set y to (item 2 of addNew) + 5
			click mouse {x, y} -- Add New Image button
			delay 2
			set chooseFile to position of button 1 of group 1 of group 94 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			set x to (item 1 of chooseFile) + 5
			set y to (item 2 of chooseFile) + 5
			click mouse {x, y} --Choose file
			delay 0.5
			key code 2 using command down --Command D for Desktop
			delay 1
			keystroke "make gal"
			key code 36 --Return to list folder contents
			delay 1
			keystroke thisFile
			delay 1
			key code 36 --Return to choose file
			set value of text area 1 of group 96 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theBody -- Image Caption
			delay 1
			set value of text field 1 of group 98 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theCredit
			delay 1
			set value of text field 1 of group 100 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to iPosition -- photo position
			key code 121 --page down
			delay 0.5
			click button "Add New Section" of group 105 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			delay 0.5
			key code 121 --page down
			delay 0.5
			click pop up button 1 of group 106 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" -- select a section
			delay 0.5
			key code 121 --page down
			delay 0.5
			key code 36 -- Return key to choose section
			delay 0.5
			click button "Add New Section" of group 112 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			delay 0.5
			key code 121 --page down
			delay 0.5
			click pop up button 1 of group 113 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			delay 0.5
			key code 125 -- down arrow
			delay 0.5
			key code 36 -- Return key to choose section
			delay 0.5
			
			set gotOK to 0
			set hitItAgain to 0
			repeat
				click button "Update Article" of group 120 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
				repeat 60 times
					if exists button "OK" of window 1 then
						click button "OK" of window 1
						set gotOK to gotOK + 1
						exit repeat
					else
						delay 1
					end if
				end repeat
				if gotOK ≠ 0 then exit repeat
				set hitItAgain to hitItAgain + 1
			end repeat
		end tell
	end tell
end galAdd

on myGal2AndUp(photoNum, thisFile, theBody, theCredit, iPosition)
	tell application "Finder"
		activate
		display dialog "This is the myGal2AndUp routine
for photos 2 and up." giving up after 4
	end tell
	set UAGroup to (128 + (10 * photoNum) - 20)
	-- Code below is the same for every file from 2 on
	tell application "System Events"
		tell process "Safari"
			activate
			set frontmost to true
			delay 0.5
			click mouse {63, 508} -- clicks away from Article Body field
			delay 0.2
			key code 121 --page down
			repeat
				if exists button 1 of group 91 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					set addNew to position of button 1 of group 91 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set x to (item 1 of addNew) + 5
					set y to (item 2 of addNew) + 5
					click mouse {x, y} -- Add New Image button
					exit repeat
				else
					delay 0.5
				end if
			end repeat
			
			delay 2
			repeat
				if exists button 1 of group 1 of group 92 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					set chooseFile to position of button 1 of group 1 of group 92 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set x to (item 1 of chooseFile) + 5
					set y to (item 2 of chooseFile) + 5
					click mouse {x, y} --Choose file button
					exit repeat
				else
					delay 0.5
				end if
			end repeat
			delay 2
			key code 2 using command down --Command D for Desktop
			delay 1
			keystroke "make gal"
			key code 36 --Return to list folder contents
			delay 1
			keystroke thisFile
			delay 1
			key code 36 --Return to choose file
			set value of text area 1 of group 94 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theBody -- Image Caption
			delay 1.5
			set value of text field 1 of group 96 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theCredit
			delay 1.5
			set value of text field 1 of group 98 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to iPosition -- photo position
			delay 1
			key code 125 using command down -- down arrow to bottom of page
			delay 0.2
			set gotOK to 0
			set hitItAgain to 0
			repeat
				click button "Update Article" of group UAGroup of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
				repeat 60 times
					if exists button "OK" of window 1 then
						click button "OK" of window 1
						set gotOK to gotOK + 1
						exit repeat
					else
						delay 1
					end if
				end repeat
				if gotOK ≠ 0 then exit repeat
				set hitItAgain to hitItAgain + 1
			end repeat
		end tell
	end tell
end myGal2AndUp

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
	local appName, topMenu, r
	
	-- Validate our input
	if mList's length < 3 then error "Menu list is not long enough"
	
	-- Set these variables for clarity and brevity later on
	set {appName, topMenu} to (items 1 through 2 of mList)
	set r to (items 3 through (mList's length) of mList)
	
	-- This overly-long line calls the menu_recurse function with
	-- two arguments: r, and a reference to the top-level menu
	tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
	local f, r
	
	-- `f` = first item, `r` = rest of items
	set f to item 1 of mList
	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
	
	-- either actually click the menu item, or recurse again
	tell application "System Events"
		if mList's length is 1 then
			click parentObject's menu item f
		else
			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse

on UIscript_check()
	-- get the system version
	set the hexData to system attribute "sysv"
	set hexString to {}
	repeat 4 times
		set hexString to ((hexData mod 16) as string) & hexString
		set hexData to hexData div 16
	end repeat
	set the OS_version to the hexString as string
	if the OS_version is less than "1030" then
		display dialog "This script requires the installation of Mac OS X 10.3 or higher." buttons {"Cancel"} default button 1 with icon 2
	end if
	-- check to see if assistive devices is enabled
	tell application "System Events"
		set UI_enabled to UI elements enabled
	end tell
	if UI_enabled is false then
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "This script utilizes the built-in Graphic User Interface Scripting architecture of Mac OS X which is currently disabled." & return & return & "You can activate GUI Scripting by selecting the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 1 buttons {"Cancel"} default button 1
		end tell
	end if
end UIscript_check

on setupNewstool()
	tell application "Safari"
		activate
		set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
		do JavaScript ("moveTo(0,0);self.resizeTo(" & screen_width as string) & ",screen.availHeight)" in document 1
		do JavaScript ("window.location.href = \"http://www.madison.com/newstool/\"") in document 1
		
	end tell
	tell application "System Events"
		tell process "Safari"
			activate
			
			set gotLI to 0
			repeat
				if gotLI > 0 then exit repeat
				if exists button "Login" of group 11 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					set gotLI to gotLI + 1
				else
					delay 1
				end if
			end repeat
			
			set value of text field 1 of group 8 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to "bkelley"
			set value of text field 1 of group 10 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to "86Gates"
			click button "Login" of group 11 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			
			set gotCA to 0
			repeat
				if gotCA > 0 then exit repeat
				if exists static text "Current Articles" of UI element "Current Articles" of group 8 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					click static text "Current Articles" of UI element "Current Articles" of group 8 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set gotCA to gotCA + 1
				else
					delay 1
				end if
			end repeat
			set gotNA to 0
			repeat
				if gotNA > 0 then exit repeat
				if exists button "New Article" of group 26 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					click button "New Article" of group 26 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set gotNA to gotNA + 1
				else
					delay 1
				end if
			end repeat
			set gotOK to 0
			repeat
				if gotOK > 0 then exit repeat
				if exists button "OK" of window 1 then
					click button "OK" of window 1
					set gotOK to gotOK + 1
				else
					delay 1
				end if
			end repeat
		end tell
	end tell
end setupNewstool

It looks like the only place your script changes photoNum is in the gotFiles handler, where photoNum is parameter variable to the handler. That means it is a local variable. It has the same name as a global variable, but the local binding “shadows” the global one. AppleScript does not (as far as I know) have out parameters (e.g. Pascal, Fortran, C#?) or reference parameters (C++, C#?; not to be confused with plain references or pass-by-reference, which AppleScript does have), so any changes to photoNum in gotFiles will never be seen outside of gotFiles.

As much as I dislike suggesting it (mostly for “functional programming” reasons), you might try using photoNum solely as a global variable. Add global photoNum to your top level and delete it as a parameter in all the handlers that currently list it. In the calls to those handlers, delete the corresponding argument.

Another alternative (a more functional programming style) would be to have galCheck return the new value for photoNum and have setupCyberduck save that value into its local binding of photoNum for use in the next call to galCheck. Since (with respect to photoNum) the only thing that galCheck does is to call gotFiles, this means that it should return the new value for photoNum. Since gotFiles is responsible for incrementing the value, the chain of responsibility can end there.

to setupCyberDuck(|.|, photoNum)
	repeat
		set photoNum to my galCheck(|.|, photoNum)
	end repeat
	my galCheck(|.|, photoNum) -- one more for good measure, no need to save the value here since we are about to exit
end setupCyberDuck
to galCheck(|.|, photoNum)
	my gotFiles(|.|, photoNum)
end galCheck
on gotFiles(|.|, photoNum)
	repeat --for whatever
		set photoNum to photoNum + 1
	end repeat
	photoNum
end gotFiles

I have not looked too closely at the rest of the logic of the script, but I think this scoping issue is the cause of your restarting problem.

Edits: Fix copy/paste error.

Your suggestion was helpful, but I’m still running into problems. Thank you for any input!

How do I return two or more variables from a subroutine? Once the subroutine modifies the values of the two variables, I need to send them back to the script that called it. In the handler called setupCyberduck, variables fileList and photoNum are declared and then modified by calling gotFiles. gotFiles increments photoNum and adds an item to fileList and I need it to send the new values back to the repeat loop so the next set of files downloaded can continue where the prior set left off.


my UIscript_check() --makes sure that UI scripting is enabled
set theServerAddress to "xxxxxxxxxxxx.xxxxxxxxxxxx.com"
set theUserName to "xxxxxxxxxxxx"
set thePassword to "xxxxxxxxxxxx"
set photoNum to 0

tell application "Finder"
	if not (exists folder "make gallery" of desktop) then make new folder at desktop with properties {name:"make gallery"} --checks for and creates the local folder
end tell

delay 3

setupNewstool()
setupCyberduck(theServerAddress, theUserName, thePassword, photoNum)

on setupCyberduck(theServerAddress, theUserName, thePassword, photoNum)
	--code to open connection
	tell application "Finder" to set fileList to name of (items of folder "make gallery" of desktop)
	tell application "Cyberduck"
		activate
		set theBrowser to make new browser
		with timeout of 360 seconds
			tell theBrowser
				set the encoding to "UTF-8"
				set show hidden to false
				connect to theServerAddress as user theUserName with password thePassword
				delay 2
				if not (exists file "make gallery") then create folder "make gallery"
				delay 1
				change folder to "make gallery"
				delay 1
				set photoNum to 0
				tell application "Finder"
					repeat
						if exists (file "end.jpg" of folder "make gallery" of desktop) then exit repeat --initiates end sequence if "end.jpg" is present
						my galCheck() --actives Cyberduck's sychronize function
						my gotFiles(fileList, photoNum)
						copy result to multiReturn
						set fileList to item 1 of multiReturn
						set photoNum to item 2 of multiReturn
						delay 45
					end repeat
				end tell
				my galCheck() --One more time to be sure all files are in.
			end tell
		end timeout
	end tell
	tell application "Finder"
		activate
		display dialog "All gallery files are in!"
	end tell
end setupCyberduck

on galCheck()
	tell application "System Events"
		tell process "Cyberduck"
			activate
			set frontmost to true
			delay 0.3
			keystroke "r" using command down --refresh file list
			delay 2
			my menu_click({"Cyberduck", "File", "Synchronize."})
			keystroke "make gallery"
			delay 0.2
			click button "Choose" of sheet 1 of window "ftp:xxxxxxxxxxxx@xxxxxxxxxxxx.xxxxxxxxxxxx.com"
			delay 0.5
			click button "Continue" of sheet 1 of window "ftp:xxxxxxxxxxxx@xxxxxxxxxxxx.xxxxxxxxxxxx.com"
			repeat while exists button "Stop" of tool bar 1 of window "ftp:xxxxxxxxxxxx@xxxxxxxxxxxx.xxxxxxxxxxxx.com"
			end repeat
		end tell
	end tell
end galCheck

on gotFiles(fileList, photoNum)
	tell application "Finder"
		set listCount to length of fileList
		set newFileList to name of (items of folder "make gallery" of desktop)
		if length of newFileList > 0 then
			if length of newFileList > listCount then
				repeat with thisFile in newFileList
					if thisFile is "end.jpg" then
						if thisFile is in fileList then
						else
							copy thisFile to end of fileList
							set photoNum to photoNum + 1
							my PSInfo(thisFile, photoNum)
						end if
					end if
				end repeat
			end if
		end if
	end tell
	set multiReturn to {fileList, photoNum}
	return mutiReturn
end gotFiles

on PSInfo(thisFile, photoNum)
	tell application "Adobe Photoshop CS3"
		activate
		set filePath to (path to desktop as string) & "make gallery:" & thisFile as alias
		open filePath showing dialogs never
		set theCredit to credit of info of current document as string
		if length of theCredit = 0 then set theCredit to "No info"
		set theHeadline to "Photo gallery: " & instructions of info of current document as string
		if length of theHeadline = 0 then set theHeadline to "No info"
		set theBody to transmission reference of info of current document as string
		if length of theBody = 0 then set theBody to "No info"
		set iPosition to keywords of info of current document as string
		do action "72dpi" from "Photo gallery set"
		save document 1
		close document 1
	end tell
	if photoNum < 2 then
		galAdd(thisFile, filePath, theCredit, theHeadline, theBody, photoNum, iPosition)
	else
		myGal2AndUp(photoNum, thisFile, theBody, theCredit, iPosition)
	end if
end PSInfo

on galAdd(thisFile, filePath, theCredit, theHeadline, theBody, photoNum, iPosition)
	tell application "Finder"
		activate
		display dialog "This is the galAdd routine
for the first photo only." giving up after 4
	end tell
	tell application "System Events"
		tell process "Safari"
			activate
			set frontmost to true
			delay 1
			set value of text area 1 of group 31 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theHeadline --Headline
			delay 0.5
			keystroke theBody
			delay 1.5
			set updateAbstract to position of button 1 of group 84 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" -- Update Abstract button
			set x to (item 1 of updateAbstract) + 5
			set y to (item 2 of updateAbstract) + 5
			click mouse {x, y} -- Update Abstract
			delay 1
			key code 121 --page down
			delay 0.3
			set addNew to position of button 1 of group 93 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			set x to (item 1 of addNew) + 5
			set y to (item 2 of addNew) + 5
			click mouse {x, y} -- Add New Image button
			delay 2
			set chooseFile to position of button 1 of group 1 of group 94 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			set x to (item 1 of chooseFile) + 5
			set y to (item 2 of chooseFile) + 5
			click mouse {x, y} --Choose file
			delay 0.5
			key code 2 using command down --Command D for Desktop
			delay 1
			keystroke "make gal"
			key code 36 --Return to list folder contents
			delay 1
			keystroke thisFile
			delay 1
			key code 36 --Return to choose file
			set value of text area 1 of group 96 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theBody -- Image Caption
			delay 1
			set value of text field 1 of group 98 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theCredit
			delay 1
			set value of text field 1 of group 100 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to iPosition -- photo position
			key code 121 --page down
			delay 0.5
			click button "Add New Section" of group 105 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			delay 0.5
			key code 121 --page down
			delay 0.5
			click pop up button 1 of group 106 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" -- select a section
			delay 0.5
			key code 121 --page down
			delay 0.5
			key code 36 -- Return key to choose section
			delay 0.5
			click button "Add New Section" of group 112 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			delay 0.5
			key code 121 --page down
			delay 0.5
			click pop up button 1 of group 113 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			delay 0.5
			key code 125 -- down arrow
			delay 0.5
			key code 36 -- Return key to choose section
			delay 0.5
			
			set gotOK to 0
			set hitItAgain to 0
			repeat
				click button "Update Article" of group 120 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
				repeat 60 times
					if exists button "OK" of window 1 then
						click button "OK" of window 1
						set gotOK to gotOK + 1
						exit repeat
					else
						delay 1
					end if
				end repeat
				if gotOK ≠ 0 then exit repeat
				set hitItAgain to hitItAgain + 1
			end repeat
			
		end tell
	end tell
end galAdd

on myGal2AndUp(photoNum, thisFile, theBody, theCredit, iPosition)
	tell application "Finder"
		activate
		display dialog "This is the myGal2AndUp routine
for photos 2 and up." giving up after 4
	end tell
	set UAGroup to (128 + (10 * photoNum) - 20)
	-- Code below is the same for every file from 2 on
	tell application "System Events"
		tell process "Safari"
			activate
			set frontmost to true
			delay 0.5
			click mouse {63, 508} -- clicks away from Article Body field
			delay 0.2
			key code 121 --page down
			repeat
				if exists button 1 of group 91 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					set addNew to position of button 1 of group 91 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set x to (item 1 of addNew) + 5
					set y to (item 2 of addNew) + 5
					click mouse {x, y} -- Add New Image button
					exit repeat
				else
					delay 0.5
				end if
			end repeat
			
			delay 2
			repeat
				if exists button 1 of group 1 of group 92 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					set chooseFile to position of button 1 of group 1 of group 92 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set x to (item 1 of chooseFile) + 5
					set y to (item 2 of chooseFile) + 5
					click mouse {x, y} --Choose file button
					exit repeat
				else
					delay 0.5
				end if
			end repeat
			delay 2
			key code 2 using command down --Command D for Desktop
			delay 1
			keystroke "make gal"
			key code 36 --Return to list folder contents
			delay 1
			keystroke thisFile
			delay 1
			key code 36 --Return to choose file
			set value of text area 1 of group 94 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theBody -- Image Caption
			delay 1.5
			set value of text field 1 of group 96 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to theCredit
			delay 1.5
			set value of text field 1 of group 98 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to iPosition -- photo position
			delay 1
			key code 125 using command down -- down arrow to bottom of page
			delay 0.2
			
			set gotOK to 0
			set hitItAgain to 0
			repeat
				click button "Update Article" of group UAGroup of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
				repeat 60 times
					if exists button "OK" of window 1 then
						click button "OK" of window 1
						set gotOK to gotOK + 1
						exit repeat
					else
						delay 1
					end if
				end repeat
				if gotOK ≠ 0 then exit repeat
				set hitItAgain to hitItAgain + 1
			end repeat
			
		end tell
	end tell
end myGal2AndUp

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
	local appName, topMenu, r
	
	-- Validate our input
	if mList's length < 3 then error "Menu list is not long enough"
	
	-- Set these variables for clarity and brevity later on
	set {appName, topMenu} to (items 1 through 2 of mList)
	set r to (items 3 through (mList's length) of mList)
	
	-- This overly-long line calls the menu_recurse function with
	-- two arguments: r, and a reference to the top-level menu
	tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
	local f, r
	
	-- `f` = first item, `r` = rest of items
	set f to item 1 of mList
	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
	
	-- either actually click the menu item, or recurse again
	tell application "System Events"
		if mList's length is 1 then
			click parentObject's menu item f
		else
			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse

on UIscript_check()
	-- get the system version
	set the hexData to system attribute "sysv"
	set hexString to {}
	repeat 4 times
		set hexString to ((hexData mod 16) as string) & hexString
		set hexData to hexData div 16
	end repeat
	set the OS_version to the hexString as string
	if the OS_version is less than "1030" then
		display dialog "This script requires the installation of Mac OS X 10.3 or higher." buttons {"Cancel"} default button 1 with icon 2
	end if
	-- check to see if assistive devices is enabled
	tell application "System Events"
		set UI_enabled to UI elements enabled
	end tell
	if UI_enabled is false then
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "This script utilizes the built-in Graphic User Interface Scripting architecture of Mac OS X which is currently disabled." & return & return & "You can activate GUI Scripting by selecting the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 1 buttons {"Cancel"} default button 1
		end tell
	end if
end UIscript_check

on setupNewstool()
	tell application "Safari"
		activate
		set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
		do JavaScript ("moveTo(0,0);self.resizeTo(" & screen_width as string) & ",screen.availHeight)" in document 1
		do JavaScript ("window.location.href = \"http://www.xxxxxxxxxxxx.com/newstool/\"") in document 1
		
	end tell
	tell application "System Events"
		tell process "Safari"
			activate
			
			set gotLI to 0
			repeat
				if gotLI > 0 then exit repeat
				if exists button "Login" of group 11 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					set gotLI to gotLI + 1
				else
					delay 1
				end if
			end repeat
			
			set value of text field 1 of group 8 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to "bkelley"
			set value of text field 1 of group 10 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" to "86Gates"
			click button "Login" of group 11 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
			
			set gotCA to 0
			repeat
				if gotCA > 0 then exit repeat
				if exists static text "Current Articles" of UI element "Current Articles" of group 8 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					click static text "Current Articles" of UI element "Current Articles" of group 8 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set gotCA to gotCA + 1
				else
					delay 1
				end if
			end repeat
			set gotNA to 0
			repeat
				if gotNA > 0 then exit repeat
				if exists button "New Article" of group 26 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3" then
					click button "New Article" of group 26 of UI element 1 of scroll area 1 of group 2 of window "NewsTool V3"
					set gotNA to gotNA + 1
				else
					delay 1
				end if
			end repeat
			set gotOK to 0
			repeat
				if gotOK > 0 then exit repeat
				if exists button "OK" of window 1 then
					click button "OK" of window 1
					set gotOK to gotOK + 1
				else
					delay 1
				end if
			end repeat
		end tell
	end tell
end setupNewstool

That’s a heck of a script. It’s often better if you can get a reduction of the script so that people can focus on the issue and not get lost in the rest of the code (which we can’t run anyway since it’s specific to your environment).

Anyway, a quick look hints that the problem may just be because you’re using local variables everywhere. Since you increment the variable within a subroutine, it’s only changed inside that subroutine. Nowhere do you pass the value back out.

Here’s a reduction that shows the issue more clearly:

set blah to 1

repeat 10 times
	doSomething(blah)
end repeat
display dialog blah

on doSomething(blah)
	set blah to blah + 1
end doSomething

At the end of the script, blah is 1 - the same value it was at the beginning. That’s because inside doSomething() you’re using a whole different variable that just happens to have the same name.

The simplest solution is to declare your variable as global. This ensures that the same variable is used throughout the script (and saves you from having to pass it between handlers). Here’s the above script using global variables instead:

global blah
set blah to 1

repeat 10 times
	doSomething()
end repeat
display dialog blah

on doSomething()
	global blah
	set blah to blah + 1
end doSomething

I’m sorry about the long script. I was in a hurry, but I’ll post shorter script clips next time. Thanks for your input. I’ll try it out.