Open file with app as duplicate - macos

The idea is that I have a template of a file that needs to be opened but it should behave as if only the contents of the file were opened instead of the file itself.
The difference for the user should be, that he needs to select a place to save the file to instead of just overwriting the file that was opened.
Similar behaviour can be seen in the Preview app. When duplicating a function Preview will open the same file in a new window. Upon saving the file the user needs to specify where to save it to.
As far as I can see NSWorkspace does not support such behaviour out of the box. Does anybody know a workaround for that? I'd appreciate it!
EDIT
In my case I don't know where the user wants to save the file to when he or she is done with it. Currently I am only copying the file to a hidden folder in the user's home directory and then I open the copy so that the original file can not be overwritten.
The resulting behaviour is:
- the user does not see where the file is located
- upon saving and closing the application that the file was edited in the user will not find the file ever again.
Workaround: I guess for now I will ask the user where she wants to save it before opening it, which seems kind of redundant to me as it could very well be, that she will not want to save it. Hopefully I will find a better solution soon.

You may be able to achieve this by sending an "open contents" ('ocon'/kAEOpenContents) Apple Event to the target app. The easiest way is probably to use the Scripting Bridge.
The normal way to use the Scripting Bridge requires that you know in advance which app you want to target and generate an Objective-C header from its scripting interface definition. However, you should be able to use it "raw". For example, something like this:
SBApplication *app = [SBApplication applicationWith...:...]; // there are method to take a PID, a bundle ID, or a URL
[app activate];
NSAppleEventDescriptor *desc = [NSAppleEventDescriptor descriptorWithString:#"foobar"];
[app sendEvent:kCoreEventClass id:kAEOpenContents parameters:keyDirectObject, desc, nil];
The NSAppleEventDescriptor object is the contents payload, in this case the string "foobar". You can use other methods to create different types of payloads. If you use the generic one and need a descriptor type, you can look in the headers in /System/Library/Frameworks/CoreServices.framework/Frameworks/AE.framework/Headers (e.g. AEDataModel.h or AERegistry.h).

Related

Opening an app using another app

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.

Securely delete NSURL file and show percent in NSProgressIndicator

I am trying to create a "document shredder" application for Mac. I have the NSURL of the files that the user selected, but how would I go about securely deleting said files and passing the progress along to a progress bar?
You want to look at the posix layer calls for file I/O, with those you can overwrite files and implement any algorithm for secure deletion you choose.
If you just need to securely delete the file and are not concerned over showing progress in a GUI you can invoke the srm (secure remove) command using NSTask. For details on the srm enter man srm in a Terminal window.
If you do want to show progress in a GUI then you can convert the output of that command, captured with NSTask and display it as you choose. Or Apple provide the source to srm, you can find it here on the Apple Open Source site and you can incorporate code/algorithms from that into your own code and provide progress indication in your GUI.
HTH
There is no direct way to "securely delete" files from the Cocoa API.
You can use NSFileManager to move files into the trash can, which then leaves it up to the user to delete, or securely delete, the files via the Finder menu.
You could also use NSFileManager to move files into the trash and then use AppleScript to send Finder a message to delete/securely delete files, however, this is generally not a good idea unless your user has specifically requested you empty their trash as you may delete things they are not ready to have deleted.
Otherwise, there does not seem to be an easy or direct way to accomplish a secure delete of files as with the standard API there is no guarantee that the bytes you write out to disk are going to overwrite the ones that existed before.
You may also want to check out this previous question here on SO in relation to why secure delete is not that secure:
how to write back to an existing file, ensuring the bits on the disk get overwritten in OS X

NSWorkspace notification - application close [duplicate]

My requirement is to open a file like pdf/ppt etc and close it after 20 minutes. I am able to open the files appropriately thanks to NSWorkspace. But how can I close the files ?. NSWorkspace does not seem to have a close method.
NSWorkspace just asks the operating system to open the file, it doesn't maintain any connection to the file at all. The OS will open the file with whatever the default application is for that file type, unless you specify a particular application to use. Anything that happens with the file after that point is completely outside of your application's control because the file is being managed by another process.
You would need to use either AppleScript/Apple Events or the Accessibility Framework to control whatever application currently has the file open and ask it to close the file, but there is no guarantee that it will do so.
The only way to guarantee that you will be able to close the file is to open it in your app. Obviously, this is impossible if you need to open a variety of diverse and possibly proprietary file types.

File extension icon on Mac

My application uses new proprietary file formats with extensions never been used before. I would like to associate specific icons to display my files in finder with nice iconography. As far as I know LaunchService is responsible to handle all these data, however I'm confused where, when and how shall I create associations.
Which entries I have to add to plist?
Where I need to actually register this extension - during installation? Is there any script for this?
Add a CFBundleDocumentTypes key to your plist, see
Storing Document Types Information in the Application's Property List

I want to implement a File Explorer, How can I read file's icon and display them?

I want to implement a File Explorer, It is different from a normal file explorer, it first scan file system and store information into database, them read file system information from Database, and display them, I need to store the icons of the file, how can I do this? Is there any Windows APIs?
Many thanks!
SHGetFileInfo can get a handle for the icon of a file.
You need to use SHGetFileInfo. This looks like a pretty good example for .NET.

Resources