Apple の脅威の通知と金銭目当てのスパイウェアへの対策について

AutomatorでPDFをシャッフルしたい

PDFをシャッフルして結合したいのですが、エラー6が表示されて結合できません。

OSはVenturaです。

MacBook Air (2018 – 2020)

投稿日 2023/05/07 19:21

返信
スレッドに付いたマーク ベストな回答

投稿日 2023/05/08 18:03

こんな感じの Automator クイックアクションでどうでしょうか。当方ではこれで大丈夫です。


コード

'use strict';

ObjC.import('Cocoa');
ObjC.import('PDFKit');

function setfname() {
    const df = $.NSDateFormatter.alloc.init;
    df.locale = $.NSLocale.localeWithLocaleIdentifier('en_US_POSIX');
    df.dateFormat = 'yyyyMMddHHmmss';
    const now = df.stringFromDate($.NSDate.date).js;
    return `${now}.pdf`;
}

function makedocs(argv) {
    const docs = [];
    const pp = [];
    for (const f of argv) {
        const furl = $.NSURL.fileURLWithPath(f);
        const doc = $.PDFDocument.alloc.initWithURL(furl);
        docs.push(doc);
        pp.push(Number(doc.pageCount));
    }
    const max = pp.reduce((a, b) => Math.max(a, b), -Infinity);
    return {docs, max}
}

function run(argv) {
    argv = argv.map(x => x.toString());

    const {docs, max} = makedocs(argv);
    const pdf = $.PDFDocument.alloc.init;
    let idx = 0;

    for (let i = 0; i < max; i++) {
        for (const doc of docs) {
            if (i < Number(doc.pageCount)) {
                pdf.insertPageAtIndex(doc.pageAtIndex(i), idx++);
            }
        }
    }

    const dname = $(argv[0]).stringByDeletingLastPathComponent.js;
    const fname = setfname();
    const fpath = $(dname).stringByAppendingPathComponent(fname).js;

    if (pdf.writeToFile(fpath)) {
        return fpath;
    } else {
        throw new Error(`failed to write to file "${fpath}"`);
    }
}


返信: 4
スレッドに付いたマーク ベストな回答

2023/05/08 18:03 Jennatoni への返信

こんな感じの Automator クイックアクションでどうでしょうか。当方ではこれで大丈夫です。


コード

'use strict';

ObjC.import('Cocoa');
ObjC.import('PDFKit');

function setfname() {
    const df = $.NSDateFormatter.alloc.init;
    df.locale = $.NSLocale.localeWithLocaleIdentifier('en_US_POSIX');
    df.dateFormat = 'yyyyMMddHHmmss';
    const now = df.stringFromDate($.NSDate.date).js;
    return `${now}.pdf`;
}

function makedocs(argv) {
    const docs = [];
    const pp = [];
    for (const f of argv) {
        const furl = $.NSURL.fileURLWithPath(f);
        const doc = $.PDFDocument.alloc.initWithURL(furl);
        docs.push(doc);
        pp.push(Number(doc.pageCount));
    }
    const max = pp.reduce((a, b) => Math.max(a, b), -Infinity);
    return {docs, max}
}

function run(argv) {
    argv = argv.map(x => x.toString());

    const {docs, max} = makedocs(argv);
    const pdf = $.PDFDocument.alloc.init;
    let idx = 0;

    for (let i = 0; i < max; i++) {
        for (const doc of docs) {
            if (i < Number(doc.pageCount)) {
                pdf.insertPageAtIndex(doc.pageAtIndex(i), idx++);
            }
        }
    }

    const dname = $(argv[0]).stringByDeletingLastPathComponent.js;
    const fname = setfname();
    const fpath = $(dname).stringByAppendingPathComponent(fname).js;

    if (pdf.writeToFile(fpath)) {
        return fpath;
    } else {
        throw new Error(`failed to write to file "${fpath}"`);
    }
}


2023/05/08 15:51 Jennatoni への返信

# このボードの不具合により回答が消えてしまったようなので再投稿します。(内容は前回と異なります)


まず、エラーについてですが、今朝の時点では Jennatoni さんと同様だったのですが、何かの拍子に正常に動くようになりました...。きっかけは分かりません...。とりあえず OS をセーフモードで起動し、立ち上がったら何もせず再起動してみる (これで諸々のキャッシュがクリアされる) と良いかもしれません。


次に、消えてしまったワークフローですが「PDFページを結合」アクションとはシャッフルの方法が異なるのでボツにしてください。で、セーフモードとかいろいろ試しても当該アクションが正常に動かないならその旨お知らせください。作り直したワークフローを投稿しなおします。


AutomatorでPDFをシャッフルしたい

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