Declare-macOS

From Xojo Documentation

Current Logged-In User

Declare Function NSFullUserName Lib "Foundation" As CFStringRef

Center a Window

Declare Sub center Lib "Cocoa" Selector "center" (windowRef As Ptr)
center(Ptr(Self.Handle)) ' The Handle property of the Window

Miniaturize a Window to the Dock

Declare Sub miniaturize Lib "Cocoa" Selector "miniaturize:" (windowRef As Ptr, id As Ptr)
miniaturize(Ptr(Self.Handle), Nil) ' The Handle property of the Window

Move a Window with Animation

Dim newRect As NSRect

' note that coordinates are upside down from what you are used to with Carbon
' 0,0 is at the bottom left
newRect.x = 10
newRect.y = Screen(0).Height - 30
newrect.width = Self.Width
newrect.height = Self.Height + 22

Declare Sub setFrameDisplayAnimate Lib "Cocoa" Selector "setFrame:display:animate:" _
(windowRef As Integer, rect As NSRect, display As Integer, animate As Integer)
setFrameDisplayAnimate(Self.Handle, newRect, 1, 1)

Display Standard Cocoa About Window

Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
Declare Function SharedApplication Lib "Cocoa" Selector "sharedApplication" (receiver As Ptr) As Ptr

Dim sA As Ptr = NSClassFromString("NSApplication")
sA = SharedApplication(sA)

Declare Sub OrderFrontStandardAboutPanel Lib "Cocoa" Selector "orderFrontStandardAboutPanel:" _
(receiver As Ptr, ID As Ptr)

OrderFrontStandardAboutPanel(sA, Nil)

Display a TextField with Rounded Corners

Declare Sub setBezelStyle Lib "Cocoa" Selector "setBezelStyle:" (handle As Integer, value As Integer)
setBezelStyle(Me.Handle, 1)

Play a Cocoa Notification Sound

Const CocoaLib = "Cocoa.framework"
Soft Declare Function NSClassFromString Lib CocoaLib (aClassName As CFStringRef) As Ptr
Soft Declare Function SoundNamed Lib CocoaLib Selector "soundNamed:" (ClsPtr As Ptr, name As CFStringRef) As Ptr
Soft Declare Function Play Lib CocoaLib Selector "play" (instPtr As Ptr) As Boolean

Dim NSSoundClassPtr As Ptr = NSClassFromString("NSSound")
Dim notificationSound As Ptr = SoundNamed(NSSoundClassPtr, "Frog")
Call Play(notificationSound)

Open a Document in a Specific App

// Launch help with selected preview browser
Declare Function NSClassFromString Lib "Foundation" (name As CFStringRef) As Ptr
Declare Function sharedApplication Lib "AppKit" Selector "sharedWorkspace" (obj As Ptr) As Ptr
Dim sharedWorkspace As Ptr = sharedApplication(NSClassFromString("NSWorkspace"))

Declare Function openFile Lib "AppKit" Selector "openFile:withApplication:" (id As Ptr, urlString As CFStringRef, appName As CFStringRef) As Boolean

// fDocument is a FolderItem that points to the document
Call openFile(sharedWorkspace, fDocument.NativePath, "AppName.app")

See Also

Declare