こんにちは。もぐも(@mogmo1012)です。
最近macを買いました。20万…高かった…🥶
それはさておき,最新版のmacOSには,Windowsのエクスプローラー上でファイルを新規作成する機能が無いみたい。解決策を探していたら,AppleScriptを使えば条件付きで実現可能であることがわかったのでメモしておきます。✍️
スクリプトだけ見たい方は2-5節を読んでね。
txtファイルのみ対応する場合
こちらのサイト様が参考になりました。
とても丁寧に解説されています。
wordpress.ideacompo.com
拡張子対応版に修正
上記のコードでは拡張子が.txtしか対応していなかったので,拡張子も入力できるようコードを修正してみました。
2. ワークフローファイルを新規作成
メニューから「ファイル」>「新規」を選択してファイルを作成します。
3. 種類をクイックアクションにする
「クイックアクション」を選択して「選択」をクリックします。
4. AppleScriptを実行する設定にします。
「ワークフローが受け取る現在の項目」:「ファイルまたはフォルダ」に設定
「アクション」:「ユーティリティ」>「AppleScriptを実行」をドラッグ&ドロップ
5. スクリプトを記述
仕様
- "."がない文字列の場合は,拡張子を".txt"としてファイルを作成する。
- 文字列に"."が含まれる場合は拡張子ありのファイル名が入力されたと認識して処理を行う。
- はじめに出現する"."以降の文字を拡張子とする
- "."から始まるファイル名は作成できない
- ":"を含むファイル名は作成できない
処理の流れ
ざっくりと処理の流れを説明すると...
- デフォルトファイル名"noname"でファイル名入力画面を表示する
- 入力したファイル名の検証する
- 拡張子なしの入力と判断した場合,入力文字列+".txt"をファイル名とする
- 拡張子ありの入力と判断した場合,そのまま処理を続ける
- 重複ファイルがあるかどうか検証
- ある場合はファイル名と拡張子の間にインデックス番号を追加
- ファイルを作成
- 重複によるファイル名変更を行った場合は,通知画面を表示する
スクリプト全文
(* reference: http://wordpress.ideacompo.com/?p=10886 *) property defaultFileName : "noname" on run {input, parameters} try tell application "Finder" to set the sourceFolder to (folder of the front window) as alias on error -- no open folder windows set the sourceFolder to path to desktop folder as alias end try try -- ファイル名入力画面を表示 set flagErrorInput to true repeat while flagErrorInput tell me activate set newFileName to text returned of (display dialog "Enter new file name" default answer "" & defaultFileName) -- ファイルの検証 set dotPosition to the offset of "." in newFileName set colonPosition to the offset of ":" in newFileName if (dotPosition = 1) then display alert "\".\"で始まる名前はシステムだけが使用できます。別の名前を指定してください。" as critical else if colonPosition is not equal to 0 then display alert "\":\"は使用できません。別の名前を使用してください。" as critical else set flagErrorInput to false end if end tell end repeat -- 入力したファイル名の検証 set stringLength to the length of newFileName set dotPosition to the offset of "." in newFileName -- ファイル名と拡張子を分割 if (dotPosition ≤ 1) or (dotPosition ≥ stringLength) then -- 拡張子がない入力文字列だと判断した場合 set fileName to newFileName set extension to ".txt" else -- 拡張子ありの文字列と判断した場合 set fileName to characters 1 thru (dotPosition - 1) of newFileName as text set extension to characters dotPosition thru stringLength of newFileName as text end if -- 重複ファイル名があるかどうか確認 set flagFileExists to true set indexFile to "" repeat while flagFileExists tell application "Finder" set flagFileExists to (exists file (fileName & indexFile & extension) in sourceFolder) end tell if not flagFileExists then -- なければファイルパスを確定して処理を抜ける set newFile to "" & sourceFolder & fileName & indexFile & extension exit repeat end if set indexFile to (indexFile + 1) end repeat -- ファイルを作成する set touchScript to "touch " & quoted form of (POSIX path of newFile) do shell script touchScript (* memo: ファイル作成後に開く場合はコメント解除してください -- ファイルを開く set openScript to "open " & quoted form of (POSIX path of newFile) do shell script openScript *) -- 重複のためファイル名を変更した場合は通知 if indexFile is not equal to "" then display alert "Renamed file name \"" & fileName & extension & "\" to \"" & fileName & indexFile & extension & "\" because it already existed" as informational end if end try return input end run
既知の問題
- 空白文字を含む拡張子を入力した場合,ファイルが生成されない。かつ通知も出てこない。
6. デバッグボタンを押してスクリプトを保存
赤枠のデバッグボタンをクリックしてスクリプトを確定し,エラーメッセージが出ないことを確認してください。正常なコードとしてデバックが通れば,メッセージは何も出てきません。
7. ファイル名を指定して保存する
コンテキストメニューに表示されるラベルにもなるので,わかりやすいファイル名にしましょう。