I have a file that's in my NSBundle, and I'd like the user to be able to save it anywhere they'd like on their computer. How would I be able to do that? I'm using Xcode 8.1, Swift 3, and making a macOS Cocoa application. Thanks for your help!
Use NSSavePanel to obtain the destination URL from the user
Use NSBundle to obtain the source URL in your app bundle
Use NSFileManager to copy the file
If once you've read the documentation, or written some code, you get stuck ask a new question showing where you've got to and what your problem is.
HTH
Related
I'm just wondering is it possible?
Let's say I wrote an app and I want to use a button to open (from the app I wrote) another app (sing its directory) on my Mac
Any idea where to start off? I've been thinking about NSFileManager but it seems like a wrong way.
You should use the methods of NSWorkspace. Which method exactly depends a bit on exactly what you want to do. For example, if you have the URL for a document file and you want to open it in the appropriate app, you would use -openURL:.
If you just want to open a specific app (and not any particular document), then you should use -launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier:. Using a bundle identifier is the most reliable way to identify the app, rather than using its name or its URL, either of which can be changed.
I am working on a Mac app. I ultimately want to use default app icons within my app. From the Info.plist and the Resource folder of an app I can get the .icns file and convert that to the image format I need. But I need to know the default application associated with the particular file extension, if any.
So how to get the default application that the system currently associates with a given file extension?
Don't go digging in other apps' bundles. It's always best to work at the level of abstraction that suits the question you want to ask. If you want to get the icon that the Finder (or a Mail attachment, etc) would display for a file of a particular type, use the NSWorkspace iconForFileType: method.
I think what you're looking for is part of the OSX Launch Services: LSCopyDefaultApplicationURLForContentType API. This returns the info on apps that can open specific Uniform Type Identifiers. There's also a similar API called LSCopyDefaultApplicationURLForURL to check which app opens a specific known file.
I am writing an app for Maverick.
The app creates a folder under /user/document, named "folder.db".
All the user related files will be in a folder "folder.db".
I would like to associate my app with "folder.db" directly, so that clicking on it would open my app and not the Finder.
How to achieve that?
Note: I tried to play with the UTI settings in xcode but not luck...
First .db is generally used for databases. So probably not a good idea. What you are looking for is a package or bundle in Cocoa terms. In Cocoa you want to look for the fileWrapper methods. Those create package/bundle files that are folders with a special flag bit set to make it act like an opaque file in Finder
You might want to study NSWorkSpace, NSFileManager, NSBundle, NSDocument and NSOpenPanel and NSSavePanel.
Those will get you on the path.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Mac OS X: Where should I store save games for a game delivered as a bundle?
i am migrating my iOS game to mac. The file I used to store high scores is saved under xxx.app/Contents/Resources. However, when I enable the sandbox and entitlement, the directory becomes unwritable. I tried xxx.app and xxx.app/Contents as well without luck.
so which directory should I use or which entitlement should I add?
EDIT
I have tried ~/Library/Application Support/Your Game Name/ and it does not show permission errors but unable to save the game data. there is no Library under user/my_name, but one under unser/shared. Do i need to add any entitlements?
EDIT 2:
Please dont close this question. The answer to earlier question does not work for me. Instead, the flagged answer of this question is correct.
Under app sandboxing your app has a container folder in which it can write pretty much anywhere, however I would recommend just writing to the Data/Documents folder, which can be obtained using the following code:
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
This will give you the folder at ~/Library/Containers/com.yourdomain.yourapp/Data/Documents. Note that the ~/Library folder is hidden under Lion/Mountain Lion so needs to be unhidden using this on the command line:
$ chflags nohidden ~/Library
(Only you, the developer, needs to unhide that folder so you can poke around from Finder to see what your app has written; your user's won't need to unhide it to use your app).
My mac os app get a NSURL of alias by user interaction (drag & drop), so the app have the permission to read the alias file, but it doesn't have permission to read the origianl file within app sandbox (Mac OS X 10.7/8).
I resolve the alias by
NSData* bookmark = [NSURL bookmarkDataWithContentsOfURL:aliasURL error:nil];
origURL = [NSURL URLByResolvingBookmarkData:bookmark
options:NSURLBookmarkResolutionWithoutUI
relativeToURL:nil
bookmarkDataIsStale:nil
error:&error];
When I try to read origURL file, I get the error: The file couldn’t be opened because you don’t have permission to view it.
I aslo tried call the start/stopAccessingSecurityScopedResource on the origURL but no help.
I also tried resolving bookmark data with NSURLBookmarkResolutionWithSecurityScope option, but get "The file couldn’t be opened because it isn’t in the correct format." error from URLByResolvingBookmarkData method.
So, How do it? Thanks.
I haven't tried this, but I think I might have an idea what's happening. The way OS X punches through the sandbox with drag-and-drop is by granting the app the files are dropped onto access to the dropped files until the app quits. This works using the plain NSString file paths on the pasteboard, so it does not rely on the security scoping mechanism.
Your app probably has access to the alias file, but only that file, not the one to which it refers. The sandbox hole-punching mechanism probably doesn't follow the alias and grant access to the underlying file. If you can get the path of the file to which the alias points (and I'm not sure that's possible), you can get around the sandboxing by prompting the user to select that file in an NSOpenPanel. That's another way of punching through the sandbox, using what Apple calls the "Power Box".
For more information on how to do this, check out the answer I wrote here: https://stackoverflow.com/a/11786156/105717. It links to another answer, then adds some helpful niceties to make what's happening clearer to the user.
Maybe, just maybe my similar situation and solution will help:
Have you definitely got the entitlement "com.apple.security.files.bookmarks.app-scope" set to "yes" in your entitlements file?
"The file couldn’t be opened because it isn’t in the correct format." I was getting this same error when trying to resolve the bookmark, that turned out to be the fact that the file was locked in Finder (do a 'get info' on the file and check the 'locked' box is off) so the security data was never generated in the first place.
Hope there's something in there to help!
Todd.