Convert path system to POSIX does not convert blanks and special chars

All 4D versions on macOS

I use a lot of LEP commands on macOS.
The command Convert path system to POSIX does not convert the macOS path names to use them with LEP terminal commands. I have to escape the special chars by myself with a method call.

Can we get an extra parameter to escape the chars to use it with LEP terminal commands ?

Example:

ACTUAL RESULT:
This path

“Catalina - Data:Users:armindeeg:CoreBrightness-backup:Mein Ordner (wichtige Exposés !):Test.txt”

is converted with the command Convert path system to POSIX to

“/Volumes/Catalina - Data/Users/armindeeg/CoreBrightness-backup/Mein Ordner (wichtige Exposés !)/Test.txt”

EXPECTED RESULT:
I expected something like this one to use it with LEP =

screen_terminal

This could be done with an extra parameter, maybe:

$LEP_path:=Convert path system to POSIX($HFS_path;escape mac chars)

Armin

Don’t forget the star :star:

Vincent,

the Star … :slight_smile: it looks different, and whats about the “()!” signs ?

/Volumes/Catalina%20-%20Data/Users/armindeeg/CoreBrightness-backup/Mein%20Ordner%20(wichtige%20Expos%C3%A9s%20!)/Test.txt

Bildschirmfoto 2020-05-15 um 18.39.26

If you think it’s a bug, I suggest you to fill one

Just a question, did you try to put the pathname into simple quotes for LEP ?
I always do it.

Escaping the space character with a backslash is not a POSIX thing, it is a terminal thing. I wouldn’t expect Convert path system to POSIX to do that for you, POSIX path is not limited to usage with LAUNCH EXTERNAL PROCESS.

That said, in the screenshot you already have code to escape meta characters.

Armin,
pathnames used in LEP needs to be quoted. Use something like “’”+$LEP_path+"’" . For more information about Pathnames in Shell see this very good writing: “Filenames and Pathnames in Shell: How to do it Correctly

Personally, I would avoid this kind of code, because the path can also have single quotes. If you are sure that would never happen, go for it.

Miyako, you are right.

#to quote a path with a single quote in it, eg: /folder with ' single quote
# you can use one of these:
path_quoted='/folder with '\'' single quote'
path_quoted='/folder with '"'"' single quote'

as picture:

But I couldn´t find a way to make it work with LEP other than write a shell script to disk and execute it. So in the meantime I will reject pathnames with single quotes in it.

You just need to escape meta-characters with a back-slash.

You can see the code in the screenshot from post:3

basically it goes like this:

C_TEXT($1;$0;$param)

If (Count parameters#0)
	
	$param:=$1
	
	C_LONGINT($platform;$i;$len)
	_O_PLATFORM PROPERTIES($platform)
	
	If ($platform=Windows)
		
		  //argument escape for cmd.exe; other commands (curl, ruby, etc.) may be incompatible
		
		$shoudQuote:=False
		
		$metacharacters:="&|<>()%^\" "
		
		$len:=Length($metacharacters)
		
		For ($i;1;$len)
			$metacharacter:=Substring($metacharacters;$i;1)
			$shoudQuote:=$shoudQuote | (Position($metacharacter;$param;*)#0)
			If ($shoudQuote)
				$i:=$len
			End if 
		End for 
		
		If ($shoudQuote)
			If (Substring($param;Length($param))="\\")
				$param:="\""+$param+"\\\""
			Else 
				$param:="\""+$param+"\""
			End if 
		End if 
		
	Else 
		
		$metacharacters:="\\!\"#$%&'()=~|<>?;*`[] "
		
		For ($i;1;Length($metacharacters))
			$metacharacter:=Substring($metacharacters;$i;1)
			$param:=Replace string($param;$metacharacter;"\\"+$metacharacter;*)
		End for 
		
	End if 
	
End if 

$0:=$param
1 Like

Thank you for your Code.

I´ve made some little extensions:

  // MethodName: File_Escaped ($path_t) // returns the escaped path (as C_TEXT) for LEP (shell)
  //
  // ----------------------------------------------------
  // Modified:
  // 2020-05-18T00:00:00 created by bb with code from Keisuke Miyako
  //
C_TEXT($0)
C_TEXT($1;$path_t)

C_BOOLEAN($shoudQuote_b)
C_LONGINT($pos_l)
C_LONGINT($len_l)
C_TEXT($metacharacter_current_t)
C_TEXT($metacharacters_list_t)

If (False)
	C_TEXT(File_Escaped ;$0)
	C_TEXT(File_Escaped ;$1)
End if 

If (Count parameters#1)
	$0:=""
	ErrorLogging (-2005181;1;"Count parameters#1"+" Method: '"+Current method name+"'")  //  
	
Else   // ok
	$path_t:=$1
	
	If (Is Windows)  //argument escape for cmd.exe; other commands (curl, ruby, etc.) may be incompatible
		$shoudQuote_b:=False
		$metacharacters_list_t:="&|<>()%^\" "
		$len_l:=Length($metacharacters_list_t)
		
		For ($pos_l;1;$len_l)
			$metacharacter_current_t:=Substring($metacharacters_list_t;$pos_l;1)
			$shoudQuote_b:=$shoudQuote_b | (Position($metacharacter_current_t;$path_t;*)#0)
			If ($shoudQuote_b)
				$pos_l:=$len_l
			End if 
		End for 
		
		If ($shoudQuote_b)
			If (Substring($path_t;Length($path_t))="\\")
				$path_t:="\""+$path_t+"\\\""
			Else 
				$path_t:="\""+$path_t+"\""
			End if 
		End if 
		
		
		If ($path_t[[1]]="-")  //  $$ untested   // Check if a pathname begins with “-” when accepting pathnames, and then prepend “.\” if it does  //https://dwheeler.com/essays/filenames-in-shell.html 
			$path_t:=".\\"+$path_t
		End if 
		
		
		
	Else   // is macOS
		$metacharacters_list_t:="\\!\"#$%&'()=~|<>?;*`[] "
		For ($pos_l;1;Length($metacharacters_list_t))
			$metacharacter_current_t:=Substring($metacharacters_list_t;$pos_l;1)
			$path_t:=Replace string($path_t;$metacharacter_current_t;"\\"+$metacharacter_current_t;*)
		End for 
		
		If ($path_t[[1]]="-")  // Check if a pathname begins with “-” when accepting pathnames, and then prepend “./” if it does  //https://dwheeler.com/essays/filenames-in-shell.html
			$path_t:="./"+$path_t
		End if 
		
	End if   // mac/win
	
	$0:=$path_t
	
End if 
  // EoM //