Hello,
I’ve been pulling my hair out with this for a while now. I hope someone can shed some light on what is happening.
This AppleScript is collecting data to be send to a PHP script for insertion into a MySQL database. I have several similar scripts in use that work every time, but this one is sporadically having issues. The only significant difference I can think of between this and the others is that I am using InDesign to make a dialog with checkboxes.
We are using Mac OS X 10.6.8 Snow Leopard and InDesign CS4.
Here is the problem that happens sporadically:
When sending POST data to the server it is always sending the first parameter (“ad”), but the other parameters (“userid” and six boolean values) are missing. At first I was using cURL, then tried a Python script to see if that made any difference. Same results either way.
I’ve attempted several methods of saving the problematic command to a text file for diagnosis, but when the above issue occurs, the cURL command to upload the file via FTP fails entirely.
Frustratingly I have been unable to reproduce the issue myself… It is happening when other users run it.
Here is the code (passwords and server addresses redacted). Any help would be greatly appreciated.
–John
on frontApp(activeApp)
tell application "System Events" to tell process activeApp to set frontmost to true
end frontApp
delay 2
-- save frontmost app name to reactivate at end of script
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell
-- check if this app is running before continuing
try
do shell script "ps acx | grep java"
on error
tell application "SystemUIServer"
activate
display dialog "You must be logged into the Client to use this QuicKey." buttons {"OK"} default button "OK" with title "Preflight Report" with icon 0
end tell
my frontApp(activeApp)
return
end try
set text item delimiters of AppleScript to ""
--get userID from a window name in this Java app -- this code works flawlessly in other scripts
tell application "System Events"
tell application process "java"
set frontmost to true
set mainWindowName to ""
set windowNumber to 0
delay 0.2
repeat while mainWindowName = ""
set windowNumber to windowNumber + 1
set windowName to title of window windowNumber
if windowName begins with "Digital Asset Client" then set mainWindowName to windowName
end repeat
end tell
end tell
set userID to ""
set testString to characters 38 thru 44 of mainWindowName as string
set theCount to 1
set testChar to character theCount of testString
repeat while testChar ≠" "
set userID to userID & testChar
set theCount to theCount + 1
set testChar to character theCount of testString
end repeat
set userID to userID as string
set userFile to ((path to current user folder as string) & ".currentuser")
try
set userData to open for access file userFile with write permission
set eof userData to 0
write userID to userData starting at eof
close access userData
on error
try
close access file userFile
end try
end try
set checkedOutAds to ""
set jobLog to ""
set adsPath to POSIX path of (path to current user folder as string) & userID & "/Ad/"
set checkedOutAds to (do shell script "ls " & adsPath without altering line endings)
if (count of paragraphs of checkedOutAds) < 1 then
tell application "SystemUIServer"
activate
try
set userResponse to text returned of (display dialog "You do not have any ads checked out. Please check out the ad or enter the ad number here." default answer "" with title "Preflight Report")
on error number -128
my frontApp(activeApp)
return
end try
end tell
if userResponse is "" then
my frontApp(activeApp)
return
else
set jobLog to userResponse
end if
end if
set adCount to count of paragraphs of checkedOutAds
if adCount > 1 then
set adsList to paragraphs of checkedOutAds
set cleanAdsList to {}
repeat with i from 1 to count of adsList
if (count of characters of item i of adsList) = 11 and character -5 of item i of adsList = "." and item i of adsList starts with "3477" then
set cleanAdsList to cleanAdsList & item i of adsList
end if
end repeat
if (count of cleanAdsList) > 1 then
set userResponse to ""
tell application "SystemUIServer"
activate
try
set userResponse to (choose from list cleanAdsList with prompt "Please select ad:" with title "Preflight Report")
on error
set userResponse to {""}
end try
end tell
if userResponse is false then
my frontApp(activeApp)
return
end if
set jobLog to item 1 of userResponse as string
else if (count of cleanAdsList) = 1 then
set jobLog to item 1 of cleanAdsList
end if
end if
if adCount = 1 then set jobLog to paragraph 1 of checkedOutAds
repeat while (count of characters of jobLog) ≠11 or character -5 of jobLog ≠"."
tell application "SystemUIServer"
activate
try
set userResponse to text returned of (display dialog "The ad number must be in the form \"XXXXXX.XXXX\". Please check out an appropriate ad or enter the ad number here." default answer "" with title "Preflight Report")
on error number -128
my frontApp(activeApp)
return
end try
end tell
if userResponse is "" then
my frontApp(activeApp)
return
else
set jobLog to userResponse
end if
end repeat
repeat while jobLog does not start with "3477" or ((count of characters of jobLog) ≠11 or character -5 of jobLog ≠".")
tell application "SystemUIServer"
activate
try
set userResponse to text returned of (display dialog "The Preflight Report is only used on Staten Island ads. Please check out an appropriate ad or enter the ad number here." default answer "" with title "Preflight Report")
on error number -128
my frontApp(activeApp)
return
end try
end tell
if userResponse is "" then
my frontApp(activeApp)
return
else
set jobLog to userResponse
end if
end repeat
-- here we use InDesign to make a dialog with checkboxes
tell application "Adobe InDesign CS4"
activate
set myDialog to make dialog
tell myDialog
set name to "Preflight Report"
set myDialogColumn to make dialog column
tell myDialogColumn
set myBorderPanel to make border panel
tell myBorderPanel
set myDialogColumn to make dialog column
tell myDialogColumn
make static text with properties {static label:"Check the box next to any issues that apply to ad " & jobLog}
set myDialogRow to make dialog row
tell myDialogRow
set myDialogColumn to make dialog column
tell myDialogColumn
set myColorsCheckbox to make checkbox control with properties {static label:"Pantone and/or SPOT colors used"}
end tell
end tell
set myDialogRow to make dialog row
tell myDialogRow
set myDialogColumn to make dialog column
tell myDialogColumn
set myLoResCheckbox to make checkbox control with properties {static label:"Low resolution images (under 300 DPI)"}
end tell
end tell
set myDialogRow to make dialog row
tell myDialogRow
set myDialogColumn to make dialog column
tell myDialogColumn
set myRasterizedCheckbox to make checkbox control with properties {static label:"Rasterized small text (under 600 DPI)"}
end tell
end tell
set myDialogRow to make dialog row
tell myDialogRow
set myDialogColumn to make dialog column
tell myDialogColumn
set myQualityCheckbox to make checkbox control with properties {static label:"Poor quality images and/or artwork"}
end tell
end tell
set myDialogRow to make dialog row
tell myDialogRow
set myDialogColumn to make dialog column
tell myDialogColumn
set mySizeCheckbox to make checkbox control with properties {static label:"Incorrectly sized PDF"}
end tell
end tell
set myDialogRow to make dialog row
tell myDialogRow
set myDialogColumn to make dialog column
tell myDialogColumn
set myCallCheckbox to make checkbox control with properties {static label:"Franchise Call written"}
end tell
end tell
end tell
end tell
end tell
end tell
set myReturn to show myDialog
if myReturn = false then
destroy myDialog
my frontApp(activeApp)
return
end if
set iColors to checked state of myColorsCheckbox as string
set iLoRes to checked state of myLoResCheckbox as string
set iRasterized to checked state of myRasterizedCheckbox as string
set iQuality to checked state of myQualityCheckbox as string
set iSize to checked state of mySizeCheckbox as string
set iCall to checked state of myCallCheckbox as string
end tell
set servResp to ""
-- cURL command we were using originally
--set curlCommand to "curl -d ad=" & jobLog & " -d operator=" & userID & " -d colors=" & iColors & " -d lores=" & iLoRes & " -d rasterized=" & iRasterized & " -d quality=" & iQuality & " -d size=" & iSize & " -d call=" & iCall & " http://[server_address]/adissuesproc.php" as string
-- try using Python instead -- same sporadic issue as cURL
set pythonPost to "import urllib2, urllib
mydata=[('ad','" & jobLog & "'),('operator','" & userID & "'),('colors','" & iColors & "'), ('lores','" & iLoRes & "'), ('rasterized','" & iRasterized & "'), ('quality','" & iQuality & "'), ('size','" & iSize & "'), ('call','" & iCall & "')] #The first is the var name the second is the value
mydata=urllib.urlencode(mydata)
path='http://[server_address]/adissuesproc.php' #the url you want to POST to
req=urllib2.Request(path, mydata)
req.add_header(\"Content-type\", \"application/x-www-form-urlencoded\")
page=urllib2.urlopen(req).read()
print page"
--attempt to save a file to the server containing the POST command for diagnosis -- this fails when the issue is present
set timeStamp to (do shell script "date \"+%y%m%d%H%M%S\"")
set logFileName to "PyLog" & timeStamp & ".txt" as string
set curlTextFile to ((path to current user folder as string) & "PyLog" & timeStamp & ".txt")
try
set curlTextData to open for access file curlTextFile with write permission
set eof curlTextData to 0
write pythonPost to curlTextData starting at eof
close access curlTextData
on error
try
close access file curlTextFile
end try
end try
do shell script "ditto -c -k --sequesterRsrc " & (POSIX path of curlTextFile) & " " & (POSIX path of curlTextFile & ".zip")
do shell script "curl -u [user]:[password] -T '" & (POSIX path of curlTextFile & ".zip") & "' ftp://[server_address]/"
do shell script "rm " & (POSIX path of curlTextFile & ".zip") & " " & (POSIX path of curlTextFile)
try
set servResp to (do shell script "python -c" & quoted form of pythonPost)
on error err
tell application "Adobe InDesign CS4"
activate
display dialog "Error:" & return & return & err & return & return & "Please send a screenshot of this error." buttons {"OK"} default button "OK" with icon 0
end tell
return
end try
tell application "Adobe InDesign CS4"
activate
destroy myDialog
display dialog "Issues successfully recorded for " & jobLog & "." buttons {"OK"} default button "OK" with title "Preflight Report"
end tell
my frontApp(activeApp)