How to get list of shared folders in Swift

Hi,

I need to detect in my MacOS app, that dropped file on a canvas belongs to shared folder.

The only way which I have found was calling a command inside terminal:
Code Block
sharing -l

so I have called such command in swift and parsed output.

This solution works fine but I have noticed that your user needs to have Admin rights.

Is there any solution to gather list of shared folders also when user has Standard account (without admin rights)?

It would be great if some framework method exists (calling terminal command is not my preferred way).

calling terminal command is not my preferred way

Agreed. I recently wrote up my thoughts on this as part of a DTS incident and figured I should share them here:

To start, I want to be clear that DTS strongly discourages folks
from using command-line tools as API. Command-line tools are intended
to be used by the user (or a site admin) and their user interface (the
arguments they accept and the output they print) is tailored for that
audience. If you use a command-line tool as an API then you run the
risk that changes to the tool’s UI will break your code.

If the available APIs are lacking and you resort to using a
command-line tool as an API, make sure to file a bug requesting
a replacement API. In your bug make it clear that you intend to work
around this limitation by using a command-line tool as API, and then
outline the specific command and arguments you intend to use.

File a separate bug for each missing API and [post] the resulting bug
numbers.



As to your main problem, that info is held in Open Directory and you can find it with code like this:

Code Block
import OpenDirectory
let session = ODSession.default()
let node = try ODNode(session: session, type: ODNodeType(kODNodeTypeLocalNodes))
let query = try ODQuery(
node: node,
forRecordTypes: kODRecordTypeSharePoints,
attribute: nil,
matchType: ODMatchType(kODMatchAny),
queryValues: nil,
returnAttributes: kODAttributeTypeAllAttributes,
maximumResults: -1
)
let results = try query.resultsAllowingPartial(false) as! [ODRecord]
for r in results {
let v1 = try r.values(forAttribute: kODAttributeTypeRecordName)
print(v1)
let v2 = try r.values(forAttribute: "dsAttrTypeNative:directory_path")
print(v2)
print("--")
}


A typical result looks like this:

Code Block
--
[Quinn’s Public Folder]
[/Users/quinn/Public]
--


The only gotcha here is the dsAttrTypeNative:directory_path attribute identifier. OD’s well-known attributes are defined in the OD framework headers. For example, kODAttributeTypeRecordName is defined in <CFOpenDirectory/CFOpenDirectoryConstants.h>. Stuff that’s not defined there is less reliable. Technically it’s undocumented, which means you have to think carefully about whether you want to take a dependency on it. In this case I think it’s reasonable to do so — that attribute has been around for a very long time and I can’t think of any reason it’d go away — but it’s always a concern.

Regardless, make sure you code defensively here. When unpacking the attribute value, handle the cases where:
  • The value is missing (the values(forAttribute:) method will throw in that case)

  • The resulting array is empty

  • An array item is not the type you expect

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
How to get list of shared folders in Swift
 
 
Q