Automating Photoshop with JS - javascript

I had to place videos(mp4-files) in one photoshop document. I thought it would be easier to find a solution with png/jpg, and then project it on mp4. but the fact is that photoshop saving png/jpg and mp4 in different ways. Therefore, despite the fact that there is an import solution, I have difficulties with exporting mp4 by code.
I have 2 arrays of mp4 files and each mp4 from the first array needs to be overlaid on each of the second and saved by mp4. I solved the problem by uploading a video to an open photoshop file with a simple code:
function replaceContents(newFile) {
var docRef = app.open(newFile);
return docRef;
}
function importVideos(order_number) {
var doc = app.activeDocument;
var file = new File('E:/path/' + order_number + '.mp4');
// open a new document with needed video
var docTemp = replaceContents(file);
// copy opend layer with video from new doc to my main doc
var layer = docTemp.activeLayer.duplicate(doc.layerSets.getByName(color), ElementPlacement.PLACEATEND);
// close new unnecessary doc
docTemp.close(SaveOptions.DONOTSAVECHANGES);
layer.name = order_number;
return layer;
}
Here is the code for saving videos and in doExport() doc should be saved as a video.
function Saving(color) {
var array1 = app.activeDocument.layerSets.getByName('s');
var array2 = app.activeDocument.layerSets.getByName(color);
for (i = 0; i < 5; i++) {
array1.artLayers[i].visible = true;
for (j = 0; j < 5; j++) {
array2.artLayers[i].visible = true;
doExport();
array2.artLayers[i].visible = false;
}
array1.artLayers[i].visible = false;
}
}
So a new question: how to export a video from photoshop with a code with the ability to specify the file name and the save path?
P.S. if you do this through Actions, you can't enter input parameters like the name of the saved file, it seals the Action as you did it.
If you know how to create arguments for Actions, you are welcome!

Related

Convert local path URI to File or Blob

I am working on a copy and paste feature for my website, so here is my problem. When i copy an image directly from a webpage it works as it should (the first if statement on the code), but if i am trying to copy a image from my own computer a i get a local path (like the one in the else statement)
$scope.pasteImage = function(eventPaste)
{
$scope.uploading = true;
var promises = [];
var items = (eventPaste.clipboardData || eventPaste.originalEvent.clipboardData).items;
for (var i = 0; i < items.length; i++)
{
var blob = null;
if (eventPaste.originalEvent.clipboardData.items[i].type.indexOf("image") == 0 || eventPaste.originalEvent.clipboardData.items[i] == 0)
{
blob = eventPaste.originalEvent.clipboardData.items[i].getAsFile();
}
else
{
var file = new File("file:///home/oem/testabc/vembly/source/server/images/pregnant.png")
console.log(file)
}
}
console.log(eventPaste)
console.log(blob)
var files = [blob];
uploadService.uploadMultiple(files)
}
so, my question is if its possible to transform that file (else statment) into a blob so i can use it in the uploadMultiple(files) funtction that i have.
No.
It would be a huge security problem if any website could use JavaScript to read data from any file path on your system it happened to guess existed.

Firebase Bulk json upload

I am new in Firebase.I haven't huge knowledge on the firebase.I want a bulk file uploader.
Suppose I have a file uploader in HTML.Using this a CSV/XML file will upload from a HTML.I convert this CSV/XML file in JSON(Array of JSON) then I want to upload this file in firebase.I have already converted and uploaded this file in firebase.But I face some problem, when file size is going big it takes too much time.
$rootScope.showLoader = true;
usSpinnerService.spin('spinner-1');
var ref = firebase.database().ref().child('Cars')
var newItem = $firebaseObject(ref);
var obj = {};
var count = 0;
for (var i = 0, len = jsonFile.length; i < len; i++) {
newItem[jsonFile[i]["DealerID"]] = jsonFile[i];
newItem.$save().then(function() {
count++;
if (count === (jsonFile.length - 1)) {
$rootScope.showLoader = false;
usSpinnerService.stop('spinner-1');
toastr.success('Successfully Upload this file');
}
})
}
This is my code. I use here angularfire service for this.
Can anyone give example how I optimize the time? It will be helpful for me. I can't use any server here.

