executescripttext with file name
hegedus
Hi,
I am working on some cross platform issues integrating with Python.
I am using (or attempting to use) ExecuteScriptText to run a shell script on the Mac and a bat file on Windows
On the Mac I create a string:
Do shell script "'fullpath/to/file.sh'" with single quotes to accommodate
On Windows I use parsefilepath(5,thelocation,"\\",0,0) to define the file path but am running into trouble.
The file path is "C:Users\andy.hegedus\Desktop\Texture Classifier\Run_Python.bat" as the string.
Do I have to worry that the slashes are being interpreted as escape codes such as the \R into the operation, ExecuteTextScript or will it be interpreted as a legal file path?
Andy
Here is a snippet of code that I used LONG ago for creating and running batch files. Not sure if this is helpful, but it did work for me. There are likely built-in functions that can replace the ugly path conversion functions.
string S_filelist
// files must be on disk at LoggerPath
// uses external scp program to transfer files
// Runs in the background. uses ssh user key.
wave /T configTxt=root:Packages:RS232Logger:configTxt
string s_filename
variable refnum, numfiles=0
do
s_filename = StringFromList(numfiles, S_filelist)
if (strlen(s_filename)==0)
break
endif
refnum=0
open /Z /R/P=LoggerPath refnum as s_filename // s_filename is changed here
if (V_flag==0) // file exists
close refnum
numfiles+=1
else
S_filelist=RemoveListItem(numfiles,S_filelist)
endif
// need to check that this always works as planned...
while(1)
if (numfiles <1)
return 0
endif
PathInfo LoggerPath
string batPath=DL_MacPath2WinPath(S_path)
string cmd
string UnixPathStr=DL_MacPath2ForwardSlashPath(S_Path)
// create a new batch file
string s_batname
variable i=1, maxbats=10
refnum=0
do
sprintf s_batname, "uploader%d.bat", i
open /Z /P=LoggerPath refnum as s_batname
if (refnum) // file is open
break
endif
i+=1
while (i<=maxbats)
if (i>maxbats)
printf "%s %s ERROR: File upload failed - too many unwriteable batch files\r", date(), time()
return 0
endif
// write to the batch file
// pscp usage:
// pscp [options] source [source...] [user@]host:target
// pscp [options]
fprintf refnum, "\"%s\" %s", DL_MacPath2WinPath(ConfigTxt[%scpPathStr]), ConfigTxt[%scpFlags]
// source [source...]
for (i=0;i<numfiles;i+=1)
fprintf refnum, " \"%s%s\"", UnixPathStr, StringFromList(i, S_filelist)
endfor
// [user@]host:target
fprintf refnum, " %s\r\n", ConfigTxt[%ServerAddress]
fprintf refnum, "attrib -R %0\r\n" // make THIS batch file writeable
fprintf refnum, "exit\r\n"
close refnum
SetFileFolderInfo /Z/RO batPath+s_batname // make bat file read only so that we don't overwrite it before it has executed
// the batch file is invoked using start so that we don't have to wait for completion
sprintf cmd, "cmd.exe /C start \"\" /min \"%s%s\" ", batPath, s_batname
ExecuteScriptText /W=0.5 /B/Z cmd
if (V_flag)
printf "%s %s ERROR: File upload failed at ExecuteScriptText\r", date(), time()
endif
return 1
end
// double backslashes are needed when building commands as strings
function /T DL_MacPath2WinPath(str)
string str
variable i=3
str[1,1]=":\\"
do
if (stringmatch (str[i], ":"))
str[i,i]="\\"
endif
i+=1
while(i<strlen(str))
return str
end
function /T DL_MacPath2ForwardSlashPath(str)
string str
variable i=3
str[1,1]=":/"
do
if (stringmatch (str[i], ":"))
str[i,i]="/"
endif
i+=1
while(i<strlen(str))
return str
end
September 22, 2023 at 12:14 am - Permalink
Andy,
I haven't used "ExecuteScriptText" for quite some time, so relying on the help file it looks like you need to escape the backslashes in the file path:
I thought there was a function to convert a path to the escaped version, but could not find such a thing. You could try "ReplaceString" to replace a single backslash with a double backslash.
ReplaceString("\", thelocation, "\\")
September 22, 2023 at 06:08 am - Permalink