Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSFileManager with authorization


Required plugins for this example: MBS MacBase Plugin, MBS MacCocoa Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSFileManager with authorization

This example is the version from Thu, 31th May 2023.

Project "NSFileManager with authorization.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() // ask to be allowed to replace a file workspace.requestAuthorization(workspace.NSWorkspaceAuthorizationTypeCreateSymbolicLink) End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() // ask to be allowed to replace a file workspace.requestAuthorization(workspace.NSWorkspaceAuthorizationTypeReplaceFile) End EventHandler
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() // ask to be allowed to replace a file workspace.requestAuthorization(workspace.NSWorkspaceAuthorizationTypeReplaceFile) End EventHandler
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
EventHandler Sub Open() workspace = New MyNSWorkSpaceMBS #If False Then // read some attributes Dim filemanager As New NSFileManagerMBS Dim source As FolderItem = GetFolderItem("/usr/local/bin/ps2ps2", FolderItem.PathTypeShell) Dim Error As NSErrorMBS Dim d As Dictionary = filemanager.attributesOfItemAtPath(source, error) Break #EndIf End EventHandler
Note "Entitlements required"
com.apple.developer.security.privileged-file-operations see https://developer.apple.com/documentation/appkit/nsworkspace/authorization
Property workspace As MyNSWorkSpaceMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyNSWorkSpaceMBS Inherits NSWorkSpaceMBS
EventHandler Sub requestAuthorizationCompleted(type as Integer, authorization as NSWorkspaceAuthorizationMBS, error as NSErrorMBS, tag as variant) If error <> Nil Then MsgBox error.LocalizedDescription End If If authorization <> Nil Then Dim fm As NSFileManagerMBS = NSFileManagerMBS.fileManagerWithAuthorization(authorization) MsgBox "got file manager with authorization"+EndOfLine+Str(fm.Handle) // now do something non evil! Select Case type Case NSWorkspaceAuthorizationTypeCreateSymbolicLink // e.g. create symbolic link to our file Dim source As FolderItem = SpecialFolder.Desktop.Child("test.txt") Dim b As BinaryStream = BinaryStream.Create(source, True) b.Write "Hello World" // now move to some place you may normally not be allowed to move it Dim linkfile As FolderItem = GetFolderItem("/usr/local/test.txt", folderitem.PathTypeNative) Dim result As Boolean error = Nil result = fm.createSymbolicLink(linkfile, source, error) If result Then Call NSWorkspaceMBS.selectFile(linkfile) MsgBox "Success" Else MsgBox error.LocalizedDescription End If Case NSWorkspaceAuthorizationTypeSetAttributes // e.g. make file for root Dim source As FolderItem = SpecialFolder.Desktop.Child("test.txt") Dim b As BinaryStream = BinaryStream.Create(source, True) b.Write "Hello World" Dim dic As New Dictionary dic.Value(fm.NSFileOwnerAccountID) = 0 dic.Value(fm.NSFileGroupOwnerAccountID) = 0 dic.Value(fm.NSFilePosixPermissions) = &o755 Dim result As Boolean error = Nil result = fm.setAttributes(dic, source, error) If result Then MsgBox "OK" Else MsgBox error.LocalizedDescription End If case NSWorkspaceAuthorizationTypeReplaceFile // e.g. move a file to a location Dim source As FolderItem = SpecialFolder.Desktop.Child("test.txt") Dim b As BinaryStream = BinaryStream.Create(source, True) b.Write "Hello World" // now move to some place you may normally not be allowed to move it Dim dest As FolderItem = SpecialFolder.Applications.Child("dest.txt") Dim result As Boolean Dim output As FolderItem error = Nil result = fm.replaceItem(dest, source, "", 0, output, error) If result Then MsgBox output.NativePath Else MsgBox error.LocalizedDescription End If End Select End If End EventHandler
End Class
End Project

The items on this page are in the following plugins: MBS MacCocoa Plugin.


The biggest plugin in space...