Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCF/Sandbox File Access/Sandbox Files
Function:
Required plugins for this example: MBS MacCF Plugin, MBS MacOSX Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCF/Sandbox File Access/Sandbox Files
This example is the version from Tue, 11th Jul 2016.
Project "Sandbox Files.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open() app.AutoQuit = true // let's see where documents folder is dim docFolder as FolderItem = SpecialFolder.Documents #if RBVersion < 2013 dim docPath as string = docFolder.UnixpathMBS #else dim docPath as string = docFolder.NativePath #endif List.AddRow "Document folder: "+docPath if docPath.instr("/Containers/") = 0 then MsgBox "Please sandbox this app." // run command in command.txt file. // you need to put your developer name in the command.txt file end if // now our pref file dim datafile as FolderItem = docFolder.Child("bookmark.pref") if datafile.Exists and datafile.Length>0 then List.AddRow "Second run." // read bookmark dim b as BinaryStream = BinaryStream.Open(datafile) dim BookmarkData as string = b.Read(b.Length) // resolve bookmark dim relativeURL as CFURLMBS = nil dim isStale as Boolean = false dim options as integer = CFBookmarkMBS.kResolutionWithoutUIMask + CFBookmarkMBS.kResolutionWithSecurityScope dim url as CFURLMBS = CFBookmarkMBS.ResolveBookmarkDataToCFURLMBS(BookmarkData, options, relativeURL, isStale) dim error as CFErrorMBS = CFBookmarkMBS.LastError if error<>nil then List.AddRow "Error: "+error.Description end if // got something? if url<>Nil then dim UrlString as string = url.str.str list.AddRow "URL: "+UrlString // start access if CFBookmarkMBS.StartAccessingSecurityScopedResource(url) then List.AddRow "StartAccessingSecurityScopedResource success." dim file as FolderItem = url.file #if RBVersion < 2013 dim filePath as string = file.UnixpathMBS #else dim filePath as string = file.NativePath #endif List.AddRow "File: "+filePath // now read file b = BinaryStream.Open(file) dim data as string = b.Read(b.Length) List.AddRow "Data read: "+str(lenb(data)) // stop access CFBookmarkMBS.StopAccessingSecurityScopedResource url List.AddRow "Success." else List.AddRow "StartAccessingSecurityScopedResource failed." end if else List.AddRow "failed to get URL from stored bookmark." end if else List.AddRow "Please select a file..." // pick a file dim f as FolderItem = GetOpenFolderItem("") if f = nil then Return list.AddRow "URL: "+f.URLPath // open file and see if we can read it dim b as BinaryStream = BinaryStream.Open(f) dim data as string = b.Read(b.Length) List.AddRow "Data read: "+str(lenb(data)) // create bookmark data dim options as integer = CFBookmarkMBS.kCreationWithSecurityScope dim bookmarkData as string bookmarkData = CFBookmarkMBS.CreateBookmarkData(f, options) dim error as CFErrorMBS = CFBookmarkMBS.LastError if error<>nil then List.AddRow "Error: "+error.Description end if if lenb(bookmarkData) = 0 then // failed? Maybe not sandboxed... List.AddRow "Failed to create bookmark." Return end if // now write to data file b = BinaryStream.Create(datafile, true) b.Write bookmarkData b.Close List.AddRow "Saved bookmark. Please relaunch app." end if Exception io as IOException List.AddRow "IOException: "+str(io.errorNumber)+" "+io.message End EventHandler
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
MyFileTypes
Filetype text/plain
End MyFileTypes
End Project

Feedback, Comments & Corrections

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




Links
MBS Xojo Chart Plugins