Mac OS Xの「エイリアス」ファイルをすべて見つけて、そのターゲットファイルを見つけるにはどうすればいいですか(壊れたリンクにもかかわらず)?

Mac OS Xの「エイリアス」ファイルをすべて見つけて、そのターゲットファイルを見つけるにはどうすればいいですか(壊れたリンクにもかかわらず)?

私のハードドライブで説明できないシンボリックリンクが見つかりました。残念ながら、これらのリンクは機能しません。つまり、リンクをクリックしても元のファイルにリンクされません。 Finderはこれを「エイリアス」としてマークしますが、Mac OSエイリアスファイルではありません。

今私のハードドライブからすべてのシンボリックリンクを探したいと思います。次に、各ファイルに対してかつて接続されていたファイルを検索します(おそらく同じハードドライブにあります)。次に、ファイルを同じサブディレクトリにコピーします。

おおまかに言えば、私が望む擬似コードソリューションは次のように書くことができます。

FIND all files (in Directory) type=SYMLINK. FOR each one, DO ..
      FIND referentFile (somewhere on the HDD)
      cp referentFile to (Subdirectory that contains the alias).
DONE.

とても感謝しています。

ベストアンサー1

この質問の最初の部分に答えがあります。異なる意見を表現する:

エイリアスを探すコマンドは次のとおりです。

mdfind kMDItemKind="Alias"

一般的に言えば、次のことができます。探す(メタデータ検索)コマンドは、ファイルを(非常に)すばやく検索します。マニュアルページから:

これ探すこのコマンドは、中央メタデータストアを照会し、指定されたメタデータ照会と一致するファイルのリストを返します。クエリは文字列でもクエリ式でもかまいません。

だから私はまだOSXをあきらめていません。実際、mdfindこれは私がLinuxを使用しているときにOSXのために最も恋しいことの1つです。

残念ながら、問題の2番目の部分は驚くべきことに解決するのがより困難です。関連するStackExchangeの投稿が見つかりました。

これにより、いくつかの潜在的なソリューションについて考えることができました。

ブログ投稿中に私が一番好き端末がシンボリックリンクなどのエイリアスに従うようにします。。それを参照するgetTrueName.cという小さなオープンソースCプログラム。ソースコードは次のとおりです。

// getTrueName.c
// 
// DESCRIPTION
//   Resolve HFS and HFS+ aliased files (and soft links), and return the
//   name of the "Original" or actual file. Directories have a "/"
//   appended. The error number returned is 255 on error, 0 if the file
//   was an alias, or 1 if the argument given was not an alias
// 
// BUILD INSTRUCTIONS
//   gcc-3.3 -o getTrueName -framework Carbon getTrueName.c 
//
//     Note: gcc version 4 reports the following warning
//     warning: pointer targets in passing argument 1 of 'FSPathMakeRef'
//       differ in signedness
//
// COPYRIGHT AND LICENSE
//   Copyright 2005 by Thos Davis. All rights reserved.
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of the
//   License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public
//   License along with this program; if not, write to the Free
//   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
//   MA 02111-1307 USA


#include <Carbon/Carbon.h> 
#define MAX_PATH_SIZE 1024
#define CHECK(rc,check_value) if ((check_value) != noErr) exit((rc))

int main ( int argc, char * argv[] ) 
  { 
    FSRef               fsRef; 
    Boolean             targetIsFolder; 
    Boolean             wasAliased; 
    UInt8               targetPath[MAX_PATH_SIZE+1]; 
    char *              marker;

    // if there are no arguments, go away
    if (argc < 2 ) exit(255); 

    CHECK( 255,
      FSPathMakeRef( argv[1], &fsRef, NULL ));

    CHECK( 1,
      FSResolveAliasFile( &fsRef, TRUE, &targetIsFolder, &wasAliased));

    CHECK( 255,
      FSRefMakePath( &fsRef, targetPath, MAX_PATH_SIZE)); 

    marker = targetIsFolder ? "/" : "" ;
    printf( "%s%s\n", targetPath, marker ); 

    exit( 1 - wasAliased );
  }

このソースコードをダウンロードし、次のようにコンパイルしました。

gcc -o getTrueName -framework Carbon getTrueName.c

もちろんテストしてみたいです。端末を離れないように、次のようにエイリアスを作成しました。

user@host:~$ FULL_PATH_TO_TARGET_FILE=/tmp/alias-target

user@host:~$ echo "testing" > "${FULL_PATH_TO_TARGET_FILE}"

user@host:~$ osascript \
-e 'tell application "Finder"' \
-e "make new alias to file (posix file \"${FULL_PATH_TO_TARGET_FILE}\") at desktop" \
-e 'end tell'

alias file alias-target of folder Desktop of folder user of folder Users of disk MacHD

その後、このプログラムを使用してエイリアスを確認しましたgetTrueName

user@host:~$ ./getTrueName ~/Desktop/alias-target

/tmp/alias-target

勝利!

ブログ投稿愚かなMac OS Xのトリック:エイリアスの解決解決策もあるようです。これには、次のPerlスクリプトが含まれています。

#!/usr/local/bin/perl
use Mac::Errors;
use Mac::Files;
use Mac::Resources;

my $path = '/Users/pudge/Desktop/some alias';
my $res  = FSpOpenResFile($path, 0) or die $Mac::Errors::MacError;
# get resource by index; get first "alis" resource
my $alis = GetIndResource('alis', 1) or die $Mac::Errors::MacError;
my $link = ResolveAlias($alis);
print $link;

必要なライブラリもなく、多くをインストールしたくなかったのでこれをあきらめました。

私も見つけました。mac_alias、有望に見えます。 5〜10分間、これは触れましたが、エイリアスの対象を解決する方法がわかりません。

おすすめ記事