macos - 如何在 OS X 上使用 CLI 在文件或目录上设置图标?

标签 macos icons command-line-interface

使用 Finder 中的“获取信息”对话框可以直接在文件或目录上设置图标。

  • 从例如复制图像预览
  • 打开文件或目录上的“获取信息”
  • 按 T​​AB 选择图标
  • 粘贴 Cmd-V

  • 但是你如何使用命令行来做到这一点呢?

    最佳答案

    这是它的 bash 脚本“setIcon.sh”

    #!/bin/sh
    # Sets an icon on file or directory
    # Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
    iconSource=$1
    iconDestination=$2
    icon=/tmp/`basename $iconSource`
    rsrc=/tmp/icon.rsrc
    
    # Create icon from the iconSource
    cp $iconSource $icon
    
    # Add icon to image file, meaning use itself as the icon
    sips -i $icon
    
    # Take that icon and put it into a rsrc file
    DeRez -only icns $icon > $rsrc
    
    # Apply the rsrc file to
    SetFile -a C $iconDestination
    
    if [ -f $iconDestination ]; then
        # Destination is a file
        Rez -append $rsrc -o $iconDestination
    elif [ -d $iconDestination ]; then
        # Destination is a directory
        # Create the magical Icon\r file
        touch $iconDestination/$'Icon\r'
        Rez -append $rsrc -o $iconDestination/Icon?
        SetFile -a V $iconDestination/Icon?
    fi
    
    # Sometimes Finder needs to be reactivated
    #osascript -e 'tell application "Finder" to quit'
    #osascript -e 'delay 2'
    #osascript -e 'tell application "Finder" to activate'
    
    rm $rsrc $icon
    

    关于macos - 如何在 OS X 上使用 CLI 在文件或目录上设置图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8371790/

    相关文章:

    macos - 从 Mac 部署 war 到生产环境 |等同于 WinSCP

    安卓通知 : how to post setSmallIcon using custom Bitmap

    CSS 从图像到图标的转变

    android - 如何将 hdpi、mdpi、ldpi 和 xhdpi 文件保存在 Android Assets 文件夹中?

    linux - 我如何在 linux 上制作一个 zip 文件?

    linux - 从 CSV 文件中提取具有唯一值的前 3 行

    swift - 带有 Swift Cocoa 框架的 macOS 命令行工具 : Library not loaded

    xcode - 轻松构建与 MacPorts 捆绑的 Mac OS X .app

    macos - Mac OS X (10.12.6) 与 Python 3.7.0a 安装 lxml 失败

    ios - 如何在开始/完成 iOS 测试时运行/终止 shell 命令?