Javascript: Audio from local file not playing without HTML "<audio>" element present

Now this one really has me stumped, so I was hoping to see if anyone could spot where I am doing things incorrectly.
So essentially, I have a page with two elements. One is an HTML5 file handler, and the second is a button. When the user selects a file I respond to the onchange event that is generated, decoding the audio and constructing a buffer to be used. I know there is the HTML5 audio tag, but this is going to be a utility that needs to be able to break up the file into manageable chunks.
I have done several tests and have found that the audio that I decode myself will only play after an audio element on the page has been played. I have absolutely no idea what could be causing this behavior, since I have studied several examples online on playing audio. I will include my audio engine below.
Just to clarify, everything is handled by this code.
Thank you.
"use strict";
var AudioFile = function (){
this.length = 0; // Number of samples
this.duration = 0; // Time in seconds
this.sampleRate = 0; // Sample rate
this.channels = 0; // Number of channels
this.data = []; //Audio/Waveform data
};
var audioCtx = null;
class AudioEngine {
constructor (){
// All of the necessary audio control variables
if(!audioCtx){
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioCtx = new AudioContext();
}
// Will hold audio data waiting to be played
this.buffer = null;
// This will hold the decoded audio file upon completion
this.decodedFile = new AudioFile();
// Automatically create buffer upon finished decoding?
this.autoCreateBuffer = true;
// Specify this if you want to have a function recieve
// the decoded audio, i.e. completionCallback(decodedFile);
this.completionCallback = null;
}
// This will decode an audio file
fileCallback (event){
console.log("file callback");
this.buffer = null;
var reader = new FileReader();
var file = event.target.files[0];
reader.onload = this.loadCallback.bind(this);
reader.readAsArrayBuffer(file);
}
// Called by fileCallback after file has been loaded
loadCallback (file){
console.log("load callback");
var raw = file.target.result;
audioCtx.decodeAudioData(raw, this.decodeCallback.bind(this));
}
// Called by loadCallback after file has been decoded
decodeCallback (data){
console.log("decode callback");
var audioTemp = new AudioFile();
audioTemp.length = data.length;
audioTemp.duration = data.duration;
audioTemp.sampleRate = data.sampleRate;
audioTemp.channels = data.numberOfChannels;
var arr = [];
for(var i = 0; i < data.numberOfChannels; i++){
arr.push(new Float32Array(data.length));
data.copyFromChannel(arr[i], i);
}
audioTemp.data = arr.slice(0);
this.decodedFile = audioTemp;
if(this.autoCreateBuffer){
var buffer = audioCtx.createBuffer(audioTemp.channels, audioTemp.length, audioTemp.sampleRate);
var samples;
for(var c = 0; c < audioTemp.channels; c++){
samples = buffer.getChannelData(c);
for(var i = 0; i < audioTemp.length; i++){
samples[i] = this.decodedFile.data[c][i];
}
}
this.buffer = buffer;
}
if(this.completionCallback){
this.completionCallback(audioTemp);
}
}
// Play data that is in buffer
play(){
if(this.buffer){
var source = audioCtx.createBufferSource();
var tmp = this.buffer.getChannelData(0);
source.buffer = this.buffer;
source.connect(audioCtx.destination);
source.start(0);
console.log("play");
}
}
}
Okay, I have figured out what seems to be the problem. This code is currently in the testing phase and it seems to have something to do with the console being open on firefox that messes with sound being played. Although, it is not entirely consistent with how it behaves, at least I know the general cause of the problem I was having.
In other words, "audio" elements have no problems whether the console is open or not, but there seems to be undefined behavior when opening/minimizing the console with JavaScript controlled audio. However, it always behaves as expected when the console is closed.

How do we continue to be able to play in the array?

Making the audio player. The browser is not compatible tries to play the audio in the array using the embed tag. Continues to play and the next one is not to be played .Only one is played. I want to play the contents of the array in order.
var files = ['notibell_1.wav', 'notibell_2.wav', 'notibell_3.wav'];
var daudio = document.createElement('embed');
daudio.setAttribute('id', 'daudio');
daudio.height = "50";
daudio.width = "400";
daudio.controls = true;
daudio.autoplay = true;
daudio.type = "audio/wav";
for (var i = 0; i < files.length; i++) {
daudio.src = files[i];
}
bottom.appendChild(daudio);
This won't work because you are updating daudio.src 3 times before they are able to play the sound. I would recommend that you use the HTML5 native audio elements and have your sounds ready at least in mp3 and ogg.
If you want to play the contents of the array in order, you can use the ended event listener to reproduce the next sound:
Javascript
var files = ['notibell_1.wav', 'notibell_2.wav', 'notibell_3.wav'];
var i = 0;
audio = new Audio(files[0]);
//Let's add the ended event listener,
//change the source and play it.
audio.addEventListener('ended',function(){
if(++i < files.lenght){
audio.src = files[i];
audio.pause();
audio.load();
audio.play();
}
else{
audio.pause();
}
});
I haven't tested, but you can get the idea
You are creating one single audio element, assign all the properties, and then set the source in a loop, overwriting it 2 times. Your loop should contain the whole element creation and appension:
var files = ['notibell_1.wav', 'notibell_2.wav', 'notibell_3.wav'];
for (var i = 0; i < files.length; i++) {
var daudio = document.createElement('embed');
daudio.setAttribute('id', 'daudio');
daudio.height = "50";
daudio.width = "400";
daudio.controls = true;
daudio.autoplay = true;
daudio.type = "audio/wav";
daudio.src = files[i];
bottom.appendChild(daudio);
}
Now all 3 audio files play at the same time. One easy way to avoid this would be to make a playlist-file. .m3u is an easy format. Make another file called playlist.m3u:
notibell_1.wav
notibell_2.wav
notibell_3.wav
Then just make one audio element, giving it the playlist as a source:
var daudio = document.createElement('embed');
daudio.setAttribute('id', 'daudio');
daudio.height = "50";
daudio.width = "400";
daudio.controls = true;
daudio.autoplay = true;
daudio.type = "audio/wav";
daudio.src = 'playlist.m3u';
bottom.appendChild(daudio);

Add javascript to pdf file using iTextSharp

I want to embed a javascript snippet inside of a pdf file so that it will immediately print when it's opened from a browser window. To try and achieve this I'm following this example here.
I have created a helper class that has a static method to handle this task. I already have the pdf file path string ready to pass into the method. What I don't understand is how the output stream portion of this works. I would like the updated pdf to be saved to my servers hard drive. I do not want to stream it back to my browser. Any guidance would be greatly appreciated.
public class PdfHelper
{
public static void AddPrintFunction(string pdfPath, Stream outputStream)
{
PdfReader reader = new PdfReader(pdfPath);
int pageCount = reader.NumberOfPages;
Rectangle pageSize = reader.GetPageSize(1);
// Set up Writer
PdfDocument document = new PdfDocument();
PdfWriter writer = PdfWriter.GetInstance(document, outputStream);
document.Open();
//Copy each page
PdfContentByte content = writer.DirectContent;
for (int i = 0; i < pageCount; i++)
{
document.NewPage();
// page numbers are one based
PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
// x and y correspond to position on the page
content.AddTemplate(page, 0, 0);
}
// Inert Javascript to print the document after a fraction of a second to allow time to become visible.
string jsText = "var res = app.setTimeOut(‘var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.full;this.print(pp);’, 200);";
//string jsTextNoWait = “var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.full;this.print(pp);”;
PdfAction js = PdfAction.JavaScript(jsText, writer);
writer.AddJavaScript(js);
document.Close();
}
}
For how to accomplish this task, please take a look at this and this SO posts.
Basically you should have something like this:
var pdfLocalFilePath = Server.MapPath("~/sourceFile.pdf");
var outputLocalFilePath = Server.MapPath("~/outputFile.pdf");
using (var outputStream = new FileStream(outputLocalFilePath, FileMode.CreateNew))
{
AddPrintFunction(pdfLocalFilePath, outputStream);
outputStream.Flush();
}

Categories