osx.plugin.zsh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # ------------------------------------------------------------------------------
  2. # FILE: osx.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.1.0
  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}; clear;"
  34. end tell
  35. end tell
  36. end tell
  37. EOF
  38. }
  39. }
  40. function vsplit_tab() {
  41. local command="cd \\\"$PWD\\\""
  42. (( $# > 0 )) && command="${command}; $*"
  43. the_app=$(
  44. osascript 2>/dev/null <<EOF
  45. tell application "System Events"
  46. name of first item of (every process whose frontmost is true)
  47. end tell
  48. EOF
  49. )
  50. [[ "$the_app" == 'iTerm' ]] && {
  51. osascript 2>/dev/null <<EOF
  52. tell application "iTerm" to activate
  53. tell application "System Events"
  54. tell process "iTerm"
  55. tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  56. click
  57. end tell
  58. end tell
  59. keystroke "${command}; clear;"
  60. keystroke return
  61. end tell
  62. EOF
  63. }
  64. }
  65. function split_tab() {
  66. local command="cd \\\"$PWD\\\""
  67. (( $# > 0 )) && command="${command}; $*"
  68. the_app=$(
  69. osascript 2>/dev/null <<EOF
  70. tell application "System Events"
  71. name of first item of (every process whose frontmost is true)
  72. end tell
  73. EOF
  74. )
  75. [[ "$the_app" == 'iTerm' ]] && {
  76. osascript 2>/dev/null <<EOF
  77. tell application "iTerm" to activate
  78. tell application "System Events"
  79. tell process "iTerm"
  80. tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  81. click
  82. end tell
  83. end tell
  84. keystroke "${command}; clear;"
  85. keystroke return
  86. end tell
  87. EOF
  88. }
  89. }
  90. function pfd() {
  91. osascript 2>/dev/null <<EOF
  92. tell application "Finder"
  93. return POSIX path of (target of window 1 as alias)
  94. end tell
  95. EOF
  96. }
  97. function pfs() {
  98. osascript 2>/dev/null <<EOF
  99. set output to ""
  100. tell application "Finder" to set the_selection to selection
  101. set item_count to count the_selection
  102. repeat with item_index from 1 to count the_selection
  103. if item_index is less than item_count then set the_delimiter to "\n"
  104. if item_index is item_count then set the_delimiter to ""
  105. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  106. end repeat
  107. EOF
  108. }
  109. function cdf() {
  110. cd "$(pfd)"
  111. }
  112. function pushdf() {
  113. pushd "$(pfd)"
  114. }
  115. function quick-look() {
  116. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  117. }
  118. function man-preview() {
  119. man -t "$@" | open -f -a Preview
  120. }
  121. function trash() {
  122. local trash_dir="${HOME}/.Trash"
  123. local temp_ifs=$IFS
  124. IFS=$'\n'
  125. for item in "$@"; do
  126. if [[ -e "$item" ]]; then
  127. item_name="$(basename $item)"
  128. if [[ -e "${trash_dir}/${item_name}" ]]; then
  129. mv -f "$item" "${trash_dir}/${item_name} $(date "+%H-%M-%S")"
  130. else
  131. mv -f "$item" "${trash_dir}/"
  132. fi
  133. fi
  134. done
  135. IFS=$temp_ifs
  136. }
  137. function vncviewer() {
  138. open vnc://$@
  139. }