[ AppleScript ] Finder 関連のサンプル集

Finder 関連の AppleScript サンプル集です。

Finder で新規フォルダを作成するサンプル ———————————————————————-

デスクトップに「123456」という新規フォルダを作成します。

tell application "Finder"
    make new folder at desktop with properties {name:"123456"}
end tell

動作確認:Mac OS 9.2.2

Finder でクリップボードの中身を取り出すサンプル ———————————————————————-

クリップボードの中身を取り出す AppleScript のサンプルです。

tell application "Finder"
    set CB to the clipboard
    log "クリップボードの中身→" & CB
end tell

動作確認:Mac OS 10.4.5

Finder でカレントユーザの色んなパスを取得するサンプル ———————————————————————-

カレントユーザの色んなパスを取得する AppleScript のサンプルです。

tell application "Finder"
    set a to desktop as text -- デスクトップ
    set b to home as text -- ホーム
end tell

ハードディスク名から始まる絶対パス?ではなく、環境に依存しない汎用的なパス指定をしたいもんですね。

動作確認:Mac OS 10.4.7

[ AppleScript ] Finder でラベルを付けるサンプル ———————————————————————-

次のサンプルでは、[ ホーム ] フォルダ内の [ デスクトップ ] フォルダに、オレンジのラベルを設定しています。

tell application "Finder"
    try
        set label index of desktop to 1
    on error
        log ("Error")
    end try
end tell

ラベルの種類を変更するには、

set label index of desktop to 1

と言うコードの末尾にある数字を変更する事で、ラベルの種類を変更できます。 どの数字がどのラベル名に対応しているかは、次の通りです。

数字 ラベル名
0 ラベルなし
1 オレンジ
2 レッド
3 イエロー
4 ブルー
5 パープル
6 グリーン
7 グレイ

動作確認 : Mac OS 10.5.7 + AppleScript 2.0.1

Finder でシステム終了するサンプル ———————————————————————-

システム終了する AppleScript のサンプルです。

tell application "Finder"
  shut down
end tell

スクリプトを実行すると、ダイアログを出さずに「システム終了」します。

動作確認:Mac OS 10.4.5

Finder で選択階層に新規フォルダを作成するスクリプト ———————————————————————-

選択階層に新規フォルダを作成する AppleScript のサンプルです。

  • Mac OS X の Finder でリスト表示していると、任意の場所で新規フォルダが作りにくい。
  • 普通に「新規フォルダ」を選択すると、リスト表示している最上層にフォルダが作られてしまう。
  • コンテクストメニューかスクリプトで何とかならんだろうか…。

なんて考えて作ってみました。

  • フォルダを選択していたら、その中に新規フォルダを作成。
  • ファイルを選択していたら、同階層に新規フォルダを作成。
  • 複数項目を選択していると、その分処理を繰り返し。
tell application "Finder"
  repeat with ThisFile in (selection as list)
    set a to (ThisFile as alias) as text
    if a ends with ":" then
      make new folder at (a as alias)
    else
      make new folder at (my MyFilePath(a) as alias)
    end if
  end repeat
end tell
on MyFilePath(this_text)
  set y to 0
  repeat with thisChar in this_text
    set y to y + 1
    set x to the offset of thisChar in ":"
    if x is not 0 then
      set z to y
    end if
  end repeat
  return (text from character 1 to z of this_text)
end MyFilePath

動作確認:Mac OS 10.4.8

Finder で選択しているファイルにコメントを付けるサンプル ———————————————————————-

Finder で選択しているファイルすべてにコメントを付ける AppleScript のサンプルです。スクリプトメニューから実行できるので便利。

display dialog "Comment ?" default answer "Comment" with icon 1
set myTitle to text returned of result
tell application "Finder"
  repeat with thisF in (selection as list)
    set comment of thisF to myTitle
  end repeat
end tell

既にコメントが存在した場合、既存のコメントを上書きしますので、取り扱いにご注意下さい。

動作確認:Mac OS 10.4.6

Finder のウインドウをリサイズするサンプル ———————————————————————-

しつこいようですが、今度は、Finder のウインドウをリサイズする AppleScript です。 Finder のウインドウサイズだけ変えても面白くないので、mi のウインドウも一緒に変更してみます。

ちなみに、私のディスプレイサイズは、1600-1200 です。

// mi
tell application "mi"
  activate
  set bounds of window (get windowindex of document 1) to {0, 44, 1200, 1200}
end tell
// Finder
tell application "Finder"
  activate
  set bounds of window 1 to {1200, 44, 1600, 1200}
end tell

これで、画面左に mi のウインドウ、画面右に Finder のウインドウがピッタリ配置され、ファイルをドロップしやすくなった。

上の要領で、mi + Safari や、Finder + Cyberduck のセット等を作っておき、メニューバー登録しておくと、ワークスペースを簡単に切り替えられて便利。

メニューバーへの登録方法は、こちらの記事 を参考に。

Finder で日付を名前にして新規フォルダを作成するサンプル ———————————————————————-

日付を名前にして新規フォルダを作成する AppleScript のサンプルです。 リクエストがあったんで作ってみました。

copy (current date) to today

set Y to year of today
set M to month of today
set D to day of today

if D ≤ 9 then set D to "0" & D

if M is January then set M to "01"
if M is February then set M to "02"
if M is March then set M to "03"
if M is April then set M to "04"
if M is May then set M to "05"
if M is June then set M to "06"
if M is July then set M to "07"
if M is August then set M to "08"
if M is September then set M to "09"
if M is October then set M to "10"
if M is November then set M to "11"
if M is December then set M to "12"

