7 Replies Latest reply on Feb 28, 2017 7:45 AM by Loic.Aigon

    Mac files url to HFS path

    Loic.Aigon Adobe Community Professional

      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

        • 1. Re: Mac files url to HFS path
          TᴀW Adobe Community Professional & MVP

          Hi Loic,

           

          How about:

           

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

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

           

          Ariel

          • 2. Re: Mac files url to HFS path
            Loic.Aigon Adobe Community Professional

            Well I guess I will go with this one

             

            Thx

             

            Loic

            • 3. Re: Mac files url to HFS path
              Laubender Adobe Community Professional & MVP

              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[n].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[n].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

              • 4. Re: Mac files url to HFS path
                TᴀW Adobe Community Professional & MVP

                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

                • 5. Re: Mac files url to HFS path
                  Loic.Aigon Adobe Community Professional

                  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

                  • 6. Re: Mac files url to HFS path
                    Trevorׅ Adobe Community Professional

                    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

                    • 7. Re: Mac files url to HFS path
                      Loic.Aigon Adobe Community Professional

                      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