[PB Cocoa] Methods, Tips & Tricks

Mac OSX specific forum
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

Wilbert,

If you have time, could you please post an example of how to retrieve all the currently open windows (from all running applications that is, not just PB) names, handles (or something), positions and sizes, and then how to move/resize any one of them, all this through Cocoa of course.

I'm trying to create a tool for an application that uses a lot of popup windows, to automatically arrange/snap/resize them in a clean way. The application is working on Windows & Mac but I only know how to do it on Windows. Would be great to have this tool working also on Mac OSX.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

@Grabiller, I think you don't need Cocoa for this.
Take a look at the "Quartz Window Services Reference"
https://developer.apple.com/library/mac ... tions.html
Does that make any sense ?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

wilbert wrote:@Grabiller, I think you don't need Cocoa for this.
Take a look at the "Quartz Window Services Reference"
https://developer.apple.com/library/mac ... tions.html
Does that make any sense ?
Hmm those functions seems to be limited to retrieve a list but I'm not sure what to do with it, how to then move and resize those windows. Plus I would really like to do it from Cocoa for later compatibility (how long the Carbon API will be available/supported by Apple ?).
guy rabiller | radfac founder / ceo | raafal.org
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
guy rabiller | radfac founder / ceo | raafal.org
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

grabiller wrote:From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
Apparently it's not that easy (or I'm missing something).

I've read some suggestions on using AppleScript to resize the window of another application.
Unfortunately I'm not very familiar with AppleScript :(
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

wilbert wrote:
grabiller wrote:From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
Apparently it's not that easy (or I'm missing something).
I've read some suggestions on using AppleScript to resize the window of another application.
Unfortunately I'm not very familiar with AppleScript :(
After extensive search on Google, It seems no one knows how to do that on Mac OSX, which I found totally incredible.

Well, some pretend it's easy, but never give any solution/details. The only comment/answer I've seen close to an answer is:
Use CGWindowListCopyWindowInfo to grab the kCGWindowOwnerPID of each window. Then you can use "distributed objects" if you want access to ObjectiveC stuff, or Mach RPC for other stuff. All of this is documented at http://developer.apple.com

Unfortunately he does not give the exact/direct link on the Apple website.

Perhaps that gives you a hint where to look at ?
guy rabiller | radfac founder / ceo | raafal.org
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Danilo »

Why would you want to resize external applications?
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

I found this:
https://github.com/nint22/WindowSnaps/b ... Delegate.m

Seems to do what I'm looking for. I understand part of the code but I have no idea how to transpose that in PB.

Perhaps you (or someone else) can help ? I know it's a lot to ask but I think it would be really neat to have a little cross-platform set of procedures to list, move and resize windows.
Last edited by grabiller on Thu Jul 24, 2014 6:45 am, edited 1 time in total.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

Danilo wrote:Why would you want to resize external applications?
I've explained that in my first message, please re-read:
grabiller wrote:../.. I'm trying to create a tool for an application that uses a lot of popup windows, to automatically arrange/snap/resize them in a clean way. The application is working on Windows & Mac but I only know how to do it on Windows. Would be great to have this tool working also on Mac OSX ../..
The application being Lightwave 3D.

Someone did a tool for that on Windows with AutoIt. Here is the result:
http://www.youtube.com/watch?v=XW7Bmmv6IXI

I would like to create something similar, but cross-platform and more advanced.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: [PB Cocoa] Methods, Tips & Tricks

Post by fsw »

I'm used to organize gui-apps (in a tile'd way) on linux with OpenBox and some additional keyboard shortcuts.

Because I wanted to have the same on MacOS I searched and found spectacle.
It's opensource and the code is on github.
Maybe the spectacle code can give you an idea how it's done.

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Danilo »

Here is the start to enum the windows and get the PID:

Code: Select all

ImportC ""
    CGWindowListCreate(_1, _2)
    CGWindowListCreateDescriptionFromArray(arr)
    CFArrayGetCount(arr)
    CFArrayGetValueAtIndex(arr, index)
    CFRelease(arr)
    CFDictionaryGetValue(_1,_2)
    CFStringCreateWithCharacters(alloc,text.p-Unicode,len)
    CFNumberGetValue(_1,_2,_3)
EndImport


;CFArrayRef
WindowList = CGWindowListCreate(1, 0)
Debug WindowList
DescriptionList = CGWindowListCreateDescriptionFromArray(WindowList)
Debug DescriptionList
Debug "--------------"
If DescriptionList
    count = CFArrayGetCount(DescriptionList)
    If count
        For i = 0 To count-1
            id = CFArrayGetValueAtIndex(DescriptionList,i)
            Debug "ID: "+id
            pid_num = CFDictionaryGetValue(id, CFStringCreateWithCharacters(0,"kCGWindowOwnerPID",17))
            If pid_num
                CFNumberGetValue(pid_num,9,@pid.l)
                Debug "PID: "+pid
            EndIf
        Next
    EndIf
