LoginSignup
0
4

More than 5 years have passed since last update.

スキャンされた文書を自動的に年月フォルダに分類するフォルダアクション

Last updated at Posted at 2017-05-24

きっかけ

ScanSnapで紙形式の文書をpdf化して、特定のフォルダに保存されるようにし、scanフォルダ内が肥大化してきたら月ごとにフォルダ分けして整理していた。(scanned/201705 , scanned/201704等)
Automatorで自動化できるのではないかと思いやってみた。

Automatorでスクリプトを書いたことは殆ど無いので、すべてシェルスクリプトとかメジャーなものに投げて、それを実行するプログラムにしようかと一瞬考えたが、あまりスマートなものでもないのでできるだけAutomator内でやってみることにした。

方法

Automatorでサービスフォルダアクションを作成する。

(2017/11/24 フォルダアクションの間違いでした、訂正)
スクリーンショット 2017-11-24 9.58.51.png

プログラムを書く

流れとしては非常に単純で、現在の月日の名前のついたフォルダを新規作成し、入力ファイルを全て移動するのみである。
スクリーンショット 2017-05-24 12.28.07.png

書いたAppleScript

目的達成のため、ぽそっとAppleScriptを書く必要があったので。

日時を整形するScript

Automatorの「変数」には現在の日付を取得する変数「現在の月」、「現在の年」があるが、そのまま連結すると、"20175月"となってしまい、やや不格好である。
そこで、AppleScriptを用いて、日付を整形することにした。
(参考:AppleScriptで現在日時の文字列を取得する)

on run {input, parameters}
    set current_date to current date

    set val_year to year of current_date as number
    set val_month to month of current_date as number

    if val_month < 10 then
        set val_month to "0" & val_month as string
    end if
    set data_time_string to val_year & val_month
    return data_time_string as string
end run

実際にファイルを移動するScript

引数の一つ目に受け取ったフォルダへ、二つ目以降に受け取ったファイルを移動するScriptを書いた。
Automatorでは、処理されなかった引数がだらだら追加されていくようである。
(参考:AutomatorでAppleScriptに値を渡す)

on run {input, parameters}
    set tgtFolder to item 1 of input
    repeat with counter from 2 to (count of input)
        set tgtFiles to item counter of input
        tell application "Finder"
            move tgtFiles to tgtFolder
        end tell
    end repeat
    return input
end run

ファイル移動にはFinderを用いた。
(参考:Applescriptのごく基本的なサンプル(OS X用))

テスト

テスト用には指定されたFinder項目を取得を用いる。
スクリーンショット 2017-05-24 12.38.54.png
ちなみに種類フォルダ ではない フィルタを適用しないと、どんなフォルダを作っても、現在の年月フォルダ内に勝手に移動させられてしまう。
年月フォルダができた瞬間に、自身を中に移動させようとしてエラーが起きそう。

まとめ

これは僕がAutomatorに慣れていないからかもしれないが、Automatorだけでは少し力不足感があった。
AppleScriptまで組み合わせるともうちょっといろいろできそうかも。
ちょいネタにはいいかもしれないけど、本格的に勉強するには、シェルスクリプトとかと比べるとコスパが悪い。

0
4
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
0
4