2 Replies Latest reply on Oct 2, 2012 4:40 PM by thor2112

    Checking last character

    thor2112

      What would you consider being the best way to check the last character of a string?

       

      var folderPath = '~/Desktop/Test';

       

      if (folderPath[folderPath.length - 1] !== '/') { folderPath += '/'; }

       

        or

       

      if (folderPath.slice(-1) !== '/') { folderPath += '/'; }

       

      I see that changePath() can also be used to keep track of this path separator.

       

      Thanks.

        • 1. Re: Checking last character
          -hans- Level 4

          well,

           

          coercing class folder to string will never include the slash ...

           

          Folder('~/Desktop/test/').toString() + '/' //~/Desktop/test/

          Folder('~/Desktop/test').toString() + '/' //~/Desktop/test/

           

          How's the path created¿ why should you test it?

          • 2. Re: Checking last character
            thor2112 Level 1

            Hi Hans,

             

            I was trying to come up with a function to move a folder and it's contents, or a file... similar to how the OS X Finder would do it.  However, I just noticed when the files are copied over, they're not the same file size as the original which makes me think it's stripping resource forks, etc.  :-(

             

            I'm looking now at using doScript and running ditto from the command-line with applescript.  Here's the code I was working with earlier though:

             

            ----------------------------------------

            function moveItem(oldPath, newPath) {

                newPath = newPath.toString();

             

                //if (newPath[newPath.length - 1] !== '/') { newPath += '/'; }

                if (newPath.slice(-1) !== '/') { newPath += '/'; }

               

                var oldPathType = oldPath.constructor.name;

             

                if (oldPathType === 'Folder') {

                    newPath += (oldPath.name + '/');

             

                    with (Folder(newPath)) {

                        if (! exists) { create(); }

                    }

             

                    for each (var i in oldPath.getFiles()) {

                        if (i.constructor.name === 'Folder') {

                            moveItem(i, newPath);

                        } else {

                            i.copy(newPath + i.name);

                            i.remove();

                        }

                    }

             

                    oldPath.remove();

                } else if (oldPathType === 'File') {

                    oldPath.copy(newPath + oldPath.name);

                    oldPath.remove();

                } else {

                    throw 'Problem moving file/folder!';

                }

            }

            ----------------------------------------