EndIf
CFRelease(DescriptionList)
CFRelease(WindowList)
It is pretty straightforward to translate the code, so I hope you can do the rest yourself. I'm having a beer or two now (after returning from nightshift work),
not in the mood for doing all the work for you. It is boring because PB does not import any of this API functions, but it should be doable. ;)

Press ALT+CMD+ESC to terminate the running processes, if it does not exit correctly (possibly some clean-up missing). :D

Wouldn't it be better to discuss your problem within a separate topic instead in this topic of general interest?
Maybe a moderator can split it and move grabiller's problems to a separate topic, probably starting at &start=159

Try Cinema4D as a cross-platform alternative. :twisted:
- Destiny - Animation Short
- MAXON CINEMA 4D Architecture Reel 2014
- CINEMA 4D Demo Reel - Spring 2014
- CINEMA 4D Demo Reels
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by grabiller »

@fsw
Thanks a lot for pointing me to this application, it does exactly what I need, it will surely be helpful.

@Danilo
Thanks a lot. I'm not asking you to "do all the work for me" but at least this example gives me a point to start. I'll see how far I can go from there.

Regarding a separate topic you are right, sorry, if I need more help I'll create a new thread in the Mac section.

ps: What makes you think I need an alternative ? ;-)

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by deseven »

Getting the portion of the screen in which it is currently safe to draw your application content (i.e. excluding the dock and the top menu):

Code: Select all

