Top / AppleScript

Finder Scripts

[1] [2] [3] [4] [5]

●今開いているフォルダ (Finderウィンドウ) のパスをテキストに保存

作業中のファイル (のエイリアス) はデスクトップに置いておくべきなのかもしれないのですが、コンピュータを共同で利用していたり、デスクトップが散らかるのを嫌って、フォルダに入れて整理しておくことが多いのではないでしょうか。でも、そうやって整理しちゃうとデスクトップにあれば一目瞭然だったのが、目的のファイルがどこにあるのか分からなくなったりします。まあ閉じなければ、スリープ、再起動やシステム終了をしてもそのまま開いたままになって問題ないのですが、ずっと同じ作業をしているわけじゃないので、別の作業のときは閉じたくなるんですよね。

以下のスクリプトは、今開いているフォルダ (Finderウィンドウ) のパス (「Macintosh HD:書類:work:」みたいなもの) をテキストファイルに出力してデスクトップに保存します。作業中の (開いている) ファイルのパスを起動中のアプリケーションから取得できる場合もあるので、そういう風に改造してもいいかもしれません。

実はこんなファイルを作成しなくても、作業中のファイルのエイリアスを1つのフォルダにまとめて保存しておくのが簡単ですし、わかりやすいでしょう (File Buddyみたいなファイルブラウザというか、いろんな階層・フォルダにあるファイルを束ねてリスト表示できるソフトウェア、ランチャー、アーカイブソフトなどを利用してもいいですね)。でもそれには問題もあって、コピー (バックアップ) したエイリアスをダブルクリックすると、バックアップ中にある対応するファイルでなく、元のファイルを開こうとしちゃうんです。ふつうにコピーしてバックアップした場合、エイリアスのパスは自動的には変更されないわけです。だから、普段はエイリアスを使ったとしても、以下のようなスクリプトも併用すると便利かもしれません (テキストなのでパスを書き換えたり、一覧を目視できる)。

set theName to "Finder_" & strDate() of me
-- set theName to "Finder_" & date string of (current date) -- 標準的な日付の文字列を使う
set theFolder to (path to desktop) as string -- 保存するフォルダを指定 (これはデスクトップ・フォルダ)
set thePath to theFolder & theName & ".txt" -- 作成するファイルのパス

tell application "Finder"
    -- if (exists file thePath) then delete file thePath -- 既に同名のファイルがあれば、それはゴミ箱に捨てる
    if (exists file thePath) then -- 既に同名のファイルがあった場合、別名にする (番号を付加) 
        set theName to newName(theName, theFolder) of me
        set thePath to theFolder & theName & ".txt"
    end if
    make file at folder theFolder with properties {name:theName & ".txt"} -- 実際にファイルを作る
    open for access file thePath with write permission
    try -- 多くの環境で動くようにとFinderWindowsというのを導入してみた
        set FinderWindows to every window as alias list
    on error
        set FinderWindows to every window
    end try
    -- でも、なぜか環境によっては激しく遅くなってしまうみたい
    -- その場合、上のtryからend tryまでの5行をなくして
    -- 下のFinderWindowsのところをevery windowとか
    -- folder of aWindowをcontainer of aWindowとかにしたらいいかも?
    repeat with aWindow in FinderWindows
        write ((folder of aWindow) as string) & return to file thePath
        -- if popup of aWindow is false then write ((folder of aWindow) as string) & return to file thePath
    end repeat
    close access file thePath
    set creator type of file thePath to "QEdt" -- クリエータを好みのテキストエディタにする (これはQuoEdit)
end tell

on strDate() -- 今日の日付の文字列 (20010203みたいな) を返す
    set theDate to (current date) as string -- 今の日付と時刻の文字列がtheDateに得られる
    set theMonth to word 3 of theDate as number
    set theDay to word 5 of theDate as number
    if theMonth < 10 then set theMonth to "0" & theMonth -- 一桁の月の頭に0を付ける
    -- set theMonth to characters -2 thru -1 of ("0" & theMonth as string) -- 頭に0を付けて末尾2文字を取得
    if theDay < 10 then set theDay to "0" & theDay -- 一桁の日にちの頭に0を付ける
    set theDate to word 1 of theDate & theMonth & theDay
    return theDate -- 実は書かなくてもいい
end strDate

on newName(origName, theFolder)
    set theName to origName
    set num to ""
    tell application "Finder"
        repeat until not (exists file (theName & num & ".txt") of folder theFolder)
            -- repeat while (exists folder (theName & num &  ".txt") of folder theFolder)
            -- 付加する番号は1から
            set num to (num as number) + 1
            -- 付加する番号は2から
            -- if num = "" then set num to 1
            -- set num to num + 1
        end repeat
    end tell
    set theName to theName & num
    return theName
end newName

●テキストに書かれたフォルダ (Finderウィンドウ) を開く

上のようにして作成した各行がフォルダ (Finderウィンドウ) のパス (「Macintosh HD:書類:work:」みたいなもの) になっているテキストを読み込んで、それらすべてを開くスクリプトです。このドロップレットアプリケーション (このスクリプトをアプリケーション形式で保存) にいくつかのテキストをドラッグ&ドロップすればいいです。ドロップレットでなく、コンテクストメニューとかから実行できるように改造してもいいと思います。

on open theItemsDropped
    tell application "Finder"
        repeat with aFile in theItemsDropped
            set theFile to open for access aFile
            try
                repeat
                    set theFolder to read theFile before return
                    open folder theFolder
                end repeat
            end try
            close access theFile
        end repeat
    end tell
end open

Top / AppleScript