【问题标题】:Tagging files with colors in OS X Finder from shell scripts在 OS X Finder 中从 shell 脚本中用颜色标记文件
【发布时间】:2011-01-26 23:51:06
【问题描述】:

可以在 Mac OS X Finder 中用颜色标记文件和文件夹。 有没有办法通过 shell 脚本做到这一点?

【问题讨论】:

    标签: macos applescript finder


    【解决方案1】:

    此 shell 脚本将文件或文件夹名称作为第一个参数,将标签索引(0 表示无标签,1 表示红色,...,7 表示灰色)作为第二个参数。

    #!/bin/sh
    osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"`cd -P -- "$(dirname -- "$1")" && printf '%s\n' "$(pwd -P)/$(basename -- "$1")"`\" to $2"
    

    更直接地说,如果$filename是一个shell变量,带有要标记的文件或文件夹的绝对路径名,而$label是一个带有标签索引号的shell变量,

    osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"$filename\" to $label"
    

    是一个shell命令,用于将标签分配给文件或文件夹。

    【讨论】:

    • 如果文件名包含双引号或以反斜杠结尾,这将失败。
    • @Kevin:有什么解决办法吗?
    • @Kevin:还有……你为什么会有一个包含双引号的文件名?我认为这是无效的......或者可能只是在 Windows 中......
    • 文件名中唯一不允许的字符是 NUL (U+0000) 和路径分隔符(根据您查看的 API 是“/”或“:”)。将字符串传递给 AppleScript 的一种安全方法是将命令行参数提供给 osascript(即 osascript -e <script> <arg>,然后使用脚本内的 on run theArguments ... end run 处理程序检索它们。
    • 标签的顺序与 Finder 中的不同。 1 为橙色。
    【解决方案2】:

    这是我写的一个快速的 Python 脚本:

    https://github.com/danthedeckie/finder_colors

    从命令行设置文件夹和文件的颜色。

    用法:

    finder_colors.py red /Users/daniel/src
    

    将 /Users/daniel/src 目录设置为红色。

    finder_colors.py /Users/daniel/src
    

    返回颜色(在本例中为“红色”)。如果您正在编写 python 脚本,您可以将 finder_colors 作为模块导入,并直接使用它(finder_colors.get(...) 和 finder_colors.set(...)。

    【讨论】:

    • 如果你解释一下你是怎么做的,会更有帮助。通过您解决方案的相关代码示例。
    • 感谢@Pfitz - 我添加了使用信息。它在内部工作的方式是使用内置的 python 库来编辑文件的扩展属性。我找不到优雅的原生 shell 脚本方法来执行此操作,并且不喜欢 apple-script 解决方案。
    • 执行我,但您的代码在扫描“/Desktop/image.jpeg”等文件时不起作用。你有什么想法来解决这个问题吗?提前致谢。
    【解决方案3】:

    根据此处和参考帖子中的回复,我制作了以下函数并将其添加到我的~/.bash_profile 文件中:

    # Set Finder label color
    label(){
      if [ $# -lt 2 ]; then
        echo "USAGE: label [0-7] file1 [file2] ..."
        echo "Sets the Finder label (color) for files"
        echo "Default colors:"
        echo " 0  No color"
        echo " 1  Orange"
        echo " 2  Red"
        echo " 3  Yellow"
        echo " 4  Blue"
        echo " 5  Purple"
        echo " 6  Green"
        echo " 7  Gray"
      else
        osascript - "$@" << EOF
        on run argv
            set labelIndex to (item 1 of argv as number)
            repeat with i from 2 to (count of argv)
              tell application "Finder"
                  set theFile to POSIX file (item i of argv) as alias
                  set label index of theFile to labelIndex
              end tell
            end repeat
        end run
    EOF
      fi
    }
    

    【讨论】:

      【解决方案4】:

      一种丑陋的做法是:

      exec osascript <<\EOF
      tell app "Finder"
      
          -- [...]
          -- selecting the file
          -- [...]
      
          -- 4 is Blue
          set label index of thisItem to 4
      end tell
      

      基本上是启动一个使用finder来设置颜色的applescript。

      我得到了提示:

      (颜色)http://www.macosxhints.com/article.php?story=20070602122413306

      (外壳)http://www.macosxhints.com/article.php?story=20040617170055379

      【讨论】:

        【解决方案5】:

        osxutils 包中还有命令行工具“setlabel”。它不需要 AppleScript 或 Finder 正在运行。

        【讨论】:

          【解决方案6】:

          这将使用与 Finder 相同的颜色顺序。

          #!/bin/bash
          
          if [[ $# -le 1 || ! "$1" =~ ^[0-7]$ ]]; then
            echo "Usage: label 01234567 file ..." 1>&2
            exit 1
          fi
          
          colors=( 0 2 1 3 6 4 5 7 )
          n=${colors[$1]}
          shift
          
          osascript - "$@" <<END > /dev/null 2>&1
          on run arguments
          tell application "Finder"
          repeat with f in arguments
          set f to (posix file (contents of f) as alias)
          set label index of f to $n
          end repeat
          end tell
          end
          END
          

          我正在重定向 STDERR,因为我在 10.8 上收到了类似 2012-09-06 13:50:00.965 osascript[45254:707] CFURLGetFSRef was passed this URL which has no scheme (the URL may not work with other CFURL routines): test.txt 的警告。 STDOUT 被重定向,因为 osascript 打印最后一个表达式的值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-09-21
            • 2014-11-20
            • 2012-03-25
            • 2011-10-19
            • 2014-04-13
            • 2011-02-16
            • 2016-03-06
            相关资源
            最近更新 更多