When to use GetPathSeparator()

Creating a macro - Writing a Script - Using the API

When to use GetPathSeparator()

Postby mcmurchy1917 » Mon Feb 19, 2018 10:40 am

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   Expand viewCollapse view
   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   Expand viewCollapse view
   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
mcmurchy1917
 
Posts: 22
Joined: Fri Feb 22, 2013 2:15 pm

Re: When to use GetPathSeparator()

Postby Villeroy » Mon Feb 19, 2018 11:37 am

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: When to use GetPathSeparator()

Postby RoryOF » Mon Feb 19, 2018 11:44 am

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.6 on Xubuntu 18.04.2 (mostly 64 bit version) and infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 28180
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: When to use GetPathSeparator()

Postby Villeroy » Mon Feb 19, 2018 11:48 am

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests