Oznaczanie plików kolorami w programie OS X Finder ze skryptów powłoki

W Finderze Mac OS X można oznaczyć pliki i foldery kolorem. Czy istnieje sposób, aby to zrobić ze skryptu powłoki?

Author: woz, 2010-03-12

6 answers

Ten skrypt powłoki przyjmuje nazwę pliku lub folderu jako pierwszy argument i indeks etykiety (0 dla No label, 1 Dla red, ..., 7 dla Graya) jako drugi argument.

#!/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"

Bardziej bezpośrednio, jeśli $filename jest zmienną powłoki z bezwzględną nazwą ścieżki pliku lub folderu, który ma być oznaczony, a $label jest zmienną powłoki z numerem indeksu etykiety,

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

Jest poleceniem powłoki do przypisania etykiety do pliku lub folderu.

 8
Author: Isaac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-12 21:02:58

Oto szybki skrypt Pythona, który napisałem:

Https://github.com/danthedeckie/finder_colors

Który ustawia kolory folderów i plików z linii poleceń.

Użycie:

finder_colors.py red /Users/daniel/src

Ustawia katalog/Users/daniel / src na czerwony.

finder_colors.py /Users/daniel/src

Zwraca kolor (w tym przypadku teraz 'czerwony'). Jeśli piszesz skrypt Pythona, możesz zaimportować finder_colors jako moduł i używać go bezpośrednio (finder_colors.get(...), oraz finder_colors.set(...).

 7
Author: Daniel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-16 11:04:23

Na podstawie odpowiedzi tutaj i w cytowanych postach zrobiłem następującą funkcję i dodałem ją do mojego pliku ~/.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
Author: Robert Harder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-03 15:24:53

Jeden brzydki sposób, aby to zrobić, to:

exec osascript <<\EOF
tell app "Finder"

    -- [...]
    -- selecting the file
    -- [...]

    -- 4 is Blue
    set label index of thisItem to 4
end tell

Uruchamianie applescriptu, który używa Findera do ustawiania koloru.

Dostałem wskazówki od:

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

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

 2
Author: Anton Biller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-12 20:41:50

Istnieje również narzędzie wiersza poleceń 'setlabel' w pakiecie osxutils. Nie wymaga AppleScript ani tego, że Finder jest uruchomiony.

 1
Author: svth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-11 23:44:07

To użyje tej samej kolejności kolorów co 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

Przekierowuję STDERR, bo dostałem Ostrzeżenia typu 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 na 10.8. STDOUT jest przekierowywany, ponieważ osascript wyświetla wartość ostatniego wyrażenia.

 0
Author: user495470,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-16 00:24:13