Platforms to show: All Mac Windows Linux Cross-Platform
/Mac/Alias/FolderItems in Preferences
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /Mac/Alias/FolderItems in Preferences
This example is the version from Tue, 22th Dec 2008.
Notes: Last modified: Tue, 22th Dec 2008
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /Mac/Alias/FolderItems in Preferences
This example is the version from Tue, 22th Dec 2008.
Notes: Last modified: Tue, 22th Dec 2008
Class mainWindow
Inherits Window
// Controls
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
Dim f as FolderItem
f = prefs.GetFolderItem(128, nil, true)
if f = nil then
msgbox "No file specified yet!"
else
if f.exists then
RevealItemInFinder f
BringFinderToFront
else
msgbox "The file can not be located. It used to be at:"+chr(13)+f.absolutePath
end
end
End Sub
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Function MouseDown(X As Integer, Y As Integer) As Boolean Handles Event
showURL "http://www.monkeybreadsoftware.de/realbasic"
End Function
End ControlInstance
// Properties
Protected Dim prefs As PrefsFile
// Event implementations
Sub DropObject(obj As DragItem)
if obj.FolderItemAvailable then
prefs.SetFolderItem(128, obj.FolderItem)
end if
End Sub
Sub Open()
InitPreferences
AcceptFileDrop "AnyFile"
End Sub
// Methods
Sub InitPreferences()
prefs = new PrefsFile("TT's Alias Mgr Demo Preferences")
End Sub
End Class
Module FinderAppleEvents
// Methods
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 err as Integer
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 = NewAppleEvent (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
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 BringFinderToFront()
Dim ok as Boolean
Dim ae as AppleEvent
ae = NewAppleEvent ("misc", "actv", "MACS") // kAEActivate
if ae <> nil then
ok = ae.Send
end
if not ok then
'MsgBox "Oops, Activate failed unexpectedly"
end
End Sub
End Module
Class PrefsFile
// Properties
Protected Dim filename As String
// Methods
Sub Constructor()
// default constructor - this one shall not be used!
raise new RuntimeException
End Sub
Sub Constructor(name as String)
me.filename = name
init()
End Sub
Protected Sub init()
// check if our prefs file already exists, and create one if does not exist yet.
Dim pf as FolderItem, rf as ResourceFork, s as String
pf = SpecialFolder.Preferences.Child(me.filename)
if pf.exists then
// file exists - verify that this is ours
rf = pf.OpenResourceFork
if rf = nil then
Raise new CustomException("PrefsFile.init", "OpenResourceFork() failed")
end
s = rf.GetResource("Ownr", 128)
rf.Close
if s <> "TT's Sample Preference" then
Raise new CustomException("PrefsFile.init", "Filename conflict")
end
else
// create a new file
rf = pf.CreateResourceFork("PrefsFile") ' recommended file type: "pref"
if rf = nil then
// the disk or prefs file is probably write protected - let's not make a big problem out of this
' Raise new CustomException("PrefsFile.openFile", "CreateResourceFork() failed")
return // -> prefs file can not be written to
end
// add a resource that identifies our personal file format
rf.AddResource "TT's Sample Preference", "Ownr", 128, "Owner ID"
rf.Close
end
End Sub
Protected Function openFile() As ResourceFork
Dim pf as FolderItem
pf = SpecialFolder.Preferences.Child(me.filename)
return pf.OpenResourceFork
End Function
Sub SetFolderItem(id as Integer, f as FolderItem)
Dim rf as ResourceFork, s as String
dim alias as MacAliasMBS
rf = openFile()
if rf <> nil then
alias = new MacAliasMBS
if alias.Create(nil,f) >= 0 then
s = alias.GetRecord()
rf.AddResource s, "alis", id, "Alias of "+f.name
rf.Close
end
end
End Sub
Function GetFolderItem(id as Integer, default as FolderItem, mountIfMissing as Boolean) As FolderItem
Dim rf as ResourceFork, s as String
dim alias as MacAliasMBS
Dim f as FolderItem, mode as Integer
rf = openFile()
if rf <> nil then
s = rf.GetResource("alis", id)
if s <> "" then
alias = new MacAliasMBS
if mountIfMissing then
mode = alias.attemptMount
end
alias.SetRecord(s)
f = alias.Resolve(nil, mode)
// if the position or name of the target has changed,
// then we want to update our prefs file with the new
// target information:
if alias.needsUpdate then
if alias.Update(nil, f) = 1 then
rf.AddResource alias.GetRecord(), "alis", id, "Alias of "+f.name
end
end
rf.Close
return f
end
rf.Close
end
return default
End Function
End Class
Class CustomException
Inherits RuntimeException
// Properties
Dim msg As String
Dim where As String
// Methods
Sub Constructor()
me.msg = "Unspecified error"
End Sub
Sub Constructor(msg as String)
me.msg = msg
End Sub
Sub Constructor(location as String, msg as String)
me.where = location
me.msg = msg
End Sub
End Class
Class App
Inherits Application
End Class
Links
MBS REAL studio Chart Plugins - Pfarrgemeinde St. Arnulf Nickenich