XojoScript copy project file to a new directory

I’m trying to write a XojoScript for MacOS to

  1. Make a new directory alongside the project file
  2. Copy the project file into it

The first steps are to get the path to the project folder and set the working directory to the project folder, but I can’t get that to work.

Var ProjFilePath As String = ProjectShellPath // Path to the Xojo project file

Dim Dirs() As String = ProjFilePath.split("/")  
Dirs.RemoveAt(Dirs.LastRowIndex) // Remove the project file from the path
Dim ProjectFolderPath As String = String.FromArray(Dirs,"/") + "/"

Var command As String
Var result As String

Print "Project folder path is "+projectfolderpath // This shows the correct path to the project folder

command = "cd "+ ProjectFolderPath
result = DoShellCommand(command)
If result <> "" Then Print "cd "+result // cd reports no error

result = DoShellCommand("pwd -P")
Print "Current working Directory is: "+Result // This shows "/" instead of the expected result of cd

When I manually do the same steps using Terminal, cd changes the working directory to the project folder and pwd shows that it worked; then I can do mkdir. Using the script, cd seems to have no effect - the working dir remains “/”.

The calls to DoShellCommand are independent, so your second call knows nothing of the prior cd. Try it this way instead:

result = DoShellCommand( "cd " + ProjectFolderPath + " && pwd -P" )

Also, you might want to quote the ProjectFolderPath, just in case.

var quotedProjectFolderPath as string = _
    "'" + projectFolderPath.ReplaceAll( "'", "'\''" ) + "'"
result = DoShellCommand( "cd " + quotedProjectFolderPath + " && pwd -P" )

(Not tested.)

2 Likes