Page 1 of 1

Unable to preview/open a file including spaces in filename

Posted: Wed Jul 03, 2024 10:07 am
by flashbob
.. I want to open a file (e.g. pdf) in current directory. Works fine on windows, but not on MAC because of the space in filename. Any idea how to fix it ?

Code: Select all


Enumeration FormGadget
  #Btn
EndEnumeration
Define event


OpenWindow(0, 50, 50, 200, 70, "Open file", #PB_Window_SystemMenu)
ButtonGadget(#Btn, 25, 25, 120, 25, "open pdf")

Repeat
  event = WaitWindowEvent()  
  Select event
    Case #PB_Event_Gadget
      Select EventGadget() 
        Case #Btn
          ; RunProgram("open","Test1.pdf","")   ; ok
          RunProgram("open","Test 1.pdf","")  ; file with space not working
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow


Re: Unable to preview/open a file including spaces in filename

Posted: Wed Jul 03, 2024 10:17 am
by Lord
Try enclosing parameter in quotes:

Code: Select all

RunProgram("open", #DQUOTE$+"Test 1.pdf"+#DQUOTE$, "")

Re: Unable to preview/open a file including spaces in filename

Posted: Wed Jul 03, 2024 10:56 am
by flashbob
... perfect, that's how it works. Thx a lot!

Re: Unable to preview/open a file including spaces in filename

Posted: Thu Jul 04, 2024 11:23 am
by Piero
flashbob wrote: Wed Jul 03, 2024 10:56 am that's how it works.

Code: Select all

Procedure simpleShell(ShellCommand$)
   Protected shell
   shell = RunProgram("/bin/zsh","","", #PB_Program_Open|#PB_Program_Write)
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      While ProgramRunning(shell) : Delay(10) : Wend ; Wait until program quits (optional)
      CloseProgram(shell)
   EndIf
EndProcedure

Procedure.s QuoteString(str$) ; useful to quote FILE/FOLDER PATHS
   ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
EndProcedure

simpleShell("open " + QuoteString(~"Test \" ' 1.pdf"))

Re: Unable to preview/open a file including spaces in filename

Posted: Thu Aug 22, 2024 8:54 pm
by Bmld756
Hello,

That right for RunProgram, but for no problem with the space with SetCurrentDirectory or OpenFile.

Re: Unable to preview/open a file including spaces in filename

Posted: Fri Aug 23, 2024 5:59 pm
by Wolfram

Code: Select all

Enumeration FormGadget
  #Btn
EndEnumeration
Define event


OpenWindow(0, 50, 50, 200, 70, "Open file", #PB_Window_SystemMenu)
ButtonGadget(#Btn, 25, 25, 120, 25, "open pdf")

Repeat
  event = WaitWindowEvent()  
  Select event
    Case #PB_Event_Gadget
      Select EventGadget() 
        Case #Btn

          filePath$ = "/Users/yourName/Desktop/S P A C E/Test 1.pdf"
          If SetCurrentDirectory( GetPathPart(#DQUOTE$ + filePath$ +#DQUOTE$) )
            Debug GetCurrentDirectory() ; /Users/yourName/Desktop/S P A C E/
            
          Else
            Debug "Error open path"
          EndIf
          RunProgram("open", #DQUOTE$ + filePath$ +#DQUOTE$,"")  ; file with space working here
      EndSelect
  EndSelect
Until event = #PB_Event_CloseWindow


Re: Unable to preview/open a file including spaces in filename

Posted: Fri Aug 23, 2024 6:09 pm
by AZJIO
Wolfram wrote: Fri Aug 23, 2024 5:59 pm

Code: Select all

GetPathPart(#DQUOTE$ + filePath$ +#DQUOTE$)
This is not correct, the #DQUOTE$ constant is superfluous here. The path must not contain quotes; they are not allowed in names.
Although it did not give an error and returned the correct result.
The RunProgram function is different, there is a parameter enumeration string, not a path, so there quotes are interpreted correctly as highlighting an object so that it is not separated by a space.

SetCurrentDirectory() sets the current path, so you don't have to use the full path to the file. If you use the full path to the file, then SetCurrentDirectory() does not give you any benefits.

Linux is case sensitive in folder and file names, so it is important not to make mistakes in these little things.

For Linux you need to use "xdg-open". I suggested adding an example to the documentation. You also need to install this "xdg-utils" package

Code: Select all

RunProgram("xdg-open", #DQUOTE$ + filePath$ +#DQUOTE$, GetPathPart(tmp$))

Re: Unable to preview/open a file including spaces in filename

Posted: Sat Aug 24, 2024 12:30 am
by Piero
AZJIO wrote: Fri Aug 23, 2024 6:09 pmThe path must not contain quotes; they are not allowed in names.
Huh?
You know I'm your Fan AZJIO, but we are talking about Mac OS, and it also seems you didn't really read my post above (~"Test \" ' 1.pdf")

See? GET A MAC! I cannot conceive you without a Mac!!!

Re: Unable to preview/open a file including spaces in filename

Posted: Sat Aug 24, 2024 1:47 am
by Piero
Wolfram wrote: Fri Aug 23, 2024 5:59 pm

Code: Select all

RunProgram("open", #DQUOTE$ + filePath$ +#DQUOTE$,"")  ; file with space not working
The above works here…

Anyway, I'm getting tired about repeating it:
the problem is that on Mac the runprogram PARAMETER is interpreted 'too literally'
when needed, you cannot "quote/escape/fix" it!!!

using a workaround (e.g., the simpleShell Procedure posted above, named simple because it doesn't return anything)
you can enjoy the power of a "shell" (Terminal.app uses the zsh shell) and pipe commands' output to other commands, do regexps,
and much, much, much more!

…the "open" command is NOTHING………
…but try typing "man open" on Terminal…