Automator やAppleScriptを使って50MBごと分割したい

大量の画像ファイルを合計サイズ50MBごとに分割しフォルダに分けたいと考えています


AppleScriptで条件文まではできたのですが、フォルダを作成してファイルの移動の操作まで作れていません

フォルダ選択をしてフォルダ内のファイルを50MBごと分割したいと考えています

下記のプログラムを作っていましたがファイル選択(choose file)ではなくフォルダ内にあるファイルをループで処理したいです

アドバイスよろしくお願いします。



tell application "Finder"

--choose file でファイルを選ぶ(エイリアスとして)

get size of (choose file)

--結果(result)を 100000 で割って四捨五入、単位を整える

set file_size to ((round (result / 100000) rounding to nearest) / 10)

--ダイアログを表示する

set x to file_size

display dialog file_size

repeat until file_size > 50

get size of (choose file)

--結果(result)を 100000 で割って四捨五入、単位を整える

set x to ((round (result / 100000) rounding to nearest) / 10)

--ダイアログを表示する

set file_size to file_size + x

display dialog file_size

end repeat

end tell

MacBook Air 13", macOS 10.15

投稿日 2020/01/22 01:01

返信
スレッドに付いたマーク ランキングトップの返信

投稿日 2020/01/24 06:25

A案


on run
    try
        my main(choose folder)
    on error errs number errn
        return errs
    end try
end run

on main(dir)
    tell application "Finder"
        set t to 0
        set n to 1

        repeat with f in sort files in dir by name
            set ext to name extension of f

            if ext is in {"jpeg", "jpg", "png", "tiff", "tif", "bmp", "psd"} then
                set s to size of f

                if t + s > 50000000 then
                    set n to n + 1
                    set t to (size of f)
                else
                    set t to t + s
                end if

                set dst to (dir as text) & (n as text)

                if not (exists folder dst) then
                    make new folder at dir with properties {name:n as text}
                end if

                move f to folder dst
            end if
        end repeat
    end tell
end main


返信: 7
スレッドに付いたマーク ランキングトップの返信

2020/01/24 06:25 Hiro__S への返信

A案


on run
    try
        my main(choose folder)
    on error errs number errn
        return errs
    end try
end run

on main(dir)
    tell application "Finder"
        set t to 0
        set n to 1

        repeat with f in sort files in dir by name
            set ext to name extension of f

            if ext is in {"jpeg", "jpg", "png", "tiff", "tif", "bmp", "psd"} then
                set s to size of f

                if t + s > 50000000 then
                    set n to n + 1
                    set t to (size of f)
                else
                    set t to t + s
                end if

                set dst to (dir as text) & (n as text)

                if not (exists folder dst) then
                    make new folder at dir with properties {name:n as text}
                end if

                move f to folder dst
            end if
        end repeat
    end tell
end main


2020/01/24 06:24 junjun10 への返信

再投稿


いくつか作ってみました。


保存方法

スクリプトエディタにコードをコピペしてスクリプト形式で保存。


使い方

1 当該スクリプトファイルをスクリプトエディタで開き

2 ツールバーの「実行」を押し

3 ダイアログに従い処理したいフォルダを選択して「OK」ボタンを押す


A案 - AppleScript。コードはシンプルですが、処理速度がかなり遅いです。


B案 - JavaScript。スクリプトエディタにコピペする際、ツールバーで「JavaScript」を選択してください。処理速度は結構速いです。ただし、コードの内容は若干ハードル高めかも。


番外 - AppleScript。Python スクリプトをラッピングしたものです。次期 OS では Python がバンドルされなくなるようなので、自分で Python と PyObjC を組み込む必要があることから番外としましたが、処理速度は上の2つより速いです。



2020/01/22 03:36 junjun10 への返信

こちらを参考にしてください。


また、Finder辞書を活用するといいです。 → アプリケーションのスクリプト用語説明を表示する


デスクトップに"test"という新規フォルダを作成する。

tell application "Finder"
	set theNewFolder to make new folder at desktop with properties {name:"test"}
end tell

2020/01/24 06:26 Hiro__S への返信

B案


'use strict';

var app = Application.currentApplication(); app.includeStandardAdditions = true;

function run() {
    try {
        return main(app.chooseFolder().toString());
    } catch(err) {
        return err.message;
    }
}

function main(dir) {
    let n = 1;
    let t = 0;

    let filemanager = $.NSFileManager.defaultManager;
    let err = $();

    let files = filemanager.contentsOfDirectoryAtPathError(dir, err);

    if (err.isNil()) {
        for (let f of files.sortedArrayUsingSelector('localizedStandardCompare:').js) {
            let fpath = $(dir).stringByAppendingPathComponent(f);

            if (! /\.(jpe?g|png|tiff?|bmp|psd)$/i.test(f.js)) {
                continue;
            }

            let s = $();
            let err = $();

            let res = $.NSURL.fileURLWithPath(fpath).getResourceValueForKeyError(
                s,
                $.NSURLTotalFileSizeKey,
                err
            );

            if (res) {
                s = s.js;
                if (t + s > 50000000) {
                    n++;
                    t = s;
                } else {
                    t += s;
                }

                let num = n.toString().padStart(3, '0');
                let new_dir = $(dir).stringByAppendingPathComponent(num);

                if (! filemanager.fileExistsAtPath(new_dir)) {
                    let err = $();
                    let nil = $();
                    let res = filemanager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(
                        new_dir,
                        true,
                        nil,
                        err
                    );
                    if (! res) {
                        throw new Error(err.localizedDescription.js);
                    }
                }

                let new_fpath = new_dir.stringByAppendingPathComponent(fpath.lastPathComponent);
                let err = $();

                if (! filemanager.moveItemAtPathToPathError(fpath, new_fpath, err)) {
                    throw new Error(err.localizedDescription.js);
                }
            }
        }
    }
}


2020/01/24 06:26 Hiro__S への返信

番外


on run
    try
        my main(choose folder)
    on error errs number errn
        return errs
    end try
end run

on main(dir)
    do shell script "python <<'EOF' - " & quoted form of POSIX path of dir & "
# coding: utf-8

import sys, os, re, Cocoa

folder = os.path.normpath(sys.argv[1]).decode('utf-8')
files = Cocoa.NSArray.arrayWithArray_(os.listdir(folder))
image = re.compile(ur'\\.(?:jpe?g|png|tiff?|gif|bmp|psd)$', re.I)

n = 1
t = 0

for f in files.sortedArrayUsingSelector_('localizedStandardCompare:'):
    fpath = os.path.join(folder, f)

    if not image.search(f) or os.path.isdir(fpath):
        continue

    res, s, err = Cocoa.NSURL.fileURLWithPath_(fpath).getResourceValue_forKey_error_(
        None,
        Cocoa.NSURLTotalFileSizeKey,
        None
    );

    if res:
        if t + s > 50000000:
            n += 1
            t = s
        else:
            t += s

        new_dir = os.path.join(folder, u'{:03d}'.format(n))

        if not os.path.exists(new_dir):
            os.mkdir(new_dir)

        new_fpath = os.path.join(new_dir, f)

        os.rename(fpath, new_fpath)
EOF"
end main


このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

Automator やAppleScriptを使って50MBごと分割したい

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。