1

I am running a nodewebkit application, and I want to search a folder for aliases.

The following code is working, but not recognizing folder aliases or file aliases as symbolic links.

Where am I wrong?

    var path = '/Users/test/Desktop/testfolder';
    var fs = require('fs');
    fs.readdir(path, function(err, files) {
        if (err) return;
        files.forEach(function(f) {
            var newPath = path + '/' + f;
            console.log("looking for "+ newPath +" symlink: "+fs.lstatSync(path).isSymbolicLink());
            fs.lstat(newPath, function(err, stats){
                if(err){
                    console.log(err);
                }
                if(stats.isFile()){
                    console.log('is file mofo',f);
                }
                if(stats.isDirectory()){
                    console.log('is Directory mofo',f);
                }
                if(stats.isSymbolicLink()){
                    console.log('is symbolic link');
                }
            });
        });
    });
mklement0
  • 382,024
  • 64
  • 607
  • 775
Silve2611
  • 2,198
  • 2
  • 34
  • 55
  • Are you talking about _Finder.app_ aliases? They are specific to that application and technically distinct from symlinks - you'd need an OS X-specific library for that, I presume. – mklement0 Oct 26 '15 at 01:40
  • Yeah that's what I just found out. Using mdfind is a way but i think it is smarter to just use swift. thx – Silve2611 Oct 26 '15 at 01:49
  • If delegating to a shell command is an option - which is obviously slow - it _is_ possible to detect Finder aliases (come to think of it: that's what you implied by using `mdfind`); out of curiosity: how would you use `mdfind`? How are you planning to use Swift instead? – mklement0 Oct 26 '15 at 01:53
  • My application exists of two parts. One writte in swift and one with node-webkit. I cannot use wift in node.js but I can trigger a call and write semothing into a db. Using URLByResolvingAliasFileAtURL:options:error: – Silve2611 Oct 26 '15 at 01:54
  • mdfind could be used with node-mdfind but the documentation is really bad so I am not really sure on how to use it – Silve2611 Oct 26 '15 at 01:56

1 Answers1

1

Finder aliases on OS X are technologically distinct from symlinks.

From the filesystem's perspective, they are regular files, and only Finder itself and the OS X-specific APIs know to handle them - Node.js has no built-in API for that.

IF calling out to the shell - which will be slow - is an option, you can try the following:

function isFinderAlias(path) {
  var contentType = require('child_process')
        .execFileSync('mdls', 
          [ '-raw', '-name', 'kMDItemContentType', path ], { encoding: 'utf8' })
  return contentType === 'com.apple.alias-file'
}

For a swift-based way to resolve a Finder alias to its target path, see this answer.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775