tell application "Finder"
    make new folder at desktop with properties {name:(Y as string) & (M as string) & (D as string)}
end tell

動作確認:Mac OS 10.4.4

ファイルリストにあるファイルに Finder でラベルを付けるサンプル ———————————————————————-

ファイルリスト ( テキストファイル ) に合致するファイルに Finder でラベルを付けるアップルスクリプトのサンプルです。

set Sourcetxt to (choose file with prompt "ソーステキストファイルを指定して下さい" as string) as string
set ThisFolder to (choose folder with prompt "フォルダを選んでください")

-- ファイル読み込み
set aFilePointer to open for access alias Sourcetxt
set aData to read aFilePointer as text
close access aFilePointer

-- 配列に格納
set AppleScript's text item delimiters to return
set aList to text items of aData
set AppleScript's text item delimiters to tab
tell application "Finder"
    repeat with i from 1 to count of aList
        try
            set theFile to ((ThisFolder as text) & (item i of aList) as text) as alias
            set label index of theFile to 4
        on error
            log ("error" & item i of aList)
        end try
    end repeat
end tell

ファイルリスト ( テキストファイル ) は、文字コードが Shift-JIS で、改行コードが CR で、最終行の改行無しにしておかないと動きません。

ドロップしたファイルにコメントを付加するサンプル ———————————————————————-

ドロップされたファイルすべてにコメントを付加する AppleScript のサンプルです。アプリケーションとして保存してから使います。

on open myList
  display dialog "Comment ?" default answer "Comment" with icon 1
  set myTitle to text returned of result
  repeat with thisF in myList
    tell application "Finder"
      set comment of thisF to myTitle
    end tell
  end repeat
end open

既にコメントが存在した場合、既存のコメントを消去して新しいコメントを追加しますので、取り扱いにご注意下さい。

動作確認:Mac OS 10.4.6

デスクトップの解像度を取得するサンプル ———————————————————————-

デスクトップの解像度を取得する AppleScript のサンプルです。 the desktop と言うオブジェクトは、Mac OS 10.4 以降じゃないと使えないらしい。

tell application "Finder"
    bounds of the window of the desktop
end tell

動作確認:Mac OS 10.4.5

選択項目の種類を取得するサンプル ———————————————————————-

Finder で選択している項目の種類を取得する AppleScript のサンプルです。

tell application "Finder"
  repeat with ThisFile in (selection as list)
    set ThisFileInfo to info for ThisFile as alias
    log (kind of ThisFileInfo)
  end repeat
end tell

動作確認:Mac OS 10.4.7

変数の内容をテキストファイルに書き出すサンプル ———————————————————————-

変数の内容をテキストファイルに書き出す ( 追記 ) する AppleScript のサンプルです。

デスクトップに「test.txt」と言う名前のファイルが存在しない場合、新規作成します。ファイルが既に存在していれば追記。

tell application "Finder"
  set aText to ("日本語のサンプル文字列" & return)
  set aTextFile to open for access ((desktop as text) & "test.txt") with write permission
  set aEOF to get eof of aTextFile
  try
    write aText starting at (aEOF + 1) to aTextFile
  on error aErrorText
    display dialog aErrorText
  end try
  close access aTextFile
end tell

日本語を書き出す時、文字コードはどうなるんだろう?

動作確認:Mac OS 10.4.7

8 COMMENTS

si

はじめまして。 このサンプルに、ときめきました。

‘123456’という名称を作成日時にする事も可能なんでしょうか? 2006年2月15日に実行すれば、 ‘20060215’という名称になるように。

アドバイスいただければ、嬉しいです。 お願いします。

si

感謝です。おねだりしてしまって申し訳ないです。 早速DLさせてもらい、実行に成功しました。

大変便利なモノをサッと制作できるなんて憧れます。 勉強のサンプルにさせて頂きます。

ひとつ、ヒントもらえませんでしょうか? 1)’20060215’フォルダ作成スクリプトと、 2)’日付’フォルダ作成スクリプトでは、 実行した時。 1)は、直ぐにフォルダを作成。 2)は、RunをQuitの選択ダイアログが出る。 この動作の違いはスクリプトに書いてあるのでしょうか?

調子に乗って質問までしてしまい、申し訳ないです。

Bowz

動きましたか…。良かったです。

質問の件ですが、ひょっとして、スクリプトをアプリケーションとして保存する時のオプションで「初期画面」のチェックが入っていませんか?

si

まさしく、初期画面にチャックはいってました。 ひとつ賢くなりました。

ありがとうございました!

Bun

はじめまして リストからラベルをつけるスクリプト とても重宝しております。

1点お願いがございます 現状、選択したフォルダー内のファイルのみラベルを付ける設定になっていると思われますが、親フォルダー以下の階層のファイルにもラベルを付けたいと切に願っております、お手数でなかったら是非ご教授いただけないでしょうか。

よろしくお願いします。

bun

はじめまして、ラベルを付けるサンプル重宝しております

現状選択したフォルダー内のファイルにラベルを付けるスクリプトですが 選択フォルダー以下のフォルダー内のファイルにもラベルをつけたいと切に願っております

よろしくれば、ヒントいただけますでしょうか。

Yoshitaka Goto

Bun さん、コメントありがとうございます! ちょっと今、本業が忙しいのと、ずいぶんと AppleScript を書いていないので、 お力添えが難しい状況です。申し訳ありません。(T_T)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です