4 Replies Latest reply on Mar 21, 2012 5:16 PM by absqua

    Can't delete alias if referenced object not exist

    Philipp Speck

      My JavaScript doesn't delete alias in OSX when the referenced object doesn't exist anymore. The bug occurs on a network volumes when the referenced object was deleted or moved from another System. Do you have an idea how to delete/relpace this dead alias with JavaScript? Why it's not possible to delete an alias with a missing object? Thanks for helping me!

       

      myShortcut.remove();

       

      Bildschirmfoto 2012-03-21 um 10.52.26.png

        • 1. Re: Can't delete alias if referenced object not exist
          Philipp Speck Level 1

          Of course the code is working as long as the alias ist valid.

          • 2. Re: Can't delete alias if referenced object not exist
            absqua Level 4

            Hmm. I have no problem removing broken aliases created in the Finder, but am not able to delete broken symlinks on remote filesystems created with ln -s (or with file.createAlias()). Seems like a bug to me, as the broken symlink can be deleted any other way (in the Finder or with rm). I suppose you can use doScript() and an Applescript:

             

            app.doScript('tell application "Finder" to delete POSIX file "' + myShortcut.fsName + '"', ScriptLanguage.APPLESCRIPT_LANGUAGE);
            

             

            Slow, though. You could use a "do shell script" Applescript with rm but the multiple levels of quoting and escaping are a headache.

             

            Jeff

            • 3. Re: Can't delete alias if referenced object not exist
              John Hawkinson Level 5

              Slow, though. You could use a "do shell script" Applescript with rm but the multiple levels of quoting and escaping are a headache.

              Indeed. I use this function to make it easier. It doesn't handle absolutely everything, but gets most of the quoting right (backslashes, single quotes, and double quotes):

               

              function shell(cmd) {
                      var
                              rv,
                              call ='do shell script "'+
                                      cmd.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+
                                      '"';
              
                      try {
                              rv = app.doScript(call,
                                      ScriptLanguage.APPLESCRIPT_LANGUAGE);
                      } catch(e0) {
                              rv = e0+"\n"+(rv?"":rv);
                      }
                      return rv;
              }
              
              • 4. Re: Can't delete alias if referenced object not exist
                absqua Level 4

                I'll file that one away. Thanks John!