It is currently Wed Oct 21, 2020 3:11 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 202 posts ]  Go to page Previous  1 ... 10, 11, 12, 13, 14
Author Message
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Sep 16, 2016 11:43 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
More on that (do something when user changes interface mode):

Code:
Define app = CocoaMessage(0,0,"NSApplication sharedApplication")
Define appDelegate = CocoaMessage(0,app,"delegate")
Define delegateClass = object_getClass_(appDelegate)
Define selector = sel_registerName_("darkModeChanged:")
Define distributedNotificationCenter = CocoaMessage(0,0,"NSDistributedNotificationCenter defaultCenter")

Procedure darkModeChanged(notification)
  Debug "mode changed"
EndProcedure

class_addMethod_(delegateClass,selector,@darkModeChanged(),"v@:@")
CocoaMessage(0,distributedNotificationCenter,
             "addObserver:",appDelegate,
             "selector:",selector,
             "name:$",@"AppleInterfaceThemeChangedNotification",
             "object:",#nil)

OpenWindow(0,#PB_Ignore,#PB_Ignore,100,100,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

Repeat : Until WaitWindowEvent(100) = #PB_Event_CloseWindow


Again thanks wilbert for showing me how to add observers :)

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Mon Dec 12, 2016 8:22 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3678
Location: Netherlands
Small code to show how to format text of an EditorGadget with html code.
Code:
If OpenWindow(0, 0, 0, 320, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  EditorGadget(0, 10, 10, 300, 130)
 
 
  HTMLCode.s = "<font face='Helvetica' size='14'><b>Test</b> <font color='red'>code</font></font>"

  AttributedString = CocoaMessage(0, CocoaMessage(0, 0, "NSAttributedString alloc"), "initWithHTML:",
                                  CocoaMessage(0, CocoaMessage(0, 0, "NSString stringWithString:$", @HTMLCode),
                                               "dataUsingEncoding:", 10), "documentAttributes:", #Null)
  If AttributedString
    TextStorage = CocoaMessage(0, GadgetID(0), "textStorage")
    CocoaMessage(0, TextStorage, "setAttributedString:", AttributedString)
    CocoaMessage(0, AttributedString, "release")
  EndIf
 
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
 
EndIf

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Thu May 04, 2017 11:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
Restarting your app (code ported from this gist)

Code:
Procedure restartApp(delay.i)
  Protected task = CocoaMessage(0,CocoaMessage(0,CocoaMessage(0,0,"NSTask alloc"),"init"),"autorelease")
  Protected args = CocoaMessage(0,0,"NSMutableArray arrayWithCapacity:",0)
  Protected appPath.s = PeekS(CocoaMessage(0,CocoaMessage(0,CocoaMessage(0,0,"NSBundle mainBundle"),"bundlePath"),"UTF8String"),-1,#PB_UTF8)
  Protected command.s = "sleep " + Str(delay) + ~"; open -a \"" + appPath + ~"\""
  CocoaMessage(0,args,"addObject:$",@"-c")
  CocoaMessage(0,args,"addObject:$",@command)
  CocoaMessage(0,task,"setLaunchPath:$",@"/bin/sh")
  CocoaMessage(0,task,"setArguments:",args)
  CocoaMessage(0,task,"launch")
  End
EndProcedure

OpenWindow(0,#PB_Ignore,#PB_Ignore,400,300,"restart app test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ButtonGadget(0,150,135,100,30,"restart")

Repeat
  ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget And EventGadget() = 0
    restartApp(1)
  EndIf
Until ev = #PB_Event_CloseWindow

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Nov 03, 2017 4:39 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3678
Location: Netherlands
Create UUID.

Method 1
Code:
ImportC ""
  CFUUIDCreate(alloc = #Null)
  CFUUIDCreateString(alloc, uuid)
EndImport

Procedure.s CreateUUID()
  Protected.i uuidRef, uuidStringRef, UUID.s
  uuidRef = CFUUIDCreate()
  uuidStringRef = CFUUIDCreateString(#Null, uuidRef)
  CFRelease_(uuidRef)
  UUID = PeekS(CocoaMessage(0, uuidStringRef, "UTF8String"), -1, #PB_UTF8)
  CFRelease_(uuidStringRef)
  ProcedureReturn UUID 
EndProcedure

Debug CreateUUID()


Method 2 (MacOS 10.8+)
Code:
UUID.s = PeekS(CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSUUID UUID"), "UUIDString"), "UTF8String"), -1, #PB_UTF8)
Debug UUID

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Sat Jun 13, 2020 10:24 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1120
Location: Portugal
I have created a script to renew an applications icon and saved it as a script.

Can wilberts AppleScript procedure be used to run the script or is there another way?

Regards

CD

_________________
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Sat Jun 13, 2020 5:12 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3678
Location: Netherlands
collectordave wrote:
Can wilberts AppleScript procedure be used to run the script or is there another way?

If you load the script you can use that procedure.
Another option might be the terminal command osascript

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Sun Sep 27, 2020 11:37 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
Run a program and write to its stdin using NSTask (because PB's RunProgram is buggy):

Code:
Procedure RunProgramNative(path.s,args.s,workdir.s = "",stdin.s = "")
  Protected i
  Protected argsArray
  If args
    Protected arg.s = StringField(args,1," ")
    If arg
      argsArray = CocoaMessage(0,0,"NSArray arrayWithObject:$",@arg)
      If CountString(args," ") > 0
        For i = 2 To CountString(args," ") + 1
          arg = StringField(args,i," ")
          If arg
            argsArray = CocoaMessage(0,argsArray,"arrayByAddingObject:$",@arg)
          EndIf
        Next
      EndIf
    EndIf
  EndIf
  Protected task = CocoaMessage(0,CocoaMessage(0,0,"NSTask alloc"),"init")
 
  CocoaMessage(0,task,"setLaunchPath:$",@path)
 
  If argsArray
    CocoaMessage(0,task,"setArguments:",argsArray)
  EndIf
 
  If workdir
    CocoaMessage(0,task,"setCurrentDirectoryPath:$",@workdir)
  EndIf
 
  If stdin
    Protected writePipe = CocoaMessage(0,0,"NSPipe pipe")
    Protected writeHandle = CocoaMessage(0,writePipe,"fileHandleForWriting")
    CocoaMessage(0,task,"setStandardInput:",writePipe)
    Protected string = CocoaMessage(0,0,"NSString stringWithString:$",@stdin)
    Protected stringData = CocoaMessage(0,string,"dataUsingEncoding:",#NSUTF8StringEncoding)
  EndIf
 
  CocoaMessage(0,task,"launch")
 
  If stdin
    CocoaMessage(0,writeHandle,"writeData:",stringData)
    CocoaMessage(0,writeHandle,"closeFile")
  EndIf
 
  CocoaMessage(0,task,"release")
EndProcedure

Based on this article. Reading from stdout is also an option, see the bottom example.

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 202 posts ]  Go to page Previous  1 ... 10, 11, 12, 13, 14

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye