osx.plugin.zsh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # ------------------------------------------------------------------------------
  2. # FILE: osx.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.0.1
  6. # ------------------------------------------------------------------------------
  7. function tab() {
  8. local command="cd \\\"$PWD\\\""
  9. (( $# > 0 )) && command="${command}; $*"
  10. the_app=$(
  11. osascript 2>/dev/null <<EOF
  12. tell application "System Events"
  13. name of first item of (every process whose frontmost is true)
  14. end tell
  15. EOF
  16. )
  17. [[ "$the_app" == 'Terminal' ]] && {
  18. osascript 2>/dev/null <<EOF
  19. tell application "System Events"
  20. tell process "Terminal" to keystroke "t" using command down
  21. tell application "Terminal" to do script "${command}" in front window
  22. end tell
  23. EOF
  24. }
  25. [[ "$the_app" == 'iTerm' ]] && {
  26. osascript 2>/dev/null <<EOF
  27. tell application "iTerm"
  28. set current_terminal to current terminal
  29. tell current_terminal
  30. launch session "Default Session"
  31. set current_session to current session
  32. tell current_session
  33. write text "${command}"
  34. end tell
  35. end tell
  36. end tell
  37. EOF
  38. }
  39. }
  40. function pfd() {
  41. osascript 2>/dev/null <<EOF
  42. tell application "Finder"
  43. return POSIX path of (target of window 1 as alias)
  44. end tell
  45. EOF
  46. }
  47. function pfs() {
  48. osascript 2>/dev/null <<EOF
  49. set output to ""
  50. tell application "Finder" to set the_selection to selection
  51. set item_count to count the_selection
  52. repeat with item_index from 1 to count the_selection
  53. if item_index is less than item_count then set the_delimiter to "\n"
  54. if item_index is item_count then set the_delimiter to ""
  55. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  56. end repeat
  57. EOF
  58. }
  59. function cdf() {
  60. cd "$(pfd)"
  61. }
  62. function pushdf() {
  63. pushd "$(pfd)"
  64. }
  65. function quick-look() {
  66. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  67. }
  68. function man-preview() {
  69. man -t "$@" | open -f -a Preview
  70. }
  71. function trash() {
  72. local trash_dir="${HOME}/.Trash"
  73. local temp_ifs=$IFS
  74. IFS=$'\n'
  75. for item in "$@"; do
  76. if [[ -e "$item" ]]; then
  77. item_name="$(basename $item)"
  78. if [[ -e "${trash_dir}/${item_name}" ]]; then
  79. mv -f "$item" "${trash_dir}/${item_name} $(date "+%H-%M-%S")"
  80. else
  81. mv -f "$item" "${trash_dir}/"
  82. fi
  83. fi
  84. done
  85. IFS=$temp_ifs
  86. }