Upload to server via do shell script sporadically failing

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)

I would add extraneous instructions writing datas in a text file.
When the script fails, you would be able to read the text file to know where it failed and what where the datas available before the crash.

I don’t own inDesign so I’m unable to compile your script.

In the script below you will retrieve your own code plus the instructions to apply my scheme.

I inserted 8 example of instructions writing datas in the report.

Add the ones writing potentially relevant infos.

script o
property pathToReport : ""
end script

-- define the path to the log file
set o's pathToReport to (path to desktop as text) & "report_troper.txt"
-- Create the report file on the desktop
my writeto(o's pathToReport, "Report on " & date string of (current date) & linefeed & linefeed, «class utf8», false)


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 -- HERE
end tell

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 1, " & activeApp & linefeed, «class utf8», true)

-- check if this app is running before continuing
try
do shell script "ps acx | grep java" -- HERE
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

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 2" & linefeed, «class utf8», true)

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

--  Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 3" & linefeed, «class utf8», true)

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

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 4, " & userID & linefeed, «class utf8», true)

set userFile to ((path to current user folder as string) & ".currentuser")

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 5, " & userFile & linefeed, «class utf8», true)

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

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 6" & linefeed, «class utf8», true)

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)

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 7, " & adsPath & linefeed & checkedOutAds & linefeed, «class utf8», true)


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

-- Edit the message to insert with relevant datas available
my writeto(o's pathToReport, "Point 8" & linefeed, «class utf8», true)

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)


--===== Below are your original handler and the one used to write in the report file.

on frontApp(activeApp)
tell application "System Events" to tell process activeApp to set frontmost to true
end frontApp

--
-- Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
--
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as text -- EDITED
set openFile to open for access file targetFile with write permission -- EDITED
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile -- EDITED
end try
return false
end try
end writeto

Good luck.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 21 novembre 2016 17:28:21

Hi Yvan,

Thank you for your reply. Unfortunately I cannot get your code to compile. Perhaps it is using newer AppleScript syntax? I’m unavoidably stuck in the stone age with Mac OS X 10.6.8 Snow Leopard for this script.

Oops, I missed the fact that you are using 10.6.8.

You may start trying to compile :

script o
	property pathToReport : ""
end script

# define the path to the log file
set o's pathToReport to (path to desktop as text) & "report_troper.txt"
# Create the report file on the desktop
my writeto(o's pathToReport, "Report on " & date string of (current date) & linefeed & linefeed, «class utf8», false)

(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

I guess that it will fail - highlighting the words “«class furl»”
If it fails, try with:

script o
	property pathToReport : ""
end script

# define the path to the log file
set o's pathToReport to (path to desktop as text) & "report_troper.txt"
# Create the report file on the desktop
my writeto(o's pathToReport, "Report on " & date string of (current date) & linefeed & linefeed, «class utf8», false)

(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text # EDITED
		set openFile to open for access file targetFile with write permission -- EDITED
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile -- EDITED
		end try
		return false
	end try
end writeto

If it fail again edit the instructions beginning by the character “#” . I use it for years but don’t remember if it was accepted as a comment beginning by 10.6.8

Replace them by “–”

I deliberately use both syntax for the comments flagging the edited instructions to be sure that you will understand what I mean.

Maybe an other group of lines may be rejected.
Replace

(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)

by

--
-- Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
-

With all these changes, the short test script becomes :

script o
	property pathToReport : ""
end script

-- define the path to the log file
set o's pathToReport to (path to desktop as text) & "report_troper.txt"
-- Create the report file on the desktop
my writeto(o's pathToReport, "Report on " & date string of (current date) & linefeed & linefeed, «class utf8», false)

--
-- Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
--
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text -- EDITED
		set openFile to open for access file targetFile with write permission -- EDITED
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile -- EDITED
		end try
		return false
	end try
end writeto

I post this message and edit my preceding message accordingly.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 21 novembre 2016 19:38:15

Hi Yvan,

I found that some of the line endings were messed up when I copied and pasted the code from your post. Fixing those and replacing the comment characters allowed it to compile.

I’ll see what happens.

Thanks again.

How may we make users understand that they don’t have to copy paste posted scripts.
Just click the button named explicitly [Open this Scriplet in your Editor:]

Often, when you copy-paste you get some spurious characters like no-break space which aren’t accepted by the Script Editor.

macScripter deliver a fine, efficient tool : the button [Open this Scriplet in your Editor:]. Just use it !

Yvan KOENIG (VALLAURIS, France) lundi 21 novembre 2016 20:58:30

Oops, I will do that from now on. Thanks for the heads up.

I opened the long script and found some odd characters in it.

I pated it in Pages which make visible the different kinds of space characters.
I removed every space, no-break space, tab which were available at the beginning bof several instructions.

I pasted this cleaned version in my first post containing this long script.

It seems that the message formatter added no extraneous characters so I guess that you will be able to open it flawlessly.

This being written, the extraneous characters were perfectly displayed by Script Editor when I tried to compile (after disabling the pieces of code dedicated to inDesign).
When the compiler encountered such odd character, it politely highlighted it so it was easy to remove it

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 21 novembre 2016 21:29:58