Skip to content

Instantly share code, notes, and snippets.

@krizpoon
Last active June 21, 2016 13:11
Show Gist options
  • Save krizpoon/1d2bdb231918de1dd5b56a815b0a6607 to your computer and use it in GitHub Desktop.
Save krizpoon/1d2bdb231918de1dd5b56a815b0a6607 to your computer and use it in GitHub Desktop.
/**
* This JXA script combines 2 or more images into one using ImageMagick.
*
* Prerequisite:
* 1. Mac Yosemite
* 2. ImageMagick
*
* Steps:
* 1. Launch Automator app
* 2. Create an Automator service.
* 3. Set the input of the service as "image files" in "Finder"
* 4. Add a "Run JavaScript" task.
* 5. Paste the following code into the script field.
* 6. Save the service and give it a name.
*
* How to use:
* 1. Select some image files in Finder
* 2. Run the service from the system's Service menu.
*/
var ImageMagickConvert = '/usr/local/bin/convert'; // point this path to the installed location of ImageMagick's convert command
var app = Application.currentApplication();
app.includeStandardAdditions = true;
function alert(text, msg) {
var options = { };
if (msg) options.message = msg;
app.displayAlert(text, options);
}
function run(input, parameters) {
if (!input || input.length < 2) {
alert('Please select 2 or more images to combine!');
return input;
}
var cmd = ImageMagickConvert;
var output = null;
var names = [];
var dir = null;
input.forEach(function(file){
cmd += ' "' + file + '"';
// get the file name of the input file and use it for the output file name
var pathComponents = file.toString().split('/');
var filename = pathComponents.pop().split('.').shift();
var dirname = pathComponents.join('/');
names.push(filename);
// use the first file's directory as the output directory
if (!dir) dir = dirname;
});
// compose the output path
output = [dir, names.join('+') + '.png'].join('/');
// compose the final command
cmd += ' +append "' + output + '"';
// execute the command
app.doShellScript(cmd);
// return the output to next task
return Path(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment