Skip to content

Instantly share code, notes, and snippets.

@Klustre
Last active June 8, 2019 08:02
Show Gist options
  • Save Klustre/b90580a0e3310f80c3133103b504e15b to your computer and use it in GitHub Desktop.
Save Klustre/b90580a0e3310f80c3133103b504e15b to your computer and use it in GitHub Desktop.
Encode an image for use in ScriptUI
// Courtesy of @joonaspaakko
const picker = document.querySelector('input')
picker.oninput = function() {
const file = picker.files[0]
getBinary( file, function( binary ) {
const string =
`var dialog= new Window ("dialog");
var image = "${binary}";
dialog.add ("image", undefined, File.decode(image) );
dialog.show();`
document.body.innerHTML = string
})
}
function getBinary( file, callback ) {
const reader = new FileReader()
reader.readAsBinaryString( file )
reader.onload = function(){
callback( encodeURIComponent( reader.result ) )
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Encode an image for use in ScriptUI</title>
</head>
<body>
<input type="file" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment