Guest User

archivarius external file associations

a guest
Nov 17th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.28 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. function wait_for_window_to_activate() {
  5.   # first parameter  - window title or part of the window title
  6.   window_title="$1"
  7.   time_sec=900
  8.   while [[ 0 -ne $time_sec ]]; do
  9.     echo "$time_sec, waiting... to activate"
  10.     time_sec=$[$time_sec-1]
  11.     xdotool search --onlyvisible --name "$window_title" windowactivate
  12.     if [[ $? == 0 ]]; then
  13.       sleep 0.5
  14.       window_name=$(xdotool getwindowfocus getwindowname)
  15.       if echo $window_name | grep -q "$window_title" ; then
  16.         echo "window active "$window_name
  17.         break
  18.       fi
  19.     fi
  20.     sleep 0.5
  21.   done
  22. }
  23.  
  24.  
  25. function wait_for_window_to_close() {
  26.   # first parameter  - window title or part of the window title
  27.   window_title="$1"
  28.   time_sec=900
  29.   while [[ 0 -ne $time_sec ]]; do
  30.     echo "$time_sec, waiting... to close"
  31.     sleep 1
  32.     time_sec=$[$time_sec-1]
  33.     xdotool search --onlyvisible --name "$window_title" windowactivate
  34.     if ! [[ $? == 0 ]]; then
  35.       echo "window closed"
  36.       break
  37.     fi
  38.   done
  39. }
  40.  
  41.  
  42. function type_enter() {
  43.   # first parameter  - window title or part of the window title
  44.   string_to_write="$1"
  45.   xdotool type $string_to_write
  46.   sleep 0.5
  47.   xdotool key Return
  48. }
  49.  
  50.  
  51. function mousemove_mouseclick() {
  52.   window_title="$1"
  53.   relative_coord_x="$2"
  54.   relative_coord_y="$3"
  55.   mouse_click_type="$4"
  56.   xdotool search --sync --onlyvisible --name $window_title windowactivate
  57.   sleep 0.5
  58.   window_coord=$(xdotool search --sync --onlyvisible --name "$window_title" getwindowgeometry --shell)
  59.   eval $window_coord
  60.   # let X+=80
  61.   # let Y+=116
  62.   let X+=relative_coord_x
  63.   let Y+=relative_coord_y
  64.   xdotool mousemove $X $Y click $mouse_click_type
  65. }
  66.  
  67.  
  68. function repeat_send_key() {
  69.   window_title="$1"
  70.   key="$2"
  71.   repeat="${3:-1}"
  72.   xdotool search --onlyvisible --name "$window_title" windowactivate
  73.   if [[ $? == 0 ]]; then
  74.     for (( i=1; i <= repeat; i++ ))
  75.     do
  76.      xdotool key $key
  77.      sleep 0.1
  78.     done
  79.   fi
  80. }
  81.  
  82.  
  83. function tranquility_save() {
  84.   # Tranquility Reader addon
  85.   # Tranquility Reader improves the readability of web articles by removing unnecessary elements
  86.   wait_for_window_to_activate "Dissenter"
  87.   # echo $(date '+%Y %b %d %H:%M')
  88.   mousemove_mouseclick "Dissenter" 1412 74 1
  89.   sleep 5
  90.   # first type
  91.   win_title=$(echo $(xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) _NET_WM_VISIBLE_NAME) | cut -d '=' -f 2 | sed s/' "'//g | sed s/' - Dissenter"'//g)
  92.   echo $win_title
  93.   # correct title
  94.   escape=("[" "]" "~" "!" "{" "}" "(" ")" "$" "@" "<" ">" ":" ";" "," '`' "%" "+" "=" '"' "|" "?" '*' "&" "'")
  95.   for e in "${escape[@]}"
  96.   do
  97.     win_title=${win_title//[$e]/}
  98.   done
  99.   win_title=$(echo $win_title | sed -e 's/[[:space:]]\+/ /g')
  100.   win_title=${win_title//" "/"_"}".txt"
  101.   echo $win_title
  102.   # write to clipbaord
  103.   xdotool key ctrl+a
  104.   sleep 0.5
  105.   xdotool key ctrl+c
  106.   sleep 0.5
  107.   clip_read=$(xsel -b)
  108.   echo "$clip_read" >> /media/ramdisk/recordings/$win_title
  109.   echo "done"
  110.   # save page SingleFile second type
  111.   # wait_for_window_to_activate "Dissenter"
  112.   # xdotool key ctrl+shift+y
  113.   # win_title="Save File"
  114.   # wait_for_window_to_activate "$win_title"
  115.   # sleep 0.5
  116.   # xdotool keydown ctrl && sleep 0.2 && xdotool key l && xdotool keyup ctrl
  117.   # sleep 0.5
  118.   # xdotool keydown ctrl && sleep 0.2 && xdotool key Home && xdotool keyup ctrl
  119.   # sleep 0.5
  120.   # type_enter $(echo ~/Downloads/reading/)
  121.   # wait_for_window_to_close "$win_title"
  122. }
  123.  
  124.  
  125. function open_ebook() {
  126.   pdfxchange_dir_loc=$(echo ~/progs/pdfxchange)
  127.   pdfxchange_run_loc="/media/ramdisk/pdfxchange/PDFXEdit.x64.exe"
  128.   ramdisk_loc="/media/ramdisk"
  129.   win_title="Archivarius 3000"
  130.   wait_for_window_to_activate "$win_title"
  131.   echo -n "" | xclip -selection clipboard
  132.   repeat_send_key "$win_title" "Shift+F10"
  133.   sleep 0.5
  134.   repeat_send_key "$win_title" "Up" 3
  135.   repeat_send_key "$win_title" "Right"
  136.   repeat_send_key "$win_title" "Down"
  137.   repeat_send_key "$win_title" "Return"
  138.   clip_read=$(xsel -b)
  139.   if [[ ! $clip_read == "" ]]; then
  140.     first_trim=${clip_read#*$'\n'}
  141.     result=$(echo $clip_read | grep -Ei '.epub')
  142.     if [[ -n $result ]]; then
  143.       echo "epub detected"
  144.       file_name=${first_trim##*"Z:"}
  145.       file_name=${file_name%".epub"*}".epub"
  146.       file_loc=${file_name//\\//}
  147.     else
  148.       echo "other formats"
  149.       file_name=${first_trim%"%"*}
  150.       file_name=$(echo "$file_name" | sed 's/[[:blank:]]/ /g' | awk '{$1=$1};1')
  151.       file_name=${file_name%" "*}
  152.       second_trim=${first_trim#*%}
  153.       sufix=${second_trim##*\\}
  154.       file_folder=${second_trim%"$sufix"*}
  155.       file_folder=${file_folder##*"Z:"}
  156.       file_folder=${file_folder//\\//}
  157.       file_loc=$file_folder$file_name
  158.     fi
  159.     escape=("[" "]" "~" "!" "{" "}" "(" ")" "$" "@" "<" ">" ":" ";" "," '`' "%" "+" "=" '"' "|" "?" '*' "&" " " "'")
  160.     for e in "${escape[@]}"
  161.     do
  162.       file_loc=${file_loc//[$e]/\\$e}
  163.     done
  164.     file_loc=${file_loc//"\\'"/"'\"\'\"'"}
  165.     out_cmd_check=$(echo $file_loc | grep -Ei '.pdf')
  166.     if [[ -n $out_cmd_check ]]; then
  167.       pdf_cmd=$(echo exec wine "$pdfxchange_run_loc" "z:/$file_loc")
  168.       if [ -f "$pdfxchange_run_loc" ]; then
  169.         echo "skipping" > /dev/null
  170.       else
  171.         echo "copying"
  172.         cp -r "$pdfxchange_dir_loc" "$ramdisk_loc"
  173.       fi
  174.       cmd=$(echo "lxterminal -e 'bash -c "'"'"$pdf_cmd"'"'"'")
  175.       bash -c "$cmd" 2>/dev/null &
  176.       win_title="PDF-XChange Editor"
  177.       wait_for_window_to_activate "$win_title"
  178.       sleep 1
  179.       xdotool getwindowfocus windowmove 0 35
  180.       xdotool getactivewindow windowsize 1814 1040
  181.     else
  182.       cmd=$(echo "lxterminal -e 'bash -c "'"ebook-viewer '"$file_loc"'"'"'")
  183.       bash -c "$cmd" 2>/dev/null &
  184.       win_title="E-book viewer"
  185.       wait_for_window_to_activate "$win_title"
  186.       sleep 1
  187.       xdotool getwindowfocus windowmove 0 64
  188.       xdotool getactivewindow windowsize 1814 1013
  189.     fi
  190.   fi
  191. }
  192.  
  193.  
  194. # main exec code:
  195.  
  196. if echo $(xdotool getwindowfocus getwindowname) | grep -q "Archivarius 3000" ; then
  197.   open_ebook
  198. elif echo $(xdotool getwindowfocus getwindowname) | grep -q "Dissenter" ; then
  199.   tranquility_save
  200. fi
  201.  
  202. # prameters of interest:
  203.   # pdfxchange_dir_loc=$(echo ~/progs/pdfxchange)
  204.   # pdfxchange_run_loc="/media/ramdisk/pdfxchange/PDFXEdit.x64.exe"
  205.   # /media/ramdisk/recordings
  206.   # /media/ramdisk
Add Comment
Please, Sign In to add comment