2024-09-01

Install uv

❯ curl -LsSf https://astral.sh/uv/install.sh | sh
downloading uv 0.4.1 aarch64-apple-darwin
installing to /Users/noriyo_tcp/.cargo/bin
  uv
  uvx
everything's installed!

To add $HOME/.cargo/bin to your PATH, either restart your shell or run:

    source $HOME/.cargo/env (sh, bash, zsh)
    source $HOME/.cargo/env.fish (fish)

❯ vi ~/.bashrc

# やっぱ一旦消した
rm ~/.cargo/bin/uv ~/.cargo/bin/uvx

homebrew で入れる

brew install uv

# こういうエラーが出た
unknown command "attestation" for "gh"

brew update
brew install uv

 ❯ uv --version
uv 0.4.1 (Homebrew 2024-08-30)

 noriyo_tcp@MacBook-Air  ~/MyPlayground 
 ❯ uv init kindle_ss
Initialized project `kindle-ss` at `/Users/noriyo_tcp/MyPlayground/kindle_ss`

 noriyo_tcp@MacBook-Air  ~/MyPlayground 
 ❯ cd kindle_ss

 noriyo_tcp@MacBook-Air  ~/M/kindle_ss 
 ❯ ls
README.md      hello.py       pyproject.toml

 noriyo_tcp@MacBook-Air  ~/M/kindle_ss 
 ❯ vi pyproject.toml

 noriyo_tcp@MacBook-Air  ~/M/kindle_ss 
 ❯ vi hello.py

 noriyo_tcp@MacBook-Air  ~/M/kindle_ss 
 ❯ uv python install
Searching for Python installations
Installed Python 3.12.5 in 3.51s
 + cpython-3.12.5-macos-x86_64-none
 ❯ uv python list --only-installed
cpython-3.12.5-macos-x86_64-none     /Users/noriyo_tcp/.local/share/uv/python/cpython-3.12.5-macos-x86_64-none/bin/python3 -> python3.12
cpython-3.11.4-macos-x86_64-none     /usr/local/opt/python@3.11/bin/python3.11 -> ../Frameworks/Python.framework/Versions/3.11/bin/python3.11
cpython-3.10.12-macos-x86_64-none    /usr/local/opt/python@3.10/bin/python3.10 -> ../Frameworks/Python.framework/Versions/3.10/bin/python3.10
cpython-3.9.17-macos-x86_64-none     /usr/local/opt/python@3.9/bin/python3.9 -> ../Frameworks/Python.framework/Versions/3.9/bin/python3.9
cpython-3.9.6-macos-x86_64-none      /Applications/Xcode.app/Contents/Developer/usr/bin/python3 -> ../../Library/Frameworks/Python3.framework/Versions/3.9/bin/python3
cpython-3.8.17-macos-x86_64-none     /usr/local/opt/python@3.8/bin/python3.8 -> ../Frameworks/Python.framework/Versions/3.8/bin/python3.8
 ❯ uv python pin 3.12.5
Pinned `.python-version` to `3.12.5`
 ❯ uv run hello.py
Using Python 3.12.5
Creating virtualenv at: .venv
Hello from kindle-ss!
 ❯ . .venv/bin/activate
(.venv)
# その後だとこうなる
 ❯ uv run hello.py
Hello from kindle-ss!
(.venv)

# install packages
 ❯ uv add pygetwindow pyautogui pillow pytesseract
Resolved 17 packages in 2.61s
   Built pygetwindow==0.0.9
   Built mouseinfo==0.1.3
   Built pyperclip==1.9.0
   Built pyscreeze==1.0.1
   Built pymsgbox==1.0.9
   Built pyrect==0.2.0
   Built pyautogui==0.9.54
   Built pytweening==1.2.0
Prepared 15 packages in 1.25s
Installed 15 packages in 7ms
 + mouseinfo==0.1.3
 + packaging==24.1
 + pillow==10.4.0
 + pyautogui==0.9.54
 + pygetwindow==0.0.9
 + pymsgbox==1.0.9
 + pyobjc-core==10.3.1
 + pyobjc-framework-cocoa==10.3.1
 + pyobjc-framework-quartz==10.3.1
 + pyperclip==1.9.0
 + pyrect==0.2.0
 + pyscreeze==1.0.1
 + pytesseract==0.3.13
 + pytweening==1.2.0
 + rubicon-objc==0.4.9
(.venv)
 ❯ uv run kindle_ss.py
Traceback (most recent call last):
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 51, in <module>
    main()
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 32, in main
    kindle_window = get_first_kindle_window()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 9, in get_first_kindle_window
    windows = gw.getWindowsWithTitle("Kindle")
              ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pygetwindow' has no attribute 'getWindowsWithTitle'

# これが必要らしい
uv add pyobjc
 ❯ uv run kindle_ss.py
Unable to find application named 'Kindle'

このように書き換えた

def activate_kindle():
    subprocess.run(['open', '-a', 'Amazon Kindle.app'])

 ❯ uv run kindle_ss.py
Traceback (most recent call last):
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/pytesseract/pytesseract.py", line 275, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/.local/share/uv/python/cpython-3.12.5-macos-x86_64-none/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Users/noriyo_tcp/.local/share/uv/python/cpython-3.12.5-macos-x86_64-none/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'tesseract'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 64, in <module>
    main()
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 55, in main
    text = extract_text_from_image(screenshot)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 33, in extract_text_from_image
    return pytesseract.image_to_string(image)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/pytesseract/pytesseract.py", line 486, in image_to_string
    return {
           ^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/pytesseract/pytesseract.py", line 489, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
                           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/pytesseract/pytesseract.py", line 352, in run_and_get_output
    run_tesseract(**kwargs)
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/pytesseract/pytesseract.py", line 280, in run_tesseract
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

tesseract をインストールしないといけないか
https://qiita.com/ryome/items/16fc42854fe93de78a23

brew install tesseract

なんか float のエラー

サブモニタで開いた状態なら問題ないが Macbook Pro の内蔵モニタで開いているとダメだ

 ❯ uv run kindle_ss.py
Traceback (most recent call last):
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 64, in <module>
    main()
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 54, in main
    screenshot = capture_kindle_screenshot(kindle_window, file_name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/kindle_ss.py", line 28, in capture_kindle_screenshot
    screenshot = pyautogui.screenshot(region=(x, y, width, height))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/pyscreeze/__init__.py", line 579, in _screenshot_osx
    im = ImageGrab.grab(bbox=(region[0], region[1], region[2] + region[0], region[3] + region[1]))
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/PIL/ImageGrab.py", line 49, in grab
    im_resized = im.resize((right - left, bottom - top))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/PIL/Image.py", line 2305, in resize
    im = im.resize(size, resample, box)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/noriyo_tcp/MyPlayground/kindle_ss/.venv/lib/python3.12/site-packages/PIL/Image.py", line 2328, in resize
    return self._new(self.im.resize(size, resample, box))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer

float を int にキャストすると大丈夫だった。しかしフルスクリーンで表示しているとダメ。これはまあ仕方ない


抽出した文字が文字化けしている

brew install tesseract-lang