Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/Data Detectors


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

This example is the version from Mon, 21th Jan 2024.

Project "Data Detectors.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control InputText Inherits TextArea
ControlInstance InputText Inherits TextArea
EventHandler Sub TextChange() DelayTimer.Mode = timer.ModeSingle End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control MyButton Inherits PushButton
ControlInstance MyButton Inherits PushButton
EventHandler Sub Action() CheckMenu End EventHandler
End Control
Control DelayTimer Inherits Timer
ControlInstance DelayTimer Inherits Timer
EventHandler Sub Action() check End EventHandler
End Control
EventHandler Sub Open() check End EventHandler
Sub Check() // create detector for Address Dim types As Integer = _ NSTextCheckingResultMBS.NSTextCheckingTypeAddress + _ NSTextCheckingResultMBS.NSTextCheckingTypeDate + _ NSTextCheckingResultMBS.NSTextCheckingTypeLink + _ NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber + _ NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation List.DeleteAllRows Dim Error As NSErrorMBS Dim DataDetector As New NSDataDetectorMBS(types, error) Dim s As String = InputText.Text Dim st As StyledText = InputText.StyledText st.TextColor(0, s.Len) = Color.Black // analyze now Dim matches() As NSTextCheckingResultMBS = DataDetector.matches(s, 0) // show Address For Each result As NSTextCheckingResultMBS In matches Dim r As NSRangeMBS = result.range Dim resultType As String Select Case result.resultType Case NSTextCheckingResultMBS.NSTextCheckingTypeAddress resultType = "Address" st.TextColor(r.Location, r.Length) = Color.Red Case NSTextCheckingResultMBS.NSTextCheckingTypeDate resultType = "Date" st.TextColor(r.Location, r.Length) = Color.Green Case NSTextCheckingResultMBS.NSTextCheckingTypeLink resultType = "Link" st.TextColor(r.Location, r.Length) = Color.Blue Case NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber resultType = "PhoneNumber" st.TextColor(r.Location, r.Length) = Color.Purple Case NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation resultType = "TransitInformation" st.TextColor(r.Location, r.Length) = Color.Brown Else Break End Select List.AddRow "Found "+resultType+" at "+Str(r.Location+1) List.AddRow "Text: "+s.Mid(r.Location+1, r.Length) // show details_ Select Case result.resultType Case NSTextCheckingResultMBS.NSTextCheckingTypeAddress, _ NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation Dim dic As Dictionary = result.components Dim lines() As String For Each key As Variant In dic.keys list.AddRow key.StringValue+": "+dic.Value(key).StringValue Next Case NSTextCheckingResultMBS.NSTextCheckingTypeDate Dim d As date = Result.date If d <> Nil Then List.AddRow "Date: "+d.AbbreviatedDate +" "+d.LongTime End If Dim du As Double = result.duration If du <> 0 Then List.AddRow "Duration: "+Str(du) End If Case NSTextCheckingResultMBS.NSTextCheckingTypeLink List.AddRow "URL: "+result.URL Case NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber List.AddRow "PhoneNumber: "+result.phoneNumber Else Break End Select List.AddRow "" Next End Sub
Sub CheckMenu() // create detector for Address Dim types As Integer = _ NSTextCheckingResultMBS.NSTextCheckingTypeAddress + _ NSTextCheckingResultMBS.NSTextCheckingTypeDate + _ NSTextCheckingResultMBS.NSTextCheckingTypeLink + _ NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber + _ NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation Dim Error As NSErrorMBS Dim DataDetector As New NSDataDetectorMBS(types, error) Dim menu As New MenuItem // analyze now Dim Text As String = InputText.Text Dim matches() As NSTextCheckingResultMBS = DataDetector.matches(Text, 0) // show Address For Each result As NSTextCheckingResultMBS In matches Dim r As NSRangeMBS = result.range Dim foundText As String = Text.Mid(r.Location+1, r.Length) Select Case result.resultType Case NSTextCheckingResultMBS.NSTextCheckingTypeAddress Dim m As New MyMenuItem Dim oneLineText As String = ReplaceLineEndings(foundText, ", ") m.Text = "Copy Address "+oneLineText m.CopyText = foundText menu.Append m m = New MyMenuItem m.Text = "Open Apple Maps for "+oneLineText m.ShowURLText = "https://maps.apple.com/?q=" + EncodeURLComponent(oneLineText) menu.Append m m = New MyMenuItem m.Text = "Open Google Maps for "+oneLineText m.ShowURLText = "https://www.google.com/maps/search/?api=1&query=" + EncodeURLComponent(oneLineText) menu.Append m Case NSTextCheckingResultMBS.NSTextCheckingTypeDate Dim m As New MyMenuItem m.Text = "Copy Date "+foundText m.CopyText = foundText menu.Append m Case NSTextCheckingResultMBS.NSTextCheckingTypeLink Dim url As String = result.URL If Left(url, 7) = "mailto:" Then Dim email As String = url.Mid(8) Dim m As New MyMenuItem m.Text = "Copy email "+email m.CopyText = email menu.Append m m = New MyMenuItem m.Text = "New email to "+email m.ShowURLText = URL menu.Append m Else Dim m As New MyMenuItem m.Text = "Copy URL "+URL m.CopyText = URL menu.Append m m = New MyMenuItem m.Text = "Open URL "+URL m.ShowURLText = URL menu.Append m End If Case NSTextCheckingResultMBS.NSTextCheckingTypePhoneNumber Dim m As New MyMenuItem m.Text = "Copy phone number "+result.phoneNumber m.CopyText = result.phoneNumber menu.Append m m = New MyMenuItem m.Text = "Call phone number "+result.phoneNumber m.ShowURLText = "tel:"+EncodeURLComponent(result.phoneNumber.Trim) menu.Append m Case NSTextCheckingResultMBS.NSTextCheckingTypeTransitInformation Dim oneLineText As String = ReplaceLineEndings(foundText, ", ") Dim m As New MyMenuItem m.Text = "Copy transit information "+foundText m.CopyText = foundText menu.Append m m = New MyMenuItem m.Text = "Search for "+oneLineText+" with Google" m.ShowURLText = "https://www.google.de/search?q=" + EncodeURLComponent(oneLineText) menu.Append m Else Break End Select Dim m As New MenuItem("-") menu.Append m Next If menu.Count > 0 Then Call menu.PopUp End If End Sub
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
Sign
End Sign
Class MyMenuItem Inherits MenuItem
EventHandler Function Action() As Boolean If CopyText <> "" Then Dim c As New Clipboard c.SetText CopyText Return True End If If ShowURLText <> "" Then ShowURL ShowURLText return true End If End EventHandler
Property CopyText As String
Property ShowURLText As string
End Class
End Project

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


The biggest plugin in space...