When to use GetPathSeparator()

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mcmurchy1917
Posts: 23
Joined: Fri Feb 22, 2013 2:15 pm

When to use GetPathSeparator()

Post by mcmurchy1917 »

I'm using LibreOffice on Linux. I've effectively no access to Windows.

I built a macro that is to be used in a Linux and a Windows environment which needs access to several files relative to the location of the current file.

I got the macro working correctly in Linux, then remembering this statement from Andrew D. Pitonyak
URL notation is system independent, so URL paths work as well on an Apple computer as they do on a Windows computer. To create a system-specific path, use the function GetPathSeparator to obtain the system-specific path separator. Listing 3 demonstrates how to use GetPathSeparator to build a complete path. Windows-based computers use “\” as the path separator, and Unix-based computers use “/” as the path separator. URL notation uses “/” as the separator regardless of the operating system.
So before migrating to windows I changed all explicit "/" to GetPathSeparator() and a colleague tested it on window.

This was the original code

Code: Select all

	oDoc = ThisComponent	
	If (oDoc.hasLocation()) Then
	   sDocURL = oDoc.getURL()
	   sDirectoryName = DirectoryNameoutofPath(sDocURL, "/")
	End If   

    url = ConvertToURL(sDirectoryName) & "/" & "ReplacementCharacters.ods"

	If Not FileExists(url) Then
		MsgBox( url & " doesn't exist  - see instruction manual")
		exit sub
	End If
This is the converted code

Code: Select all

	oDoc = ThisComponent	
	If (oDoc.hasLocation()) Then
	   sDocURL = oDoc.getURL()
	   sDirectoryName = DirectoryNameoutofPath(sDocURL, GetPathSeparator())
	End If   

    url = ConvertToURL(sDirectoryName) & GetPathSeparator() & "ReplacementCharacters.ods"

	If Not FileExists(url) Then
		MsgBox( url & " doesn't exist  - see instruction manual")
		exit sub
	End If
On Windows the macro fails to find the file and displays the message box
file:///C:/Users/alex/Documents/website/extracts/test.odt\ReplacementCharacters.ods doesn't exist - see instruction manual
On Linux the macro found the correct file
file:///home/alex/Documents/website/extracts/ReplacementCharacters.ods
So I reverted back to using "/" instead of GetPathSeparator() and the macro worked in Linux as well as Windows, which confirms Andrew's statement
URL notation uses “/” as the separator regardless of the operating system.
above.

Both Windows and Linux then both correctly looked for and found the file

My question is under what circumstances would one use GetPathSeparator()?
Slackware user
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: When to use GetPathSeparator()

Post by Villeroy »

I never used that function. ConvertFromURL and ConvertToURL is all you need.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: When to use GetPathSeparator()

Post by RoryOF »

Villeroy wrote:I never used that function. ConvertFromURL and ConvertToURL is all you need.
I agree with Villeroy; GetPathSeparator might only be needed if one was constructing a complex path from components.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: When to use GetPathSeparator()

Post by Villeroy »

RoryOF wrote:
Villeroy wrote:I never used that function. ConvertFromURL and ConvertToURL is all you need.
I agree with Villeroy; GetPathSeparator might only be needed if one was constructing a complex path from components.
But only if you need to compose them from components in system notation and you can not be sure about the running system. I use to concatenate an URL path with regular slashes and then convert back and forth as needed.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply