I'm trying to make a script in photoshop that will modify some layers and than export them as a PNG image. I've copied the following code from another place:
function SavePNG(saveFile){
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = true;
pngOpts.quality = 100;
activeDocument.exportDocument(saveFile,ExportType.SAVEFORWEB,pngOpts);
}
The function export the active document of photoshop to the file specified by the saveFile parameter.
It's working fine with simple paths like "C:\images\result.png" but when trying with different paths like "~/Desktop/" or paths with some special characters the file isn't exported, and a "destination folder does not exist" error message appears.
Any idea how can I solve it?
Well, I'm not sure why this is occur but you could try the following modification:
function SavePNG(saveFile){
var tmpFile = "./tmp.png";
tmpFile = new File(tmpFile);
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = true;
pngOpts.quality = 100;
activeDocument.exportDocument(tmpFile,ExportType.SAVEFORWEB,pngOpts);
tmpFile.rename (saveFile);
tmpFile.changePath(saveFile);
}
it'll export the file into a temporary file and then rename & move it to the requested path, should solve the path problem.
exportDocument expects a full file name, not a folder path.
This works:
activeDocument.exportDocument(new File("~/foo/foo.png"), ExportType.SAVEFORWEB, pngOpts);
This doesn't work and gives the 'destination folder does not exist' error message:
activeDocument.exportDocument(new File("~/foo/"), ExportType.SAVEFORWEB, pngOpts);
For people having this error and not using photoshop-script.
The error might be unbound to the destination folder, but occurs because the folder, which was used for the export step, is deleted. So either
recreate the folder, which was used during recording, or
recreate the export step
Related
I have a jsx script which i use for indesign scripting. The function below is used to write to a file. It works perfectly in windows. In mac it is not able to create not read the file from applications folder. Does it have to do with permissions in mac??
i did tried with applescript to create a file. The code worked fine without errors but the file is not been created. same with the below function. The code works perfectly. but I am not able to create the txt file inside the folder.
fileobject = "applications/foldername/filename.txt"
filecontent = "text"
function writeToFile(fileObj, fileContent){
encoding = encoding || "utf-8";
fileObj = (fileObj instanceof File) ? fileObj : new File(fileObj);
var parentFolder = fileObj.parent;
if (!parentFolder.exists && !parentFolder.create())
throw new Error("Cannot create file in path " + fileObj.fsName);
fileObj.encoding = encoding;
fileObj.open("w");
fileObj.write(fileContent);
fileObj.close();
return fileObj;
}
I've tried this on MacOS 10.13 and it works fine:
function writeToFile(fileObj, fileContent) {
encoding = "utf-8";
fileObj = (fileObj instanceof File) ? fileObj : new File(fileObj);
var parentFolder = fileObj.parent;
if (!parentFolder.exists && !parentFolder.create())
throw new Error("Cannot create file in path " + fileObj.fsName);
fileObj.encoding = encoding;
fileObj.open("w");
fileObj.write(fileContent);
fileObj.close();
return fileObj;
}
writeToFile('/Applications/folder/filename.txt', 'text');
Basically it should work on 10.16 as well. If it doesn't it could have to do with security settings. To save user files into system folders /Applications etc is not a good idea.
Could you try to save a file into user folders: ~/Desktop or ~/Downloads?
I copy a video into /Documents/videos/xxxxxx.mp4 and then, when I close the app and restart it, this error appear when I want to get the file:
nativeReason = "Could not open file stream for file at path: /var/mobile/Containers/Data/Application/055CE45C-28EC-46F5-9609-F16E357B682E/Documents/videos/1458044667778.mp4\nFile does not exist at path /var/mobile/Containers/Data/Application/055CE45C-28EC-46F5-9609-F16E357B682E/Documents/videos/1458044667778.mp4";
But I can see this file at the directory:
Whats the problem?? This occur on iOS, Ti.SDK 5.2.0.GA
My code:
if(OS_IOS){
Ti.API.debug("Media: " + this.get("media").nativePath);
var infile = Ti.Filesystem.getFile(this.get("media").nativePath); //!
}else{
var infile = Ti.Filesystem.getFile(this.get("media"));
}
Ti.API.debug('infile: ' + infile.exists());
var tempFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "videos/");
if(!tempFile.exists())
tempFile.createDirectory(); //create videos directory
var tempFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "videos/" + new Date().getTime() + ".mp4");
Ti.API.debug('tempFile ' + tempFile.exists()); // tempsFile is always empty
if(OS_IOS){
if (infile.exists() && (!tempFile.exists()) ) { //copy infile to videos/
tempFile.write(infile.read());
}
}else{ //Android
infile.copy(tempFile.nativePath); //copy infile to videos/
}
//Get video, the path saved is correct
var vid = Ti.Filesystem.getFile(video.get("videoFile"));
By default Ti.Filesystem.getFile() all relative paths are currently interpreted as relative to the Resources directory, not to the current context. This is a known issue that will be addressed in a future release. : http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Filesystem-method-getFile
So, you need to save the video file name in video.get("videoFile") and get the video this way : Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "videos/"+video.get("videoFile"));
I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
If the user's company name is Foo, I want this when they save save their uploaded image:
/public/company_img/foo/<filename>.jpg
This is in my controller:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas
File::exists($path) or File::makeDirectory($path);
This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )
I would do something like that:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.
Do you have any sample codes or functions to check if an image name is existing already in the folder before uploading?
I've tried using file_exists() but it doesn't work, here is my sample code:
$path = FCPATH . "images2/";
$filename=$_FILE['userfile'];
$full_path = $path .$filename;
if(file_exists($filename))
{
///display error message///
}
Here is the simplest way to check if a file exist:
if(is_file($filename){
return true; //the file exist
}else{
return false; //the file does not exist
}
I'm assuming you are not getting the correct result with file_exists() because you don't include the full path (even tho you define it).
Try using the following: file_exists($full_path)
Also consider using some CI helper functions for handling files like images, or uploads. They are there to make this 'easier'.
File helper:
http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html
I am having a problem in matlab and the problem is described as follows:
When i try to read an image ( I have several images) and write them to a specific folder, the matlab triggers an error saying
Error using ==> imwrite at 394
Can't open file "\Temp\\inim735282.4716703009300000.jpg" for writing.
You may not have write permission.
May I know why this is happening?
this is the code where the problem occurs
mkdir('.\Temp');
temp_name = sprintf('%.16f',now);
corner_file = ['\Temp\corners', temp_name,'.in'];
image_file = ['\Temp\inim', temp_name,'.jpg'];
out_file = ['\Temp\out', temp_name,'.desc'];
out_imname = ['\Temp\out', temp_name,'.desc.jpg'];
I tried to change it by omitting
mkdir('.\Temp');
moreoever, i direct the path in the folder to the folder by doing this
binary_path = 'C:\Users\cool\Documents\MATLAB\Experment\experiments\bag_of_words\Temp';
to read and and write in and out of the folder.
Can someone please help me figure out this problem?
Thank you guys
Open MatLAB with admin privileges.
A few suggestions:
To generate a temporary output name use the command tempname.
temp_name = tempname();
To concatenate paths and file names use fullfile.
conrner_file = fullfile( '\', 'Temp', 'corners', [temp_name, '.in'] );
You should be careful not to mix '\Temp' and '.\Temp': as the first is an absolute path, while the second is a relative path to cwd.
EDIT:
How about:
temp_name = tempname(); % temp name + folder name in TEMP
corner_file = [ temp_name,'.in'];
image_file = [ temp_name,'.jpg'];
out_file = [temp_name,'.desc'];
out_imname = [temp_name,'.desc.jpg'];
Is it working now?