I'm using:
let dialog = NSOpenPanel()
to get a file URL.
I'm then reading in the contents of the text file with:
let content = try String( contentsOf: dialog.url)
This works!
I'm then trying to read in another text file in the same directory with a different extension:
let b = dialog.url?.deletingPathExtension()
// Add the new file extension
let c = b?.appendingPathExtension("TSN")
let content2 = try String( contentsOf: c)
With this I get:
"The file “FLO5.TSN” couldn’t be opened because you don’t have permission to view it."
If I try and open the .tsn file with a URL from a NSOpenPanel() dialog result it works. I need to open several data files from this same directory with different extensions. Is there a way to do this?
set off sandbox!!)
Xcode 9 and later enables sandboxing by default, which severely limits interactions with the system.
Select your target, then Capabilities and set Downloads Folder to Read/Write:
In my case, solve it's startAccessingSecurityScopedResource
Example:
let selectedFile = try result.get() // get path to file
do {
// get access to open file
if selectedFile.startAccessingSecurityScopedResource() {
let path = selectedFile.path
let data = NSData(contentsOfFile: path)
print(data) // array bytes
selectedFile.stopAccessingSecurityScopedResource()
}
} catch {
// Couldn't read the file.
print(error.localizedDescription)
}
Apple wiki about this feature https://developer.apple.com/documentation/foundation/nsurl/1417051-startaccessingsecurityscopedreso
Related
Is it possible to create a folder and have SpriteKit go through that folder, find each image (regardless of name), and create a SKShapeNode for each in that folder (regardless of how many there are)?
Assume all images are the same size, and all .png files ready to be used as unique images as textures for filling SKShapeNodes, and that all SKShapeNodes will be the same size.
I want to know, once done, how many shapes have been created, and have them uniquely named based on the name of the image of the texture used.
But all I'm reading seems to be about knowing how many images are there, and what their names are, before hand.
Clueless as to where to start searching about how to do this.
Update: no idea how to reference the "folder" with images:
I've dumped the images, with their very imaginative names [one, two, three, four, five etc. ], into a folder as "atlas", like this:
but as per Allessandro's wonderful answer, I have no idea how to address this "folder" and get at what's in it.
I think it's this line I'm getting completely wrong:
let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)
I don't know what to replace "Images" with.
Update 2:
Found a much easier way to do this:
let myTextureAtlas = SKTextureAtlas(named: "demoArt")
print(myTextureAtlas)
print(myTextureAtlas.textureNames)
Too many requests , so I try to help you with some code:
Create a folder:
func createDir(folderName:String) {
let fileManager = FileManager.default
if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") {
let newDirectory = directory.appendingPathComponent(folderName)
try! fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
}
}
Now you have your folder so you can decide to save a file or to copy one file from documents to this new directory. I make an example for the last one.
Copy a file:
let fileManager = FileManager.default
do {
try fileManager.copyItem(atPath: "hero.png", toPath: "subfolder/hero.swift")
}
catch let error as NSError {
print("Something went wrong: \(error)")
}
Now you want to know how to extract all files from a directory.
Extract all files from a directory:
func extractAllFile(atPath path: String, withExtension fileExtension:String) -> [String] {
let pathURL = NSURL(fileURLWithPath: path, isDirectory: true)
var allFiles: [String] = []
let fileManager = FileManager.default
if let enumerator = fileManager.enumerator(atPath: path) {
for file in enumerator {
if #available(iOS 9.0, *) {
if let path = NSURL(fileURLWithPath: file as! String, relativeTo: pathURL as URL).path
, path.hasSuffix(".\(fileExtension)"){
allFiles.append(path)
}
} else {
// Fallback on earlier versions
}
}
}
return allFiles
}
Usage:
let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)
let allPngFiles = extractAllFile(atPath: folderPath!, withExtension: "png") // returns file path of all the png files inside the folder
After that, you can create an array of SKShapeNode based from allPngFiles using fillTexture with a SKTexture created from each image and give to each node the name of the file used (so you know at the end also how many nodes you have created from array.count).
Slightly different answer here. I'm not sure why one needs to create a directory and copy files over. In your screenshot you have a demoArt.atlas with 7 files (one.png, two.png, etc). You indicate you want to generate an SKShapeNode. It still isn't clear to me why you want SKShapeNode, but ignoring that, you just need to know the folder name. You'll notice how the folder is blue, which means it is a reference. This means that in the image, the folder is retained.
This would get you all the files in that directory:
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let texPath = NSURL(fileURLWithPath: documentsPath).appendingPathComponent("demoArt.atlas")
let filemanager:FileManager = FileManager()
// Error check/ensure path exists
let files = filemanager.enumerator(atPath:texPath!.path)
while let file = files?.nextObject() {
print(file)
// Create your SKShapeNode here, you can load file from this
// This is all relative path, so "file" would be one.png, two.png, etc. If you need the full path, you
// need to prepend texPath
}
Note the key diff here is not copying files over. Maybe I just don't understand the problem and why it would require the copy.
If you wanted to put that in an array you can. But here you'll have the file name of the texture which you would of course need to load.
Realistically, you want to load those textures asynchronously first, then construct your SKShapeNode.
I am a novice user at ExtendScript, Basil.js & Javascript.
After seeing the project http://basiljs.ch/gallery/ending-the-depression-through-amazon/ I wondered if it is possible to simply get the Basil.js Script/ Extendscript/Indesign to grab images from a webpage and insert it directly into a indesign document.
I am a beginner so if someone had a complete script, from directory, functions... with example url, import, save, place.... ?
This is in the hope that I could simply put an address into the script and it would harvest all images on the webpage and put them into a indesign document?
I tried their example http://basiljs.ch/tutorials/getting-images-from-urls/
but when I copy that and run it in extend script it is just searching my computer...
UPDATE*
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basil/basil.js";
function draw() {
var url = "https://raw.github.com/basiljs/basil.js/master/lib/basil.png";
// download the url to a default location, filename according to url:
// -> "the project folder" + data/download/basil.png
//b.download(url);
// download url to a specific location in the project folder:
// -> "the project folder" + data/download_images_files/basil_logo.png
// b.download(url, "download_images_files/basil_logo.png");
// download url to a specific location e.g. to your desktop
// -> ~/Desktop/basil_logo.png
var newFile = new File("~/Desktop/basil_logo.png");
b.download(url, newFile);
}
b.go();
... It finds in Error in Basil's Core.js #
var runDrawOnce = function() {
app.doScript(function() {
if (typeof glob.draw === 'function') {
glob.draw();
}
}, ScriptLanguage.javascript, undef, UndoModes.ENTIRE_SCRIPT);
};
Console says...
Using basil.js 1.08 ...
sh ~/Documents/basiljs/bundle/lib/download.sh /Users/jamestk/Desktop (Address here, I don't have the sufficient reputation on stack overflow)
Basil.js Error -> b.shellExecute(): Error: sh: /Users/jamestk/Documents/basiljs/bundle/lib/download.sh: No such file or directory
The easiest way would probably be to us the b.shellExecute(cmd) command to run a shell script that downloads your images.
You could use wget from shell to download all images from an url, see this question: How do I use Wget to download all Images into a single Folder
You should make sure then to save the images in the documents data directory. After that, you could get all images by using the Folder object's getFiles() method:
var myImagesArray = Folder('~/path/to/my/folder').getFiles();
Finally you could use that array in a loop to place all the images into your document.
I have this code which prints out all files and directories for a given path. After reading the docs, I still need help to get my head around how to print out only files that do not end with .png
import Foundation
let filemanager: NSFileManager = NSFileManager()
let files = filemanager.enumeratorAtPath("/Users/empl1/Desktop/myApp2")
while let file = files?.nextObject() {
print(file)
}
Take a look at the String API documentation. You need to check if the string ends with the PNG file extension.
I've reformatted your code a little and got rid of manually going over the enumerator:
import Foundation
let filemanager: NSFileManager = NSFileManager()
let files = filemanager.enumeratorAtPath("/Users/empl1/Desktop/myApp2")
if files == nil {
print ("Could not get enumerator")
exit(-1)
}
for file in files! {
// check for extension is done here
if file.hasSuffix(".png") {
continue
}
print(file)
}
I am developing an app that updates the file /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist. This file is owned by root. I have am currently updating it with the /usr/libexec/authopen tool, running the tool with NSTask and NSPipe. Here is the code:
func saveAs(fname:String) {
// Create a dictory to write back out, and replace the knownNetworks and the preferredOrder
let d = NSMutableDictionary(dictionary:airport_preferences)
d[KnownNetworks] = networks
d[PreferredOrder] = preferred_order
let tempFileName = NSTemporaryDirectory() + "/preferences.new"
d.writeToFile(tempFileName,atomically:true)
let data = NSData(contentsOfFile: tempFileName)
let task = NSTask()
task.launchPath = "/usr/libexec/authopen"
task.arguments = ["-c","-w",fname]
let pipe = NSPipe()
task.standardInput = pipe
task.launch()
let old_signal = signal(SIGPIPE,SIG_IGN)
pipe.fileHandleForWriting.writeData(data!)
pipe.fileHandleForWriting.closeFile()
task.waitUntilExit()
signal(SIGPIPE,old_signal)
do {
try NSFileManager.defaultManager().removeItemAtPath(tempFileName)
} catch let error as NSError {
print(error.description)
}
}
To move to the App Sandbox, I am able to open the file by having the user select it in the browser. That's not hard, because I can pre-position the NSOpenPanel. Mac App Sandboxing- Updating files outside the sandbox describes how to update a file outside the sandbox, but not a file owned by root.
When I run my code under sandbox, I get this error:
tempfile: /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist length: 339710
arguments: Optional(["-c", "-w", "/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist"])
2015-09-26 11:46:07.699 WiFi Editor[94175:2887221] An uncaught exception was raised
2015-09-26 11:46:07.700 WiFi Editor[94175:2887221] Couldn't posix_spawn: error 1
2015-09-26 11:46:07.703 WiFi Editor[94175:2887221] (
(That code still has my debugging print statements in it.)
So how does one update a system file from the sandbox?
I'm afraid that's something that is not possible at all for an app that is sandboxed. Making the user paste text into a Terminal window isn't forbidden.
I'm trying to open a file in Windows Phone 7, but it says it doesn't exist. Here's the code I'm trying:
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
bool test = file.FileExists("\\ClientBin\\clubs.xml");
And in my project I added a folder called ClientBin, and the clubs.xml is in there. The clubs.xml file properties are:
Build action: Content
Copy to Output Directory: Copy always
I'm not sure what I'm doing wrong. You can see what I have in this screenshot.
Thanks!
When you ship a file with your application, it doesn't get stored in IsolatedStorage. You need use the conventional way of opening a file that ships with the XAP -
XDocument xdoc = XDocument.Load("ClientBin/customers.xml");
var customers = from query in xdoc.Descendants("Customer")
select new Customer
{
Name = (string)query.Element("Name"),
Employees = (int)query.Element("Employees"),
Phone = (string)query.Element("Phone")
};
// Data bind to listbox
listBox1.ItemsSource = customers;
HTH, indyfromoz