• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Mac files url to HFS path

People's Champ ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Hi,

Does anyone of you knows to convert this:

"/Users/user/Desktop/myFile.ext"

into

"Macintosh HD:Users:user:Desktop:myFile.ext"

Starting from a File myFile.ext located on the desktop ?

I can do string concatenations and stuff or call applescript but I was looking at something more meaningful.

TIA

Loic

TOPICS
Scripting

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Feb 24, 2017 Feb 24, 2017

Hi Loic,

How about:

f = new File(Folder.desktop + "/myFile.ext");

alert("Macintosh HD" + f.fsName.replace(/\//g, ":");

Ariel

Votes

Translate

Translate
People's Champ ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Hi Loic,

How about:

f = new File(Folder.desktop + "/myFile.ext");

alert("Macintosh HD" + f.fsName.replace(/\//g, ":");

Ariel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Well I guess I will go with this one

Thx

Loic

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Hi Ariel,

I would feel a little bit uneasy with this.

On Mac OSX file names like shown in the screenshot below are also possible:

( "Schreibtisch" => "Desktop" )

SpecialNamedTextFilesOnDesktop-MacOSX.png

Placed as linked text files with InDesign on OSX showing in the Links panel:

LinkedTextFilesPlaced.png

If I am using menu $ID/#LinksUICopyPlatformPath I get this with my text editor which is looking very promissing:

Macname:Users:Username:Desktop:my/\File.txt

Macname:Users:Username:Desktop:my\/File.txt

And indeed, if I am looping the links and ask for filePath values I get exactly the results from above:

var doc = app.documents[0];

var links = doc.links.everyItem().getElements();

for(var n=0;n<links.length;n++)

{

    $.writeln(n+"\t"+links.filePath);

};

/*

    Result:

    0    Macname:Users:Username:Desktop:my/\File.txt

    1    Macname:Users:Username:Desktop:my\/File.txt

   

*/

I'm not so sure with your code.

Hope I interpret it right. Correct me if I'm wrong.

We have a File object do fsName on it and run a RegExp where a slash is replaced by a colon.

And in front of the result string we add the name of the machine:

var doc = app.documents[0];

var links = doc.links.everyItem().getElements();

for(var n=0;n<links.length;n++)

{

    $.writeln(n+"\t"+"Macname"+File(links.filePath).fsName.replace(/\//g, ":"));

};

/*

    Result:

    0    Macname:Users:Username:Desktop:my:\File.txt

    1    Macname:Users:Username:Desktop:my\:File.txt

   

*/

The result is not looking good…

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 25, 2017 Feb 25, 2017

Copy link to clipboard

Copied

Hi Uwe,

It is not best practice to use slashes in filenames on the Mac: OS X: Cross-platform filename best practices and conventions - Apple Support

If Loic wishes to account for users who are using slashes nevertheless, I guess the thing to do would be to run the GREP on just the path part of the name and then add the file name. Something like this, I suppose (untested):

f = new File(Folder.desktop + "/myFile.ext");

alert("Macintosh HD" + f.parent.fsName.replace(/\//g, ":") + ":" + f.name);

Ariel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 27, 2017 Feb 27, 2017

Copy link to clipboard

Copied

Hi guys,

No need to tear your heads off on this one. It's a very specific need. A scriptable plugin defines some function argument to be a HFS path. I was wondering if there was any predictable way of turning a file path into a HFS path like a non revealed method File.toHFS()

There will be one user (my client) so I can presume the context but It would have been nice to be agnostic.

Loic

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Hi All,

While I agree with Ariel that anyone who uses / or \ in a filename deserves at very least to have his system explode regardless of whether his using Mac, Android, Commodore 64  or Sinclair ZX8.

The above suggested solution however is not to great (even if it satisfies Loic).

My Mac has 2 internal hard drives, 2 external hard drives (Partition to into 6 partitions)  and about 6 server drives. Ok. that might be an overkill but that's how it is. Only one of them is called "Macintosh HD".

Anyways if one on InDesign then one can user this simple POSIX to HFS conversion.

It will even handle cases that don't deserve to be taken care of, (Maybe it will fail if the name contains a "  in it but that would not be tough to take care of)

var f, as;

f = Folder.desktop; // change as needed

as = 'POSIX file "' + f.fsName + '" as Unicode text';

alert(app.doScript(as, ScriptLanguage.APPLESCRIPT_LANGUAGE));

2017-02-28_17-22-32.png

Hope that helps someone, regards,

Trevor

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Hi Trevor,

Ariel's solution was fine for my really specific context. Yours extends the scope which is fine but this is typically what I wish I could avoid meaning relying on the context (like using applescript). I just think there isn't any universal and agnostic solution.

Thanks anyway

Loic

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 27, 2018 Jun 27, 2018

Copy link to clipboard

Copied

hi Trevor

Just to let you know that I will use this line of AS Code in my current project. So it was a great addition

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 27, 2018 Jun 27, 2018

Copy link to clipboard

Copied

Thanks for letting me know.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 27, 2018 Jun 27, 2018

Copy link to clipboard

Copied

You can pass an array of arguments thru app.doScript, no need to convert the Folder to a literal.

Apparently this even works if you omit the UndoMode parameter.

Besides the file/folder argument is already passed as alias or similar, so we can omit the "POSIX file" coercion.

alert(app.doScript("item 1 of arguments as Unicode text", ScriptLanguage.APPLESCRIPT_LANGUAGE,[Folder.desktop]));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2024 Feb 11, 2024

Copy link to clipboard

Copied

LATEST

Use pathfinder - right-click on the folder and copy path as HFS .

Capture d’écran 2024-02-11 à 10.21.24.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines