在 Mac OS X 中从命令行对文件执行“获取信息”

在 Mac OS X 中从命令行对文件执行“获取信息”

如何让“获取信息”窗口从命令行出现,就像您在 Finder 中点击Command-一样I?我可以用 applescript 编写它...但如果可以的话,我会避免这样做。

答案1

这是我更新的脚本版本(包括对原始来源的引用,因为我几年前就发现了它)。功能上的主要变化是它将处理包含 MacRoman 和 UTF-8 之间编码不一致的字符的路径名(ASCII 之外的任何字符)。

#!/bin/sh
# Requires a POSIX-ish shell.

#
# Originally From: http://hayne.net/MacDev/Bash/show_getinfo
#

# show_getinfo
# This script opens the Finder's "Get Info" window
# for the file or folder specified as a command-line argument.
# Cameron Hayne ([email protected])  March 2003

# Chris Johnsen <[email protected]> August 2007, December 2009
#   Include Unicode path in AppleScript code via "utxt" block(s).
#   Handle case where cwd ends in newline.

utf8_to_AppleScript_utxt() {
    o="$(printf '\302\253')" # UTF-8 LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
    c="$(printf '\302\273')" # UTF-8 RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
    # AppleScript utxt:
    # <http://lists.apple.com/archives/applescript-implementors/2007/Mar/msg00024.html>
    # <<data utxtXXXX>> where
    #     << is actually U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
    #     >> is actually U+00BB RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
    #   XXXX are the hex digits of UTF-16 code units
    # If a BOM is present, it specifies the byte order.
    #   The BOM code point will not be a part of the resulting string value.
    # If no BOM is present, the byte order interpreted as native.
    # The iconv invocation below *MUST*
    #      include a BOM
    #   or produce native byte ordering
    #   or include a BOM and produce native byte ordering.
    # In my testing, iconv to UTF-16 includes a BOM and uses native ordering.
    iconv -f UTF-8 -t UTF-16 |
    ( printf '("" as Unicode text'
        hexdump -ve "\" & ${o}data utxt\" 63/2 \"%04x\" \"$c\""
        printf ')\n' ) |
    sed -e 's/  *\('"$c"')\)$/\1/'
}

scriptname="${0##*/}"
if test "$#" -lt 1; then
    printf "usage: %s file-or-folder\n" "$scriptname"
    exit 1
fi

if ! test -e "$1"; then
    printf "%s: No such file or directory: %s\n" "$scriptname" "$1"
    exit 2
fi

if test "${1#/}" = "$1"; then set -- "$PWD/$1"; fi

set -- "$(printf %s "$1" | utf8_to_AppleScript_utxt)"

# 10.4 requires script text to be in the primary encoding (usually MacRoman)
# 10.5+ supports UTF-8, UTF-16 and the primary encoding
(iconv -f UTF-8 -t MACROMAN | osascript -) <<EOF
set macpath to POSIX file $1 as alias
tell app "Finder" to open information window of macpath
EOF

答案2

这还支持多个文件并使 Finder 处于活动状态。utxt 方法仅在 10.4 及更早版本中才需要。

si() {
    osascript - "$@" <<-END > /dev/null 2>&1
    on run args
    tell app "Finder"
    activate
    repeat with f in args
    open information window of (posix file (contents of f) as alias)
    end
    end
    end
    END
}

STDOUT 被重定向,因为 osascript 打印最后一个表达式的结果,STDERR 被重定向,因为 10.8 显示了如下警告CFURLGetFSRef 传递了这个没有协议的 URL当相对路径转换为别名时。

答案3

ls -l我知道我并没有真正回答你的问题,但是我通过使用和命令获得了我需要的很多信息file

答案4

我尝试了几个脚本来做到这一点(即从命令行弹出一个信息窗口)。

它们都有效

除了

对于别名和常规文件,它们显示符号链接(symlinks)的正确内容,显示底层文件的文件信息。这不是在查找器中选择符号链接上的“获取信息”所显示的内容。

我一直在尝试使用 applescript 代码来修复这个问题,但目前还没有成功。

我确实写了一个简单的脚本来处理一次获取多个文件的信息。

#!/bin/sh

# show_getinfo
# loop to use getinfo script copied from web on several files

if [ $# -lt 1 ]; then
    echo "Usage: `basename $0` file_or_folder"
    exit
fi

GETINFO_SCRIPT=$HOME/Dropbox/Unix/Scripts/getinfo2_quiet.sh

for F in $*
    do
    if ! test -e "$F"; then
        echo "`basename $0`: No such file or directory: $F"
        continue
    fi
    sh $GETINFO_SCRIPT "$F"
done

相关内容