Ral*_*ert 9 macos applescript finder
可以在Mac OS X Finder中使用颜色标记文件和文件夹.有没有办法从shell脚本执行此操作?
此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"
Run Code Online (Sandbox Code Playgroud)
更直接的说,如果$ filename是一个shell变量,其中包含要标记的文件或文件夹的绝对路径名,$ label是一个带有标签索引号的shell变量,
osascript -e "tell application \"Finder\" to set label index of alias POSIX file \"$filename\" to $label"
Run Code Online (Sandbox Code Playgroud)
是一个shell命令,用于将标签分配给文件或文件夹.
这是我写的一个快速python脚本:
https://github.com/danthedeckie/finder_colors
它从命令行设置文件夹和文件的颜色.
用法:
finder_colors.py red /Users/daniel/src
Run Code Online (Sandbox Code Playgroud)
将/ Users/daniel/src目录设置为红色.
finder_colors.py /Users/daniel/src
Run Code Online (Sandbox Code Playgroud)
返回颜色(在这种情况下,现在是'红色').如果您正在编写python脚本,则可以将finder_colors作为模块导入,并直接使用它(finder_colors.get(...)和finder_colors.set(...).
小智 5
根据此处和参考帖子中的回复,我创建了以下函数并将其添加到我的~/.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
}
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           8510 次  |  
        
|   最近记录:  |