Skip to main content
rombanks
Inspiring
Jun 11, 2019 1:23 PM UTC
Answered

tilde doesn't get resolved as user home dir

Hello fellows,

I am writing a small xtendscript that saves a file to a network folder upon hitting Save.

For some reason, the tilde (~) doesn't get resolved as C:\Users\<username>\ in a file path I define (Win10, FM13).

For example:

var FilePath = "~\\AppData\\Roaming\\Adobe\\FrameMaker\\SharePoint\\TestFolder\\Latest Collaterals\\UG\\Sources\\";

if I run alert(FilePath), I get "~\AppData\..."

Any ideas why it doesn't get resolved?

Thanks,

Roman

This topic is closed to new replies. Start a new post to keep the conversation going.
WiedenmaierCorrect answer
Inspiring
Jun 12, 2019 4:48 AM UTC

Hi Markus,

Thanks for your explanations!

It's not quite clear to me why filePath.indexOf("C:\users\") returns Position 0.

Line if (filePath.indexOf($.getenv('AppData'))==0) started working only after I defined a system variable called AppData.

I'd like to test your suggestion to use File.copy() instead of saving the file.

Interestingly enough, I didn't see the File.copy() method in the Scripting Guide, nor in the Javascript Tools Guide.

So, assuming the open file is defined as:

var doc = app.ActiveDoc;

and the target folder to copy the file to is defined as:

var docLib = "T:\\My_UG\\Sources\\";

I've created a new file object:

var oFile = new File(doc.Name);

To copy the file over to the network folder, I tried:

oFile.copy(docLib);

For some reason, it doesn't work. Any idea why?

Thank you!!


Hi Rombanks,

indexOf Returns 0 in this case, as the string starts with the string provided as parameter. Strings starts at the first position, and the first position in an JavaScript array is 0 (not 1). This is by Definition.

this is what "ObjectModel Viewer" (F1 in ESTK) tells for File.copy

File.copy (target: string ): Boolean  Core JavaScript Classes  Copies this object’s referenced file to the specified target location. Resolves any aliases to find the source file. If a file exists at the target location, it is overwritten. Returns true if the copy was successful. target: Data Type: string or File  A string with the URI path to the target location, or a File object that references the target location. Example:  aFile.copy(target) 

File.copy expects and URI or a File object, so do this

var oFile = new File(doc.Name);

oFile.copy(new File(docLib));

or

oFile.copy(new File(docLib).toString());

Make sure, docLib is a file path not not a path to a Folder. docLib sounds like a Folder path. this doesn't work. a file Name is expected here.

File.copy works with an URI not with a Windows file path. An URI starts with file://c:/somepath or ~AppData\Users.... etc.

So this should also work with Network paths, but I haven't testet it yet.

$.getenv reads environment variables from the system. %appdata% is defined by default. not sure why this doesn't work in your environment. Put the string in JavaScript console and see what the result is.

24 replies

Inspiring
Jun 11, 2019 2:36 PM UTC

All you are doing with this code is creating a string and printing it, so there is nothing special going on here.

What you want to do is create a new Folder object and then use its properties and methods to do what you want.

Here are some examples of accepted syntax:

     var folder = new Folder('~/Desktop');

     var folder2 = new Folder(Folder.desktop + '/NewFolder');

     var folder3 = new Folder('//servername/sharename');

     var folder4 = new Folder(Folder.userData);

     alert(folder.fullName);

     alert(folder.fsName);

Full details can be found in the "JavaScript Tools Guide CC" PDF (do a google for it or I think its in the ESTK help menu)

Translate
rombanks
rombanksAuthor
Inspiring
Jun 11, 2019 2:43 PM UTC

Hi,

Thanks for your response!

I am using FM13 and the ESTK that comes with it.

What I do is:

1. Extract the file's path and assign it to the FilePath variable:

...

var FilePath = doc.Name.substr (0, (doc.Name.indexOf(FileName)));

...

Then, I'd like to test whether the FilePath value is  C:\Users\<username>\...

If yes, save to a SharePoint folder. Here is another catch: if the file is checked out,

Translate
Inspiring
Jun 11, 2019 2:49 PM UTC

var FilePath = doc.Name.substr (0, (doc.Name.indexOf(FileName)));

if (FilePath.indexOf($.genenv('appdata'))==0)

{

     //starts with %appdata%

     //so copy suff to network

}

and this has the same effect

if (doc.Name.indexOf($.genenv('appdata'))==0)

{

     //starts with %appdata%

     //so copy suff to Network

}

Hope this helps now.

markus

Translate
Inspiring
Jun 11, 2019 2:07 PM UTC

Hi Rombanks,

try this

var filePath = new File("~\\AppData\\Roaming\\Adobe\\FrameMaker\\SharePoint\\TestFolder\\Latest Collaterals\\UG\\Sources\\")

Hope this helps

Markus

Translate
rombanks
rombanksAuthor
Inspiring
Jun 11, 2019 2:27 PM UTC

Hi Markus,

I appreciate your response!

new File doesn't resolve it either.

What I actually do is check the path of the open file. If it is C:\Users\<username>\..., then save the file to a network folder (in my case, a SharePoint folder).

Thanks,

Roman

Translate
Inspiring
Jun 11, 2019 2:34 PM UTC

Hi,

when I do this in FM 2019 (ESTK CC 4.0.0.1 ES 4.5.5)

var filePath = new File("~\\AppData\\Roaming\\Adobe\\FrameMaker\\SharePoint\\TestFolder\\Latest Collaterals\\UG\\Sources\\")

I get this in console, when I put "filePath" variable in there

C:\Users\Markus\AppData\Roaming\Adobe\FrameMaker\SharePoint\TestFolder\Latest Collaterals\UG\Sources

Maybe, it's me, and i don't understand the problem.

Perhaps, it's also an idea to use FM's Directory properties like:

app.UserHomeDir

app.UserSettingsDir

app.TmpDir

etc.

Markus

Translate