Platforms to show: All Mac Windows Linux Cross-Platform

/MacClassic/Using Mac OS Alias files


Required plugins for this example: MBS MacBase Plugin, MBS MacClassic 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: /MacClassic/Using Mac OS Alias files

This example is the version from Fri, 21th Nov 2019.

Project "Using Mac OS Alias files.xojo_binary_project"
FileTypes
Filetype AnyFile
End FileTypes
Class mainWindow Inherits Window
Binding enablingBinder
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control Infotext Inherits Label
ControlInstance Infotext Inherits Label
End Control
Control GroupBox1 Inherits GroupBox
ControlInstance GroupBox1 Inherits GroupBox
End Control
Control NoDlgChk Inherits CheckBox
ControlInstance NoDlgChk Inherits CheckBox
End Control
Control MountChk Inherits CheckBox
ControlInstance MountChk Inherits CheckBox
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
EventHandler Function MouseDown(X As Integer, Y As Integer) As Boolean #Pragma unused x #Pragma unused y Me.TextColor = RGB(255,0,0) return true End EventHandler
EventHandler Sub MouseUp(X As Integer, Y As Integer) #Pragma unused x #Pragma unused y Me.TextColor = RGB(0,0,255) showurL "http://monkeybreadsoftware.de/realbasic" End EventHandler
End Control
Control CheckIcon Inherits CheckBox
ControlInstance CheckIcon Inherits CheckBox
End Control
EventHandler Sub DropObject(obj As DragItem, action As Integer) #Pragma unused action if obj.FolderItemAvailable then DemoIt (obj.FolderItem) end End EventHandler
EventHandler Sub EnableMenuItems() FileOpen.enabled = true FileSelectFolder.enabled = true End EventHandler
EventHandler Sub Open() AcceptFileDrop "AnyFile" End EventHandler
Function FileOpen() As Boolean Dim f As FolderItem f=GetOpenFolderItem("AnyFile") if f<>nil then DemoIt (f) end if End Function
Function FileSelectFolder() As Boolean Dim f as Folderitem f=SelectFolder If f<> Nil Then DemoIt (f) End If End Function
Sub CopyIcon(source as folderitem, dest as folderitem) dim n as NSImageMBS = NSWorkspaceMBS.iconForFile(source) if n <> nil then call NSWorkspaceMBS.setIcon(n, dest) end if End Sub
Sub DemoIt(f as FolderItem) Dim s, alisRsrc as String Dim mode, err as Integer Dim f1 as FolderItem, rf as ResourceForkMBS Dim alias as MacAliasMBS if f.Alias then // This is an Alias File - let's look at it // get the file's "alis" resource, which contains the Alias Record in binary format rf = ResourceForkMBS.Open(f, false) alisRsrc = rf.GetResource("alis", 0) rf.Close // create an AliasRecord from it alias = new MacAliasMBS alias.SetRecord alisRsrc // get some path information from the AliasRecord without actually resolving it s = "Alias File's name: " + f.Name + Chr(13) s = s + Chr(13) dim info as AliasInfoMBS = alias.AliasInfo s = s + "Destination target name: " + info.TargetName + Chr(13) s = s + "Destination volume name: " + info.VolumeName + Chr(13) s = s + "Destination Path: " + info.PathString + Chr(13) // now let's resolve the Alias mode = 0 if self.MountChk.value then mode = mode + alias.attemptMount if self.NoDlgChk.value then mode = mode + alias.noDialogs end end f1 = alias.Resolve(nil, mode) s = s + Chr(13) if f1 <> nil then s = s + "Resolved path: " + f1.NativePath + Chr(13) else s = s + "Unable to resolve the alias." + Chr(13) end else // This is a file or folder (no Alias), // let's create an Alias File for it on the Desktop alias = new MacAliasMBS err = alias.Create(nil, f) if err <> 0 then s = "Oops - an error occured when creating the AliasRecord: " + Str(err) else f1 = SpecialFolder.Desktop.TrueChild(f.name+" Alias") if f1.exists then s = "Oops - Can't create the Alias File: there's already a file with the name “" + f1.name + "” on the Desktop." // <-- CONVERTED else // create a new file in the boot volume's desktop folder rf = ResourceForkMBS.Create(f1) if rf = nil then s = "Oops - Failed to create the Alias File “" + f1.NativePath + "”." // <-- CONVERTED else // add the AliasRecord to the file's resource rf.AddResource alias.GetRecord(), "alis", 0, "Created by TT's Alias Mgr Plugin Demo" // set the file's type and creator codes #If RBVersion < 2019 Then If f.Directory Then f1.MacType = "fdrp" f1.MacCreator = "MACS" Else f1.MacType = f.MacType f1.MacCreator = f.MacCreator End #endif // set the "isAlias" flag in the file's properties. // RB does not provide a function for this (the Alias property of // the FolderItem class is read-only), thus we use two methods // from the MBS plugin here. err = f1.SetFileFlagsMBS(BitwiseOr(f1.GetFileFlagsMBS, &H8000)) rf.Close if CheckIcon.Value then CopyIcon f,f1 end if // make the newly created file known to the Finder immediately UpdateItemInFinder f1 s = "Alias to “" + f.name + "” successfully created on the Desktop." // <-- CONVERTED end end end end Infotext.Text = s End Sub
End Class
MenuBar Menu
MenuItem UntitledMenu4 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileOpen = "Select File..."
MenuItem FileSelectFolder = "Select Folder..."
MenuItem UntitledMenu3 = "-"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu8 = ""
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu7 = ""
MenuItem UntitledMenu6 = ""
MenuItem UntitledMenu5 = ""
End MenuBar
Module FinderAppleEvents
Sub BringFinderToFront() Dim ok as Boolean Dim ae as AppleEvent ae = New AppleEvent ("misc", "actv", "MACS") // kAEActivate if ae <> nil then ok = ae.Send end if not ok then 'MsgBox "Oops, Activate failed unexpectedly" end End Sub
Function FinderSupportsOSL() As Boolean Dim l as Integer if not System.Gestalt("fndr", l) then l = 0 end return BitwiseAnd(l, 8) <> 0 End Function
Sub RevealItemInFinder(f as FolderItem) Dim ok as Boolean ok = sendItemAE ("misc", "mvis", "MACS", f) // kAEMakeItemVisible if not ok then 'MsgBox "Oops, Reveal failed unexpectedly" end End Sub
Sub UpdateItemInFinder(f as FolderItem) Dim ok as Boolean ok = sendItemAE ("fndr", "fupd", "MACS", f.Parent) // kAESync if ok then ok = sendItemAE ("fndr", "fupd", "MACS", f) // kAESync end if not ok then 'MsgBox "Oops, Update failed unexpectedly" end End Sub
Protected Function sendItemAE(s1 as String, s2 as String, s3 as String, f as FolderItem) As Boolean Dim ae as AppleEvent Dim list as AppleEventDescList if FinderSupportsOSL() then ae = New AppleEvent (s1, s2, s3) if ae <> nil then list = New AppleEventDescList list.AppendFolderItem (f) ae.DescListParam("----") = list return ae.Send end end return false End Function
End Module
Class App Inherits Application
End Class
End Project

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


The biggest plugin in space...