Skip to main content
Inspiring
November 26, 2024
Answered

Indesign convert URI to Windows format

  • November 26, 2024
  • 3 replies
  • 609 views

Indesign jsx seems to be a bit more advanced than it's Photoshop sibling, which is what I'm used to.

Is there a quick one liner to make a filepath into a nice Windows one? ie

 

Turn

C:\\some%20folder/anotherFolder\\somefile.txt

into

C:\some folder\anotherFolder\somefile.txt

Without having to rely on regex to fix each thing (unifided slashes, %20, etc) as you go?

 

 

 

var afileName = "C:\\some%20folder/anotherFolder\\somefile.txt"

// var resolvedPath = resolve(afileName);
// var normalizedPath = path.normalize(afileName );
var nicePath = decodeURIComponent(afileName);



// Replace all front slashes with back slashes. We are Windows
nicePath = nicePath.replace(/\//g, "\\");

// replace %20 with space
// nicePath = nicePath.replace(/%20/g," ");
alert(nicePath);

 

 

 

This topic has been closed for replies.
Correct answer Peter Kahrel

Nope. You'll have to work with what you've got: use decodeURIComponent() (or decodeURI()) to decode all hex characters, then fix the slashes.

3 replies

Legend
November 27, 2024

ExtendScript has the File class for working with file paths.

There is a File.decode() method that should deal with your %20 .

Likely you won't need it, the constructor is very forgiving.

.You'd create an instance with its constructor function - no need for "new", then access any property.

The fsName property is closest you can get for the platform specific name.

 

var nicePath = File(afileName).fsName

 

 Other properties are listed here 

Above should be the same for PhotoShop and InDesign.

 

As your comments mention modern fs functions, InDesign jsx can also call the UXPSCRIPT language using the app.doScript method. I think PhotoShop is still missing such a method.

You'd call the UXPScript using

var nicePath = app.doScript("yadda",ScriptLanguage.UXPSCRIPT, [afileName]);

The "yadda" part can either refer to a script file or it can be the actual script code.

This won't be a neat single line though, you have to know and use plenty require() to get going at all.

To get started, passing arguments and the result is more, umm, elaborate.

UXP/UXPScript is also designed with security in mind, as opposed to developer convenience.

Have a read at some examples before you go down that road. Reference to the fs and path modules is linked at the bottom of that page. Actual code left as exercise. 😉

 

If you want to avoid the doScript part and start in modern script, you can also do that. Name your script file with ".idjs" extension rather than ".jsx" and stick to the new style.

 

Robert at ID-Tasker
Legend
November 26, 2024

@Ghoul Fool 

 

How do you get your FilePath? As manualy typed string?

 

Inspiring
November 27, 2024

I'm writing a text file. It's a filepath from the source folder of the InDesign file + the name of the Indesign File with a text file extension (instead of a .indd) 

Robert at ID-Tasker
Legend
November 27, 2024
quote

I'm writing a text file. It's a filepath from the source folder of the InDesign file + the name of the Indesign File with a text file extension (instead of a .indd) 


By @Ghoul Fool

 

What returns app.activeDocument.fullName?

 

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
November 26, 2024

Nope. You'll have to work with what you've got: use decodeURIComponent() (or decodeURI()) to decode all hex characters, then fix the slashes.