2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MacのQuick ActionsでPythonファイルのディレクトリに移動して相対パスで実行する。

Last updated at Posted at 2023-12-30

MacのQuick ActionsでPythonスクリプトを素早く実行する方法

Macユーザーの皆さん、Finderから直接Pythonスクリプトを実行したいと思ったことはありませんか?Quick Actionsを使えば、これが可能になります。この記事では、Finderで選択したPythonファイルのディレクトリに素早く移動し、ターミナルからスクリプトを実行するためのQuick Actionの作成方法をご紹介します。

Quick Actionsとは何か?

Quick Actionsは、macOSのFinderでファイルやフォルダーに対して簡単にアクセスできるカスタムワークフローです。これにより、スクリプト実行や画像のリサイズなど、繰り返し行う作業をワンクリックで実行できるようになります。

必要なツール

  • Macコンピューター
  • macOSのAutomatorアプリケーション
  • 基本的なAppleScriptの知識

Quick Actionの作成

Step 1: Automatorを開く

アプリケーションフォルダからAutomatorを開き、新規ドキュメントを選択します。

Step 2: Quick Actionを選択

新規作成ダイアログでQuick Actionを選びます。

Step 3: ワークフローの設定

ワークフローは、Finderで選択したファイルを受け取ります。ワークフローが受け取る項目ファイルまたはフォルダFinderでに設定します。

Step 4: AppleScriptの追加

左側のライブラリからAppleScriptを実行アクションをワークフローにドラッグします。そして、以下のスクリプトを入力します。

on run {input, parameters}
	-- 入力からファイルパスを取得
	set filePath to POSIX path of first item of input
	-- ファイル名の抽出
	set fileName to my extractFileName(filePath)
	-- ディレクトリパスの抽出
	set dirPath to my extractDirPath(filePath)
	-- Terminalを開いてディレクトリに移動し、Pythonスクリプトを実行
	set pythonScript to "python " & quoted form of fileName
	set terminalScript to "cd " & quoted form of dirPath & "; " & pythonScript
	tell application "Terminal"
		activate
		do script terminalScript
	end tell
	-- ここに他のコードを追加
end run

on extractFileName(filePath)
	set fileName to ""
	set filePathCharacters to characters of filePath
	
	repeat with i from (count of filePathCharacters) to 1 by -1
		if item i of filePathCharacters is "/" then
			set fileName to text (i + 1) thru -1 of filePath
			exit repeat
		end if
	end repeat
	
	return fileName
end extractFileName

on extractDirPath(filePath)
	set dirPath to text 1 thru -((count of characters in my extractFileName(filePath)) + 1) of filePath
	return dirPath
end extractDirPath

Step 5: Quick Actionの保存

上部の保存ボタンをクリックして、Quick Actionに名前を付けて保存します。

実際の使用例

Quick Actionを保存した後、FinderでPythonファイルを選択し、右クリックメニューから新しく作成したQuick Actionを選択します。これにより、選択したPythonスクリプトがターミナルで実行されます。

このAppleScriptのコードは、ファイルパスを受け取り、そのファイルパスからファイル名とディレクトリパスを抽出して、指定されたPythonスクリプトをTerminalで実行するためのものです。以下は、このスクリプトの各部分の詳細な説明です。

コードの詳細説明

スクリプトの全体的な流れ

  1. 入力の処理: スクリプトは、外部からの入力(通常はファイルパス)を受け取ります。
  2. ファイル名の抽出: 入力されたパスからファイル名を抽出します。
  3. ディレクトリパスの抽出: 同じく入力されたパスからディレクトリパスを抽出します。
  4. TerminalでのPythonスクリプトの実行: 抽出したディレクトリに移動し、Pythonスクリプトを実行します。

各部分の詳細

  • on run {input, parameters}: この部分は、スクリプトが開始するときに実行されます。input は外部からの入力(ここではファイルパス)を格納します。

  • set filePath to POSIX path of first item of input: 入力されたファイルパスをPOSIX形式(Unixスタイル)に変換します。

  • set fileName to my extractFileName(filePath): extractFileName 関数を呼び出して、入力されたパスからファイル名を抽出します。

  • set dirPath to my extractDirPath(filePath): extractDirPath 関数を使用して、入力されたパスからディレクトリパスを抽出します。

  • tell application "Terminal": AppleScriptにTerminalアプリケーションを操作させます。

    • activate: Terminalをアクティブ(フォーカスされた状態)にします。
    • do script terminalScript: 抽出したディレクトリに移動し、Pythonスクリプトを実行するコマンドをTerminalで実行します。
  • on extractFileName(filePath): ファイルパスからファイル名を抽出する関数です。

  • on extractDirPath(filePath): ファイルパスからディレクトリパスを抽出する関数です。

トラブルシューティング

もしQuick Actionが期待通りに動作しない場合は、以下の点を確認してください:

  • Pythonが正しくインストールされているか
  • スクリプトのパーミッションが実行可能になっているか
  • AppleScriptの構文に誤りがないか

結論

このQuick Actionは、Python開発者の日常を大きく効率化します。煩わしいディレクトリの移動やコマンド入力を省略し、作業に集中できるようになります。このガイドがあなたの開発プロセスをより快適にする一助となれば幸いです。

参考リンク

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?