OpenWindow(0,0,0,0,0,"test",#PB_Window_SystemMenu|#PB_Window_Invisible)

mainScreen = CocoaMessage(0,0,"NSScreen mainScreen")
CocoaMessage(@visibleFrame.NSRect,mainScreen,"visibleFrame")

Debug visibleFrame\origin\x
Debug visibleFrame\origin\y
Debug visibleFrame\size\height
Debug visibleFrame\size\width

; origin\y starts from the bottom left corner of the screen, so to get the safe coordinates for PB we can calculate it like that:
ExamineDesktops()
winX = visibleFrame\origin\x
winY = DesktopHeight(0)-visibleFrame\size\height-visibleFrame\origin\y
titleBarH = WindowHeight(0,#PB_Window_FrameCoordinate)-WindowHeight(0,#PB_Window_InnerCoordinate)
winMaxW = visibleFrame\size\width
winMaxH = visibleFrame\size\height-titleBarH

Debug winX
Debug winY
Debug winMaxW
Debug winMaxH

ResizeWindow(0,winX,winY,winMaxW,winMaxH)
HideWindow(0,#False)

Repeat : ev = WaitWindowEvent() : Until ev = #PB_Event_CloseWindow
empty
User
User
Posts: 27
Joined: Sat Apr 12, 2014 11:31 am

Re: [PB Cocoa] Methods, Tips & Tricks

Post by empty »

Subclassing a Gadget during runtime. This allows overriding methods for a particular gadget without affecting the others, as shown in the following example.

Code: Select all




ImportC ""
  sel_registerName(STR.p-ascii)
  class_addMethod(class, selector, imp, types.p-ascii)
  objc_allocateClassPair(class, newClassName.p-ascii, extraBytes)
  objc_registerClassPair(class)
  object_setClass(object, class)
EndImport



Procedure SubClassGadget(Gadget, newClassName.s)
  Protected oldclass = CocoaMessage(0, GadgetID(Gadget), "class")
  Protected result = 0
  result = objc_allocateClassPair(oldclass, newClassName, 0)
  objc_registerClassPair(result)
  object_setClass(GadgetID(Gadget), result)
  ProcedureReturn result
EndProcedure

Procedure SubClassWindow(Window, newClassName.s)
  Protected oldclass = CocoaMessage(0, WindowID(Window), "class")
  Protected result = 0
  result = objc_allocateClassPair(oldclass, newClassName, 0)
  objc_registerClassPair(result)
  object_setClass(Window(Window), result)
  ProcedureReturn result
EndProcedure

Procedure CocoaAddMethod(Class, Method.s, CallBack)
  Protected sel = sel_registerName(Method)
  class_addMethod(Class, sel, CallBack, "v@:@")
EndProcedure


ProcedureC EventHandler(sender)
  Debug sender
  Debug "Escape Key Pressed"
EndProcedure



OpenWindow(0, 0, 0, 322, 205, "SubClassing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 8,  10, 306, 23, "StringGadget (Calls callback on ESC key)")
StringGadget(1, 8,  45, 306, 20, "Second String Gadget")

class = SubClassGadget(0, "MyStringGadget")

CocoaAddMethod(Class, "cancelOperation:", @EventHandler())    

Debug PeekS(CocoaMessage(0, CocoaMessage(0, GadgetID(0), "className"), "UTF8String"), -1, #PB_UTF8)

Repeat 
  event = WaitWindowEvent()
Until event  = #PB_Event_CloseWindow


Edit: Added a function called SubClassWindow() for convenience.
empty
User
User
Posts: 27
Joined: Sat Apr 12, 2014 11:31 am

Re: [PB Cocoa] Methods, Tips & Tricks

Post by empty »

Menu manipulation routines.
Functions to add MenuItems or SubMenus to given MenuTitles (by index) and to remove MenuItems (by handle). Useful for "Recent Files" and correct implementation of the "Window" menu. It's a bit more elegant than deleting and rebuilding the entire menu over and over again.

Code: Select all

Procedure AppendItemToMenu(MenuItemID, Text$, Menu, MenuTitleIndex, ImageID=0)
  Protected mnuArray = MenuID(Menu)
  Protected newParent = CocoaMessage(0, mnuArray, "objectAtIndex:", MenuTitleIndex)
  Protected mItem = MenuItem(MenuItemID, Text$, ImageID)
  Protected oldParent = CocoaMessage(0, mItem, "menu")
  CocoaMessage(0, oldParent, "removeItem:", mItem)
  CocoaMessage(0, newParent, "addItem:", mItem)
  ProcedureReturn mItem
EndProcedure

Procedure AppendSubmenuToMenu(Text$, Menu, MenuTitleIndex, ImageID=0)
  Protected mnuArray = MenuID(Menu)
  Protected newParent = CocoaMessage(0, mnuArray, "objectAtIndex:", MenuTitleIndex)
  Protected mItem = MenuItem(0, Text$, ImageID)
  Protected subMenu = CocoaMessage(0, 0, "NSMenu alloc")
  CocoaMessage(0, subMenu, "initWithTitle:$", @Text$)
  Protected oldParent = CocoaMessage(0, mItem, "menu")
  CocoaMessage(0, oldParent, "removeItem:", mItem)
  CocoaMessage(0, newParent, "addItem:", mItem)
  CocoaMessage(0, mItem, "setSubmenu:", subMenu)
  ProcedureReturn subMenu
EndProcedure


Procedure AppendItemToSubmenu(MenuItemID, Text$, SubMenu, ImageID=0)
  Protected mItem = MenuItem(MenuItemID, Text$, ImageID)
  Protected oldParent = CocoaMessage(0, mItem, "menu")
  CocoaMessage(0, oldParent, "removeItem:", mItem)
  CocoaMessage(0, SubMenu, "addItem:", mItem)
  ProcedureReturn mItem
EndProcedure  

Procedure RemoveMenuItem(MenuItemHandle)
  Protected Parent = CocoaMessage(0, MenuItemHandle, "menu")
  CocoaMessage(0, Parent, "removeItem:", MenuItemHandle)
EndProcedure

; DEMO

OpenWindow(0, 200, 200, 200, 100, "Menu Example")
; create a menu and fill it with two MenuTitles and a few items
CreateMenu(0, WindowID(0)) 
; a file menu
MenuTitle("File")
MenuItem(1, "Open"   +Chr(9)+"Cmd+O")
MenuItem(2, "Save"   +Chr(9)+"Cmd+S")
; an edit menu
MenuTitle("Edit")
MenuItem(4, "Cut")
MenuItem(5, "Copy")
MenuItem(6, "Paste")
delme = MenuItem(7, "Delete me")

; now we append an item to the "File" menu (which is at index 0)
AppendItemToMenu(3, "Save as...", 0, 0)

; and we'll append a submenu to the first (index 0) menu too
sub = AppendSubmenuToMenu("Recent Files", 0, 0)

; our newly created sub menu needs a few items
AppendItemToSubmenu(100, "File number one"  +Chr(9)+"Shift+Cmd+1", sub, 0)
AppendItemToSubmenu(101, "File number two"  +Chr(9)+"Shift+Cmd+2", sub, 0)
AppendItemToSubmenu(102, "File number three"  +Chr(9)+"Shift+Cmd+3", sub, 0)

; get rid of the "delete me" item
RemoveMenuItem(delme)
Post Reply