Examples for macOS

Current logged-in user

Declare Function NSFullUserName Lib "Foundation" As CFStringRef

Center a window

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

Miniaturize a window to the dock

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

Move a window with animation

Var 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 "AppKit" Selector "setFrame:display:animate:" _
  (windowRef As Ptr, rect As NSRect, display As Integer, animate As Integer)
setFrameDisplayAnimate(Self.Handle, newRect, 1, 1)

Display standard Cocoa About window

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

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

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

OrderFrontStandardAboutPanel(sA, Nil)

Display a TextField with rounded corners

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

Play a Cocoa notification sound

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

Var NSSoundClassPtr As Ptr = NSClassFromString("NSSound")
Var 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 "AppKit" (name As CFStringRef) As Ptr
Declare Function sharedApplication Lib "AppKit" Selector "sharedWorkspace" (obj As Ptr) As Ptr
Var 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 keyword