フォルダの作成と削除

フォルダの作成と削除

はじめに

AppleScriptは、macOSでの自動化やアプリケーション操作を簡単に実現するスクリプト言語です。フォルダの作成と削除は、ファイル管理の基本的な操作であり、AppleScriptを用いることでこれらの操作を効率よく自動化できます。この記事では、AppleScriptを使用してフォルダを作成し、削除する方法を詳しく解説します。

フォルダの作成

フォルダを作成する方法

AppleScriptを使用して、Finderで新しいフォルダを作成することができます。以下のスクリプトは、指定したパスに新しいフォルダを作成する例です。


set newFolderName to "MyNewFolder"
set parentFolderPath to path to desktop folder as string

tell application "Finder"
    if not (exists folder (parentFolderPath & newFolderName)) then
        make new folder at desktop with properties {name:newFolderName}
        display dialog "フォルダを作成しました: " & parentFolderPath & newFolderName
    else
        display dialog "フォルダはすでに存在します: " & parentFolderPath & newFolderName
    end if
end tell
    

スクリプトの説明

  • set newFolderName to "MyNewFolder": 作成するフォルダの名前を指定します。
  • set parentFolderPath to path to desktop folder as string: フォルダを作成する親フォルダのパスを指定します。
  • tell application "Finder": Finderアプリケーションを操作するためのブロックを開始します。
  • if not (exists folder (parentFolderPath & newFolderName)) then: フォルダが存在しない場合に新しいフォルダを作成します。
  • make new folder at desktop with properties {name:newFolderName}: デスクトップ上に新しいフォルダを作成します。
  • display dialog "フォルダを作成しました: " & parentFolderPath & newFolderName: 作成されたフォルダの情報をダイアログボックスで表示します。
  • display dialog "フォルダはすでに存在します: " & parentFolderPath & newFolderName: 既にフォルダが存在する場合のメッセージを表示します。

フォルダの削除

フォルダを削除する方法

AppleScriptを使用して、Finderでフォルダを削除することができます。以下のスクリプトは、指定したパスのフォルダを削除する例です。


set folderToDelete to "MyNewFolder"
set parentFolderPath to path to desktop folder as string

tell application "Finder"
    if exists folder (parentFolderPath & folderToDelete) then
        delete folder (parentFolderPath & folderToDelete)
        display dialog "フォルダを削除しました: " & parentFolderPath & folderToDelete
    else
        display dialog "フォルダが見つかりません: " & parentFolderPath & folderToDelete
    end if
end tell
    

スクリプトの説明

  • set folderToDelete to "MyNewFolder": 削除するフォルダの名前を指定します。
  • set parentFolderPath to path to desktop folder as string: フォルダが存在する親フォルダのパスを指定します。
  • tell application "Finder": Finderアプリケーションを操作するためのブロックを開始します。
  • if exists folder (parentFolderPath & folderToDelete) then: 指定したフォルダが存在する場合に削除を行います。
  • delete folder (parentFolderPath & folderToDelete): 指定したフォルダを削除します。
  • display dialog "フォルダを削除しました: " & parentFolderPath & folderToDelete: 削除されたフォルダの情報をダイアログボックスで表示します。
  • display dialog "フォルダが見つかりません: " & parentFolderPath & folderToDelete: 指定したフォルダが存在しない場合のメッセージを表示します。

応用例:サブフォルダの作成と削除

サブフォルダを作成する

次に、親フォルダ内に複数のサブフォルダを作成する方法を示します。


set parentFolderName to "MainFolder"
set subfolders to {"SubFolder1", "SubFolder2", "SubFolder3"}
set parentFolderPath to path to desktop folder as string

tell application "Finder"
    if not (exists folder (parentFolderPath & parentFolderName)) then
        make new folder at desktop with properties {name:parentFolderName}
    end if
    repeat with subfolder in subfolders
        if not (exists folder (parentFolderPath & parentFolderName & ":" & subfolder)) then
            make new folder at folder (parentFolderPath & parentFolderName) with properties {name:subfolder}
        end if
    end repeat
end tell

display dialog "サブフォルダを作成しました: " & parentFolderPath & parentFolderName
    

スクリプトの説明

  • set parentFolderName to "MainFolder": 親フォルダの名前を指定します。
  • set subfolders to {"SubFolder1", "SubFolder2", "SubFolder3"}: 作成するサブフォルダの名前をリストで指定します。
  • set parentFolderPath to path to desktop folder as string: 親フォルダのパスを指定します。
  • tell application "Finder": Finderアプリケーションを操作するためのブロックを開始します。
  • if not (exists folder (parentFolderPath & parentFolderName)) then: 親フォルダが存在しない場合にフォルダを作成します。
  • make new folder at desktop with properties {name:parentFolderName}: デスクトップ上に親フォルダを作成します。
  • repeat with subfolder in subfolders: 各サブフォルダを繰り返し処理します。
  • if not (exists folder (parentFolderPath & parentFolderName & ":" & subfolder)) then: サブフォルダが存在しない場合に作成します。
  • make new folder at folder (parentFolderPath & parentFolderName) with properties {name:subfolder}: 親フォルダ内にサブフォルダを作成します。
  • display dialog "サブフォルダを作成しました: " & parentFolderPath & parentFolderName: 作成されたサブフォルダの情報をダイアログボックスで表示します。

サブフォルダを削除する

続いて、作成したサブフォルダを削除する方法を示します。


set parentFolderName to "MainFolder"
set subfolders to {"SubFolder1", "SubFolder2", "SubFolder3"}
set parentFolderPath to path to desktop folder as string

tell application "Finder"
    repeat with subfolder in subfolders
        if exists folder (parentFolderPath & parentFolderName & ":" & subfolder) then
            delete folder (parentFolderPath & parentFolderName & ":" & subfolder)
        end if
    end repeat
    if exists folder (parentFolderPath & parentFolderName) then
        delete folder (parentFolderPath & parentFolderName)
    end if
end tell

display dialog "サブフォルダを削除しました: " & parentFolderPath & parentFolderName
    

スクリプトの説明

  • set parentFolderName to "MainFolder": 親フォルダの名前を指定します。
  • set subfolders to {"SubFolder1", "SubFolder2", "SubFolder3"}: 削除するサブフォルダの名前をリストで指定します。
  • set parentFolderPath to path to desktop folder as string: 親フォルダのパスを指定します。
  • tell application "Finder": Finderアプリケーションを操作するためのブロックを開始します。
  • repeat with subfolder in subfolders: 各サブフォルダを繰り返し処理します。
  • if exists folder (parentFolderPath & parentFolderName & ":" & subfolder) then: サブフォルダが存在する場合に削除します。
  • delete folder (parentFolderPath & parentFolderName & ":" & subfolder): サブフォルダを削除します。
  • if exists folder (parentFolderPath & parentFolderName) then: 親フォルダが存在する場合に削除します。
  • delete folder (parentFolderPath & parentFolderName): 親フォルダを削除します。
  • display dialog "サブフォルダを削除しました: " & parentFolderPath & parentFolderName: 削除されたサブフォルダの情報をダイアログボックスで表示します。

まとめ

この記事では、AppleScriptを使用してFinderでフォルダを作成および削除する方法について詳しく解説しました。これらの基本的な操作をマスターすることで、macOS上でのファイル管理タスクを効率的に自動化することができます。次回の記事では、AppleScriptを用いたより高度なフォルダ操作とファイル管理のテクニックについてさらに詳しく解説しますので、お楽しみに!

上部へスクロール