Hello everyone ,
I encounter a problem that using the AppleScriptTask() to run Apple Script but can not close access of a file,
My VBA code is below:
Sub createFile()
Dim RunMyScript
RunMyScript = AppleScriptTask("wfile.scpt", "hl", "parm")
End Sub
and my Apple Script is below:
on hl(parm1)
Set theFile To (path To desktop As text) & “congtou4.csv”
Set referenceNumber To Open For access theFile With Write permission
Write “aa,bb,cc,dd,ee” To referenceNumber
display dialog “rnum is :” & referenceNumber
try
Close access referenceNumber
on error errText number errNum
display dialog errText & errNum
End try
display dialog “close ok”
End hl
I tested many times , the file was created and there was data in the file
but the close access statement , I got an error : Parameter error -50
(If I run the AppleScript on Mac not called by VBA , close file was OK)
I can only invoke AppleScriptTask once to write, the second time I got the error that the file is open, can anyone help to close the access ? Thanks !
I doubt that it will solve the problem but I made a small change :
on hl(parm1)
set theFile to (path to desktop as text) & "congtou4.csv"
set referenceNumber to open for access file theFile with write permission # EDITED
write "aa,bb,cc,dd,ee" to referenceNumber
display dialog "rnum is :" & referenceNumber
try
close access referenceNumber
on error errText number errNum
display dialog errText & errNum
end try
display dialog "close ok"
end hl
I made it because Standard Additions’s dictionary claims :
[format]open for access file : the file or alias to open for access. If the file does not exist, a new file is created.[/format]
Your original code doesn’t apply open for access to a file but to a string.
Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 1 aout 2016 14:23:16
For both
set referenceNumber to open for access file theFile with write permission
and
set referenceNumber to open for access theFile with write permission
the record was written into the file successfully, but the close error was always the same , I can not write another record because the file was open.
I’m afraid that you are facing a problem due to VBA.
As a workaround you may try to achieve the same goal without triggering open for access.
on hl(parm1)
set p2d to path to desktop as text
set fileName to "congtou4.csv"
set theFile to p2d & fileName
tell application "System Events"
if (exists disk item theFile) then delete disk item theFile
make new file at end of folder p2d with properties {name:fileName}
end tell
write "aa,bb,cc,dd,ee" to file theFile
display dialog "close ok"
end hl
Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) lundi 1 aout 2016 16:19:02
I don’t know anything about VBA. But as an immediate solution to closing the file(s) which are still open, quit the application that was running the script. The system will release all the open file accesses registered to that application.
Then, in the script itself, rearrange the ‘try’ statement so that it catches everything that can go wrong while the file access is open, so that the script keeps going long enough to close the access:
on hl(parm1)
set theFile to (path to desktop as text) & "congtou4.csv"
set referenceNumber to (open for access file theFile with write permission)
try
write "aa,bb,cc,dd,ee" to referenceNumber
close access referenceNumber
on error errText number errNum
close access referenceNumber
display dialog errText & errNum
end try
end hl
Thanks Nigel
I may not do that , because I used the VBA to loop to call the script to write record into file. if the file can not be closed , other records can not be written to file.