サポンテ 勉強ノート

サポンテの勉強ノート・読書メモなどを晒します。

Spotlight を利用して素早く新規ファイル作成【macOS】【AppleScript】

はじめに

 「Finder で『この場所に新しいテキストファイル』を作成したい」際に、クリックひとつでテキストファイルが作成できるよう、かつて AppleScript で作成したアプリケーションを、Finder ウィンドウのツールバーに登録していました。メモファイルをたくさん作るためです。

 しかし最近は、同じように .md ファイル(Markdown ファイル)を素早く作りたいケースが増えてきました。でもツールバーのボタンを、むやみに増やしたくありません。

 ツールバーのアプリケーションアイコンをクリックした際に、新規作成したファイルの候補を選択するような UI を最初に考えたのですが、それよりは Spotlight で「new_text」とか「new_md」とか、いやいっそ「nt」とか「nm」とかタイプするだけでテキストファイルや .md ファイルが作成できるようにしたい。

AppleScript でできる

 テキストファイル(プレーンテキストファイル)も .md のマークダウンファイルも、基本的には touch コマンドで作れます。最初に作った AppleScript を編集して、これらのファイルを作成するアプリケーションを作ってみます。

仕様

  1. 新規ファイルを作成する場所は、Finder で今開いている場所。なければデスクトップ。
  2. ファイル名は、デフォルトで日付文字列 + 拡張子。
  3. ファイル作成後は、すぐにファイル名の変更ができるように、Finder で選択状態にする。

使い方

 上記の通り、Spotlight を起動したら「nm」とタイプしただけでアプリケーションを起動できるようにします。これは、ファイル名を工夫するだけで OK です。

 初回のみ、以下の警告が表示されます。

実際のスクリプト

 以下が、実際のスクリプトです。これを「nm新規マークダウン書類newmarkdown.app」のような名前で「アプリケーション」として保存します。保存場所は、Spotlight でインデックスが作成される範囲ならどこでも良いですが、サポンテの場合は「~/Application/」の下にしました。

set fullPath to GetPosixPathOnFinderActiveDir() & GetDateString() & ".md"
set quoted to quoted form of fullPath
do shell script "touch " & quoted
delay 1
SelectFinderItem(fullPath)

(*
POSIX パスで指定したファイルを Finder で選択状態にする。
*)
on SelectFinderItem(targetPath)
    set targetPath to POSIX file targetPath
    tell application "Finder"
        try
            select item targetPath
        on error error_message number error_number
            beep
            display dialog error_message with icon caution buttons {"OK"} default button 1 giving up after 120
        end try
        activate
    end tell
end SelectFinderItem

(*
日付文字列を取得する。
参考: http://wakabamac.blog95.fc2.com/
*)
on GetDateString()
    set today to current date
    
    -- Year
    set theYear to ((year of today) as text) as text
    
    -- Month
    set theMonth to ((month of today) as number) as text
    if length of theMonth is 1 then set theMonth to "0" & theMonth
    
    -- Day
    set theDay to (day of today) as text
    if length of theDay is 1 then set theDay to "0" & theDay
    
    return theYear & "-" & theMonth & "-" & theDay
end GetDateString

(*
Finder でアクティブになっている POSIX パスを返す。
Finder ウィンドウが開いていれば、そのディレクトリを、開いていなければデスクトップを返す。
 *)
on GetPosixPathOnFinderActiveDir()
    tell application "Finder"
        set f to GetFolderOnFinderActiveDir() of me
        if (f = missing value) then return ""
        set a to f as alias
        return POSIX path of a
    end tell
end GetPosixPathOnFinderActiveDir

(*
Finder でアクティブになっているフォルダを返す。
Finder ウィンドウが開いていれば、そのフォルダを、開いていなければデスクトップを返す。
*)
on GetFolderOnFinderActiveDir()
    tell application "Finder"
        try
            if Finder window 1 exists then
                return target of front window
            else
                return desktop
            end if
        on error error_message number error_number
            beep
            display dialog error_message with icon caution buttons {"OK"} default button 1 giving up after 120
            return missing value
        end try
    end tell
end GetFolderOnFinderActiveDir

開発環境

ProductName: Mac OS X
ProductVersion: 10.15.7
AppleScript version: 2.7
zsh 5.7.1 (x86_64-apple-darwin19.0)

さいごに

 嗚呼、Spotlight の強力さよ。

 touch コマンドで作れない複雑な雛形は、~/Library/Templates/ などに保管しておいて、cp コマンドでコピーするようにすれば良いでしょう。