How can I write an Applescript to resize an application window on external monitor - resize

I want to write an Applescript to resize the Safari window on my external screen.
I have a Macbook Air Screen of 1440x900 and an Asus external monitor of 2560x1440.
I want to keep Safari on my external monitor, with a 170 pixel gap on the left-hand side.
I wrote the following basic script:
tell application "System Events"
tell application "Safari"
activate
set bounds of window 1 to {170, 0, 2560, 1440}
end tell
end tell
The result is that the only Safari window open gets moved from my external monitor to my Macbook Air (Mojave) screen with the correct width (2560-170 = 2390) and incorrect height (900 and not the 1440 specified).
How do I ensure the correct width and height AND keep the window on my external monitor.
Many thanks.

In this case, you don't need to use System Events; Safari knows how to resize its own windows. All you need to do is get the bounds of the window, change the last two items of the returned list, and then set the bounds to the revised array. This will change the size without moving the position of the window.
tell application "Safari"
activate
set old_bounds to bounds of window 1
set item 3 of old_bounds to 2560
set item 4 of old_bounds to 1440
set bounds of window 1 to old_bounds
end tell

suggestion to is to use this:
tell application "Safari"
set the bounds of window 1 to {260, 21, 1660, 1400}
activate
end tell

Related

Getting window restored bounds

When you maximize a window in Macos, the window fills the screen.
What I want to be able to do is get the windows restored position and size, like when you press the maximize button again to restore the original position. How can you do this?
I need this for saving the window position on exit.
There is a method prepared for this task in NSWindow.
You could ask your ViewController for its NSWindow and set an AutosaveName for its Frame like..
[self.view.window setFrameAutosaveName:#"VerySpecialWindowAutoSaveName"];
which will end up in NSUserDefaults of your App as entry like...
"NSWindow Frame VerySpecialWindowAutoSaveName" = "300 100 1200 1005 0 0 2560 1289"
But the best place for this code is ... there is no best place because it depends on your apps approach.
The whole process can be challenging when you have multiple windows in a document based application but as you can set the AutosaveName per documents window you are able to recover the frame if needed, at least for the last document. Should be mentioned that you can set the AutosaveName in InterfaceBuilder and in Code as well - so keep an eye on it they follow the same name if you use both IB & code for one and the same window.
and an example in swift you can find in this gist

Adapt form dimension according to screen resolution

how can I adjust the size of my application based on the resolution of the screen that the user possesses?
So far I have tried with Anchor, but does not meet at all my clients.
There are some of them who start their application on 11-inch screens, there are the lower parts of the app that are not displayed.
I want something responsive, that at the time the application reads the screen resolution and suitable all controls with an acceptable size.
You should set the form's Maximized and Topmost property to True. However, the controls inside the form will not be adjusted if the sizes are static.
So, to dynamically change the size of the form's controls, you have to dock/anchor them. I suggest you to use the panel control to place for you to be able to layout the controls the way you wanted.
If that seems plenty, you can place this in the form's load event:
Me.Width = Screen.PrimaryScreen.Bounds.Width - 20
Me.Height = Screen.PrimaryScreen.Bounds.Height - 20
If you don't want to use the Maximized Screen property, you may look at this: Change Resolution Of Form Based On Screen Resolution ( without changing monitor resolution and using Maximized screen option )

Animation with wxWidgets

In wxWidgets, is it possible to have on a wxPanel, an object that contains a mouse click event which moves the objects to another location when activated? The movement should be seen as an animation, e.g. Sliding from its initial location to the next location (Using wxTimer?). Is this by any means, possible?
Basically you need to change the position of whatever you are displaying smoothly so a timer at 25 Hz and then update the position a proportion of the movement each time, e.g. if you need to move from 0,0 to 100,200 smoothly in 4 seconds then updating your position by +1,+2 each timer event should do nicely. You may find that you need to invalidate a display area that includes the position before the move and after it to force a redraw but give it a try without first.

Programmatically change height of the window more than the height of the screen in Mac OS X

Respected low-level users of Mac OS, please, help.
I'm trying programmatically to change height of window of safari (or other window). I'm was trying used AppleScript and AXUIElementSetAttributeValue of Carbon, but none of these methods can't increase the window bigger than the height of the screen. But, the width changes without any problems.
I guess I'd be starting with something like SetWindowPos with SWP_NOSENDCHANGING flag under Win32.
Maybe, you can disable this functionality, which monitors the size of the window or completely shutdown the one who is responsible for it?
Note that I want to do this programmatically from an external process - I'm not asking how to control just my own app's window size and position.
Thanks.
It's not possible, search for "Note that any NSWindow with a title bar automatically constrains itself to the screen" in the Window Programming Guide.

Finder item in some point

I am clicking with the right mouse button in some Finder's window. I am getting the location of the click (its CGPoint). How can I tell what item is in that point ?
You can get selected item using below apple script
tell application "Finder"
selection
end tell
You would have to do it manually if Parag Bafna's suggestion doesn't work for you. Basically a Finder item has a position property. However the coordinates you get back are in the window coordinates. I'm assuming that your CGPoint is in global coordinates so you'll have to convert the window coordinates to global coordinates. You can do that easily enough because the window has it's own coordinates so you can figure out the global coordinates of a Finder item.
So it's doable with a little work using the Finder items' position property.
tell application "Finder"
tell window 1
set theItems to items
set oneItem to item 1 of theItems
return position of oneItem
end tell
end tell