Modify a very simple old piece of AppleScript
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

There is an old add-on action for the Mac Automator program called 'Dispense Items Incrementally'. The source code is here — it is extremely short:

http://automator.us/leopard/downloads/index.html

The problem with Automator is that if you pass it 1,000 files it will then try to process all 1,000 of these files at once. Instead, this action passes one file at a time — you add a loop at the end of the Automator program so that it will loop through all the files that are passed until completion

The problem is that I have a use case where it would be far more preferable to pass n files at a time, say 50 or 100, rather than just one file at a time. But my coding skills are limited.

All I need here is a way to set the number of files that are passed by this action. It doesn't need to be a variable that's exposed in the action in Automator — I'm happy to just recompile the action for each n that I want. Of course, if the n could be set in Automator, that would be nice. But it's not necessary.

Thanks!

awarded to gabrielsimoes

Crowdsource coding tasks.

1 Solution

Winning solution

I was successful using the following JavaScript code in Automator, and doing the usual loop technique:

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

var fm = $.NSFileManager.defaultManager

var blockSize = 2
var tempFilename = "H8K3B"

function run(input, parameters) {
    var tempFile = parameters["temporary items path"] + "/" + tempFilename

    var list = []
    for (var i = 0; i < input.length; i++) {
        list.push(input[i].toString())
    }

    if (fm.fileExistsAtPath(tempFile)) {
        var content = app.read(Path(tempFile))
        var count = parseInt(content)
    } else {
        var r = fm.createFileAtPathContentsAttributes(tempFile, $.NSData.data, {})
        var count = 0
    }

    var file = app.openForAccess(Path(tempFile), { writePermission: true }) 
    app.write((count+1).toString(), {to: file, startingAt: app.getEof(file)})
    app.closeAccess(file)

    return list.slice(blockSize*count, blockSize*(count+1))
}

You would just have to change blockSize to the desired size, and set the loop count to the appropriate number.

To use this code in Automator, you just have to add the action "Run JavaScript" under "Utilities".

This seems to work well, thanks! I'm happy to give you the bounty but I wanted to check about one thing — is there any straightforward way to get this to work if I pass it multiple folders, with files contained within? Thanks!
charlespetersen 14 days ago
I'll look into that
gabrielsimoes 14 days ago
I could make something that expands folders into files, but this wasn't implemented in the original "Dispense Items Incrementally", was it?
gabrielsimoes 14 days ago
Huh, I guess not, I hadn't tested out 'dispense items incrementally' very much since it didn't do what I needed. If making this new script work with folders isn't trivial then don't worry about it — it's not a big deal. Thanks so much for your help!
charlespetersen 14 days ago