Appcelerator - File storage fail - appcelerator

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"));

Related

Indesign extended Script unable to create a file in Mac OS 10.16

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?

JXA: Create a mailbox in Apple Mail

I am trying to create a sub-mailbox in Apple Mail using JavaScript.
I have the following code snippet (parent is a reference to the mailbox in which I want the new mailbox):
var mb = mail.Mailbox({name: "SubFolder"});
parent.mailboxes.push(mb);
The events log shows:
app = Application("Mail")
app.mailboxes.byName("Local").mailboxes.byName("Archive").mailboxes.push(app.Mailbox({"name":"SubFolder"}))
--> Error -10000: AppleEvent handler failed.
What am I doing wrong?
Thanks,
Craig.
Code now:
var mb = mail.Mailbox({name: "Local/Archive/Test Archive/SubFolder"})
logger.logDebug("mb = '" + Automation.getDisplayString(mb) + "'.");
mail.mailboxes.push(mb) // create the subfolder
This works as long as there are no spaces in the path.
I tried to force the space using \\ in front of it, but then you get "Test\ Archive" as the name.
So how do I get a space in the name to work?
Thanks.
To create a sub-folder, you need a name like a posix path --> "/theMasterMailbox/subMailBox1/subMailBox2/subMailBox3".
So, you need:
A loop to put the name of each parent folder into an array.
Use join('/') to join the elements of an array into a string.
Use mail.mailboxes.push(mb) instead of parent.mailboxes.push(mb)
Here's a sample script which creates a mailbox named "SubFolder" in the selected folder (the mailbox):
mail = Application('com.apple.Mail')
parent = mail.messageViewers()[0].selectedMailboxes()[0]
mboxNames = [parent.name()]
thisFolder = parent
try {
while (true) { // loop while exists the parent folder
mboxNames.unshift(thisFolder.container().name()) // add the name of the parent folder to the beginning of an array
thisFolder = thisFolder.container() // get the parent of thisFolder
}
} catch (e) {} // do nothing on error, because thisFolder is the top folder
mboxNames.push("SubFolder") // append the name of the new subFolder to the array
mBoxPath = mboxNames.join('/') // get a string (the names separated by "/")
mb = mail.Mailbox({name:mBoxPath})
mail.mailboxes.push(mb) // create the subfolder

Appcelerator Ti.Blob text property is null

I came across a strange issue while reading files from Ti.Filesystem.applicationDataDirectory. Whenever I want to access the text property of the Ti.Blob returned by Ti.Filesystem.File.read() I get null
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'foo.key');
var contents = f.read();
Ti.API.debug('contents: ' + JSON.stringify(contents));
var text = contents.text;
Ti.API.debug('text: ' + JSON.stringify(text)); // is NULL
The file was created like so
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'foo.key');
f.write(JSON.stringify({foo: 'bar'});
I'm developing on Android by the way with 6.0.1.GA
TL;DR it's the file extension that's causing the problem
When looking at the Ti.Blob more thoroughly I found that it says mimeType : application/gpg-keys. After updating my code above so it uses foo.json as filename everything works as expected.

check if a file exists in a folder actionScript 3

I would like to check if the file exists and if not display noimage.gif. The below is returning false in the console, can you help?
...
[Bindable]
private var imageName:String;
imageName = "pca" + this._product.productID + ".jpg";
var fileName:File = new File()
fileName.nativePath = "C:/STSMultimediaSync/market/pictures/{imageName}";
trace(fileName.exists);
....
PS - I know the file exists as when I hard code the image name, the value changes to true.
I don't think you can do {} inside AS3 (It can be done in mxml). Try :
fileName.nativePath = "C:/STSMultimediaSync/market/pictures/"+imageName;

exportDocument() 'destination folder does not exist' error

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

Resources