[PB Cocoa] Methods, Tips & Tricks
- grabiller
- 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
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.
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
Re: [PB Cocoa] Methods, Tips & Tricks
@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 ?
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)
Raspberry Pi OS (Arm64)
- grabiller
- 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
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 ?).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 ?
guy rabiller | radfac founder / ceo | raafal.org
- grabiller
- 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
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
Re: [PB Cocoa] Methods, Tips & Tricks
Apparently it's not that easy (or I'm missing something).grabiller wrote:From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
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)
Raspberry Pi OS (Arm64)
- grabiller
- 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
After extensive search on Google, It seems no one knows how to do that on Mac OSX, which I found totally incredible.wilbert wrote:Apparently it's not that easy (or I'm missing something).grabiller wrote:From "Quartz Window Services Reference", which API should I use to move/resize a window having its CGWindowID ?
I've read some suggestions on using AppleScript to resize the window of another application.
Unfortunately I'm not very familiar with AppleScript
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
Re: [PB Cocoa] Methods, Tips & Tricks
Why would you want to resize external applications?
- grabiller
- 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
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.
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
- grabiller
- 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
I've explained that in my first message, please re-read:Danilo wrote:Why would you want to resize external applications?
The application being Lightwave 3D.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 ../..
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
Re: [PB Cocoa] Methods, Tips & Tricks
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.
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
Re: [PB Cocoa] Methods, Tips & Tricks
Here is the start to enum the windows and get the PID:grabiller wrote:I found this:
https://github.com/nint22/WindowSnaps/b ... Delegate.m
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)
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).
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.
- Destiny - Animation Short
- MAXON CINEMA 4D Architecture Reel 2014
- CINEMA 4D Demo Reel - Spring 2014
- CINEMA 4D Demo Reels
- grabiller
- 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
@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.
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
Re: [PB Cocoa] Methods, Tips & Tricks
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
Re: [PB Cocoa] Methods, Tips & Tricks
Subclassing a Gadget during runtime. This allows overriding methods for a particular gadget without affecting the others, as shown in the following example.
Edit: Added a function called SubClassWindow() for convenience.
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.
Re: [PB Cocoa] Methods, Tips & Tricks
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.
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)