LoginSignup
14
13

More than 1 year has passed since last update.

MAC 右クリックで新規ファイルを作成する

Last updated at Posted at 2020-04-11

初投稿です!
日頃ソースやPCを触って上手く出来たことや気になったことを残しておく備忘録的な用途で使用しています。
間違った箇所があればご指摘くださいm(_ _)m

この業界に入ってからMacbook Airを使い始めて思ったのが、
windowsでデフォルトで使用できた機能、右クリックで新規ファイルを作成するっていうのがmacでは出来ないということ。

ソースを書くときにいちいちターミナルのtouchコマンドを使用して新規ファイルを作成するのって面倒だなーと思ったので調べてみたらAutomatorの設定を変えることで右クリックで新規ファイルを作成することが出来るとのこと。

※追記
何も右クリックでファイルを作成しなくても色々やり方はあったなと気がつく。
ターミナルに入ってtouchコマンドでファイルを作成したり、vscodeで新規ファイルを作成したりとか。。。
知識0の状態で書いた記事は結構訂正や削除しなければいけないかなと思ったりする。

手順

①Automator.app起動
 command + スペースで spotlight検索を出しAutomatorを検索

②設定変更
 書類の種類を選択してくださいからクイックアクションを選び選択ボタンを押下
 ワークフローが受け取る現在の項目: ファイルまたはフォルダを選択
 アクションの中のユーティリティのAppleScriptを実行を右にドロップ
 AppleScriptを実行のテキストの中に下記コードをコピペ

property defaultFileName : "newTextFile"

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

set flagFileExists to true
set indexFile to ""

repeat while flagFileExists
    set indexFile to (indexFile + 1)
    tell application "Finder"
        set flagFileExists to (exists file (defaultFileName & indexFile & ".txt") in sourceFolder)
    end tell
end repeat

try
    tell me
        activate
        set newFileName to text returned of (display dialog "Enter new file name" default answer "" & defaultFileName & indexFile)
    end tell
    if newFileName is not equal to "" & defaultFileName & indexFile then
        set defaultFileName to newFileName
    end if
    set newFile to "" & sourceFolder & newFileName & ".txt"

    if not flagFileExists then
        set touchScript to "touch " & quoted form of (POSIX path of newFile)
        set openScript to "open " & quoted form of (POSIX path of newFile)          
        do shell script touchScript
        do shell script openScript
    else
        display dialog "File already exists"
    end if
end try
return input
end run

 コピペ後にハンマーボタンを押下してコンパイルする。→色が変わるのを確認

③設定を保存
 ここでは新規ファイル作成で保存
スクリーンショット .png

④実際に新規ファイルを作成
 ディレクトリの上にカーソルをあわせて右クリック
 新規ファイル作成があるはずなのでそこをクリックすると新規ファイルができている。
 ディレクトリ内には出来なかったのでディレクトリ内に新規ファイルを作成する方法をご存知の方は
 ご指摘いただければ幸いです。

macOS Catalina バージョン 10.15.4

参照
https://j-wort.hatenadiary.org/entries/2012/12/23

14
13
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
14
13