# [メタ情報] # 識別子: マイライブラリ_画面入力_exe # システム名: マイライブラリ_画面入力 # 技術種別: Misc # 機能名: Misc # 使用言語: AppleScript Python # 状態: 実行用 env終了 # [/メタ情報] 要約:Finderで選択したファイルを処理し、対応するDropbox共有リンクを自動取得して送信スクリプトに渡すAppleScript。ファイルのパスや拡張子を取得し、日本語などを除去してDropbox形式のファイル名に整形。Python仮想環境のget_dropbox_link.pyでリンクを生成し、ファイル名の整合性を確認後、send_request.pyでリンク情報を送信。M1・M2用でパス構成が異なる。 ▫️画面入出力 _マイライブラリ登録 AppleScript ◆M1 Mac用 ``` on run {input, parameters} -- 入力がない場合のエラー処理 if input is {} then display alert "ファイルまたはフォルダが選択されていません。" message "Finderで処理したいファイルを選択してください。" return end if -- ファイル情報の取得 set filePath to POSIX path of item 1 of input if filePath contains "/Users/XXXXXX/" then set filePath to do shell script "echo " & quoted form of filePath & " | sed 's#^/Users/XXXXXX/#/Volumes/NO3_SSD/#'" end if set fileName to (do shell script "basename " & quoted form of filePath) set extn to do shell script "echo " & quoted form of fileName & " | sed -E 's/.*\\.(.*)$/\\1/'" -- 日本語などを除去してDropboxのファイル名形式に近づける set cutjp_fileName to do shell script "echo " & quoted form of fileName & " | sed -E 's/[一-龠ぁ-ゔァ-ヴーa-zA-Z0-9ア-ン゙゚]+/-/g; s/-([.][^.]+)$/\\1/; s/^-+(.*)$/\\1/; s/--+/-/g'" -- ====== 1. 環境設定(金庫 .env からの動的ロード & 環境差の自動解決) ====== set currentUser to short user name of (system info) set envPath to "/Users/" & currentUser & "/python_scripts/.env" set pythonPath to "" set getLinkScript to "" set sendScript to "" try set pythonPath to do shell script "grep 'VENV_PYTHON_PATH' " & quoted form of envPath & " | sed -E 's/^VENV_PYTHON_PATH=//' | sed -E 's/^[\\\"\\x27]//;s/[\\\"\\x27]$//'" set getLinkScript to do shell script "grep 'GET_DROPBOX_LINK_PATH' " & quoted form of envPath & " | sed -E 's/^GET_DROPBOX_LINK_PATH=//' | sed -E 's/^[\\\"\\x27]//;s/[\\\"\\x27]$//'" set sendScript to do shell script "grep 'SEND_REQUEST_PATH' " & quoted form of envPath & " | sed -E 's/^SEND_REQUEST_PATH=//' | sed -E 's/^[\\\"\\x27]//;s/[\\\"\\x27]$//'" on error display alert "【環境エラー】金庫 .env ファイルの読み込みに失敗しました。" message "ファイルが存在するか、または読み取り権限を確認してください: " & envPath return end try -- 安全な起動時チェック(ガードロジック:金庫の中身が空の場合は自律停止) if pythonPath is "" or getLinkScript is "" or sendScript is "" then display alert "【環境エラー】必要な鍵が .env に設定されていません。" message "VENV_PYTHON_PATH, GET_DROPBOX_LINK_PATH, SEND_REQUEST_PATH が .env 内に正しく設定されていることを確認してください。" return end if -- Python(仮想環境)でDropboxリンクを取得 set dropboxLink to do shell script pythonPath & " " & quoted form of getLinkScript & " " & quoted form of filePath -- 整合性チェック:Dropboxリンクにcutjp_fileNameが含まれているか if dropboxLink is "" then display notification "再生選択1番は空欄でマイライブラリへ登録します。" with title "マイライブラリ登録" else if dropboxLink does not contain cutjp_fileName then display dialog "Dropboxリンクと選択したファイルの名前が一致しない可能性があります。 推定ファイル名: " & cutjp_fileName & return & "このまま続行しますか?" buttons {"キャンセル", "続行"} default button "キャンセル" if button returned of result is "キャンセル" then return end if -- Python(仮想環境)で送信スクリプトを実行 set cmd to pythonPath & " " & quoted form of sendScript & " " & quoted form of dropboxLink & " " & quoted form of filePath & " " & quoted form of fileName & " " & quoted form of extn set response to do shell script cmd -- 結果を表示 display dialog response end run ``` ◆M2 Mac用 ``` -- ◆M2 Mac用 AppleScript(Dropboxリンクを自動取得するバージョン) on run {input, parameters} -- 入力がない場合のエラー処理 if input is {} then display alert "ファイルまたはフォルダが選択されていません。" message "Finderで処理したいファイルを選択してください。" return end if -- ファイル情報の取得 set filePath to POSIX path of item 1 of input if filePath contains "/Users/XXXXXX/" then set filePath to do shell script "echo " & quoted form of filePath & " | sed 's#^/Users/XXXXXX/#/Volumes/NO3_SSD/#'" end if set fileName to (do shell script "basename " & quoted form of filePath) set extn to do shell script "echo " & quoted form of fileName & " | sed -E 's/.*\\.(.*)$/\\1/'" -- 日本語などを除去してDropboxのファイル名形式に近づける set cutjp_fileName to do shell script "echo " & quoted form of fileName & " | sed -E 's/[一-龠ぁ-ゔァ-ヴーa-zA-Z0-9ア-ン゙゚]+/-/g; s/-([.][^.]+)$/\\1/; s/^-+(.*)$/\\1/; s/--+/-/g'" -- ====== 1. 環境設定(金庫 .env からの動的ロード & 環境差の自動解決) ====== set currentUser to short user name of (system info) set envPath to "/Users/" & currentUser & "/python_scripts/.env" set pythonPath to "" set pythonScriptPath to "" set sendRequestPath to "" try set pythonPath to do shell script "grep 'VENV_PYTHON_PATH' " & quoted form of envPath & " | sed -E 's/^VENV_PYTHON_PATH=//' | sed -E 's/^[\\\"\\x27]//;s/[\\\"\\x27]$//'" set pythonScriptPath to do shell script "grep 'GET_DROPBOX_LINK_PATH' " & quoted form of envPath & " | sed -E 's/^GET_DROPBOX_LINK_PATH=//' | sed -E 's/^[\\\"\\x27]//;s/[\\\"\\x27]$//'" set sendRequestPath to do shell script "grep 'SEND_REQUEST_PATH' " & quoted form of envPath & " | sed -E 's/^SEND_REQUEST_PATH=//' | sed -E 's/^[\\\"\\x27]//;s/[\\\"\\x27]$//'" on error display alert "【環境エラー】金庫 .env ファイルの読み込みに失敗しました。" message "パスを確認してください: " & envPath & return & "エラー詳細: " & errMsg return end try -- 安全な起動時チェック(ガードロジック:金庫の中身が空の場合は自律停止) if pythonPath is "" or pythonScriptPath is "" or sendRequestPath is "" then display alert "【環境エラー】必要な鍵が .env に設定されていません。" message "VENV_PYTHON_PATH, GET_DROPBOX_LINK_PATH, SEND_REQUEST_PATH が .env 内に正しく設定されていることを確認してください。" return end if -- DropboxリンクをPython経由で取得(クリップボードは使用しない) set dropboxLink to do shell script pythonPath & " " & quoted form of pythonScriptPath & " " & quoted form of filePath -- 整合性チェック:Dropboxリンクにcutjp_fileNameが含まれているか if dropboxLink is "" then display notification "再生選択1番は空欄でマイライブラリへ登録します。" with title "マイライブラリ登録" else if dropboxLink does not contain cutjp_fileName then display dialog "Dropboxリンクと選択したファイルの名前が一致しない可能性があります。 推定ファイル名: " & cutjp_fileName & return & "このまま続行しますか?" buttons {"キャンセル", "続行"} default button "キャンセル" if button returned of result is "キャンセル" then return end if -- Python スクリプトの実行(send_request.py) set cmd to pythonPath & " " & quoted form of sendRequestPath & " " & quoted form of dropboxLink & " " & quoted form of filePath & " " & quoted form of fileName & " " & quoted form of extn set response to do shell script cmd -- Python からのレスポンスを表示 display dialog response end run ```