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