Is it possible to use the alert Finder provides when moving a file to a directory where a file with that name exists in my own app? - swift

I'm building a mac-app for people to organise files. One of the things I allow people to do is to move or copy files to specific directories from within my app. I use FileManagers 'moveItem(at:to:)' or 'copyItem(atPath:toPath:)' to do so, which also allows me to catch errors. Based on these errors, I can of course create custom alerts for specific edge cases.
However, since Finder already provides alerts for such occasions, I was wondering if I could prompt Finders default alerts instead of having to re-create them.
I'm especially interested in the alert shown when multiple files already exist at the location the user tries to move/copy files to and Finder allows to 'Keep both', 'Stop' or 'Replace' these files. Because this alert also includes a 'Apply to all'-checkbox unusual place for a custom NSAlert.
Thanks!

You can use apple script to move file.
tell application "Finder"
move POSIX file "/Users/xyz/fileName" to POSIX file "/Users/xyz/test"
end tell
This will display finder alert.

Related

How do you create files in arbitrary locations using Swift?

How do you go about creating files at arbitrary locations given you have the absolute path? I want to create a file in some arbitrary location /Users/me/random/project3/<moreStuffInThePath. How would I go about doing this?
To create a directory I would do:
try FileManager.default.createDirectory(atPath: "/Users/me/random/project3/<directoryName>"
This worked fine for directories.
However the counterpart for creating files does not work:
FileManager.default.createFile(atPath: "Users/me/random/project3/something.txt", contents: someString.data(using: .utf8)) // always returns false
I have checked out other stack overflow threads on creating files. Everything I found was about creating files in the .documentDirectory or it was outdated.
Eg: How to create text file for writing
Create empty file swift
Read and write a String from text file
TLDR: How do I create text files/files with random extensions at arbitrary locations on the pc given I have the absolute path?
I would also be grateful if someone could explain why all tutorials available on this matter are about the document directory. Why is it so significant?
Thank you!
This is most likely because your app is sandboxed.
Go to your project settings, select the macOS target and go to "Signing and Capabilities". You should see something like this:
If your app needs to create files in arbitrary locations, you must remove the app sandbox by clicking on the "x" on the top right. Note that this will cause your app to be rejected from the Mac App Store.
If you want to upload your app to the Mac App Store, then you must keep the sandbox, and write to "user selected locations" only. Select "Read/Write" rather than "Read Only" next to "User Selected File". (Although it says "File", this includes directories too) This allows you to write to locations chosen by a user using something like an NSSavePanel.

Is it possible to create a virtual editor in an extension

I want to process lots of files in my extension and editors have lots of useful methods that I need to use to process the content of a file with, but I don't want to open each file individually in VSC.
I have been able to read the files I plan on editing but how can I create an editor?
Can this be done?
Have you tried vscode.window.openTextDocument()?
openTextDocument(uri: Uri): Thenable<TextDocument>
Opens a document. Will return early if this document is already open. Otherwise the document is loaded and the didOpen-event fires.
That does not imply showing the editor, which is done with a separate vscode.window.showTextDocument() call, so it should qualify as "virtual".

How to get notified if a file is opened

So if I have a directory /dir which can contain any number of files N and these files can be in any sub-directory in /dir. How can I watch /dir such that I get notified when a file in dir or any of its sub-directories is opened? I don't want to watch all files and check if a lock is acquired for that file.
I have looked at FSEvents, but I am pretty sure that I cannot do it with that.
It is for the macOS operating system
So can anyone point me in the right direction or know a solution
One method would be to write a Kernel Extension and use Apple's Kernel Authorisation (KAuth) framework.
You'd then subscribe to the File Operation scope and the KAUTH_FILEOP_OPEN action.
This would also require a user-land application to communicate with the kernel extension, to receive the notifications of file operations, so this method would depend upon your requirements for watching the files as to whether or not this is just overkill.
You could use kqueue, and based on the events that you would like to monitor you could get "notified", for example in your case you could use the EVFILT_VNODE filter:
EVFILT_VNODE Takes a file descriptor as the identifier and the
events to watch for in fflags, and returns when one
or more of the requested events occurs on the
descriptor.
To get a list of all events you could monitor check the man: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 (The implementation is pretty similar in all the BSD's including macOS), check this answer to see some differences: https://stackoverflow.com/a/49521218/1135424

With Chrome Apps soon to be removed, is there another way to edit a local file?

The Chrome Apps API has the very useful FileSystem API which allows a user to select a file for an app to edit (read and write changes to). However, with the entire Apps API soon to be removed, what other ways exists to edit a file on the local file system?
This is not an opinion-based question, I am asking for all conceivable alternatives.
Per https://developers.chrome.com/apps/migration:
Q: My app uses the chrome.fileSystem API to read and write user-specified files and / or directories. Can this be done on the open web?
A: In general, no. The open web can read single files that the user opens, but cannot retain access to those files, write to those files, or have any access to directories.
If it is critical for your app to read and write directories (e.g. it is a text editor with a folder view), you will need to either have a native helper app and extension combo, or create a native app.

how to create multiplatform file browser in ionic

I need help. I'm trying to create a multiplatform mobile application. One of the functionalities is a local file browser that will allow user to select a directory and create a list of file in this directory. I've already spent couple of hours on that issue and I can not find any solution for that.
I've checked http://ngcordova.com/docs/plugins/file/
But if I understand it correctly it is used when I already have a file, it not allows user to pick the directory.
I don't need anything fancy, it should as simple as possible
The Cordova FileSystem plugin (and ngCordova's wrapper) provide the ability the read directories from the file system. They do not provide any UI, so you would use the plugin to ask for a list of directories and then render it as you see fit. (A UL, a table, whatever.) To "browse" you could use a click handler on the list of directories such that when clicked, you then get the files/subdirectories of that folder. Again, the FS plugin adds support for doing that.