Unable to preview/open a file including spaces in filename

Mac OSX specific forum
flashbob
User
User
Posts: 92
Joined: Sat May 11, 2024 4:04 pm

Unable to preview/open a file including spaces in filename

Post 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

User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

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

Post by Lord »

Try enclosing parameter in quotes:

Code: Select all

RunProgram("open", #DQUOTE$+"Test 1.pdf"+#DQUOTE$, "")
Image
flashbob
User
User
Posts: 92
Joined: Sat May 11, 2024 4:04 pm

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

Post by flashbob »

... perfect, that's how it works. Thx a lot!
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post 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"))
Bmld756
User
User
Posts: 30
Joined: Mon Sep 19, 2022 3:30 pm

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

Post by Bmld756 »

Hello,

That right for RunProgram, but for no problem with the space with SetCurrentDirectory or OpenFile.
IMAC 21.5 2012 Core I5 - 2.70 Ghz. 16 GB NVIDIA GeForce GT 640M 512 Mo. MacOs OCPL Sequoia 15.0
MacBook Air M1 - 8Go - Sonoma 14.1
Wolfram
Enthusiast
Enthusiast
Posts: 604
Joined: Thu May 30, 2013 4:39 pm

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

Post 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

Last edited by Wolfram on Sat Aug 24, 2024 8:18 am, edited 1 time in total.
macOS Catalina 10.15.7
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

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

Post 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$))
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post 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!!!
User avatar
Piero
Addict
Addict
Posts: 865
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post 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…
Post Reply