Resizing the window of the current application

I’m trying to write a script that will resize the currently open window of any application. What I have so far:

tell current application
	set appName to the name of the current application
	display dialog appName
    set bounds of front window to {0, 22, 1680, 1050}
end tell

The second and third lines are only there for testing.

Now this worked when I ran it in the script editor but when I try running it from the script menu in the menu bar the current application comes back as System Events. Now I don’t know how to get the scrtpt to recognize the actual current application so any help would be appreciated.

Presumably when you ran it in the Script Editor, appName was set to “Script Editor”, but when you run it from the script menu, the act of clicking on the menu bar has made System Events the current application in order to get your menu pick. You’ll have to be clearer about what you mean by “current application”. How do you define it? Is it to be the last one in front before you clicked the menu bar?

Yeah, when I run it in Script Editor the application listed is “Script Editor”. Sorry I forgot to say that in the first post. And yes, the application I want to work with is the last one active before I click on the menu bar.

Since I don’t know how to get the name of the last running application, I’ll ask in a new post and answer when I get one. The new post is here. I’m interested to know myself, never having needed to know.

Thank you so much Jacques. That works perfectly. And thanks for all your help Adam, much appreciated.

Hello

I have been forwarded to this thread from another forum as a possible solution to my problem.

I basically run a dual display system and wish to run a script to ‘toggle’ the current app between the 2 displays. This script gives me half of what I need, however I’m an applescript beginner so need some help customising it!

Can someone help me add the required commands to:

  1. Determine which screen the app is currently sat on &
  2. Issue the relevant command to move the application window to the opposite screen

Many Thanks

Matt

This will work only when the frontmost application is scriptable! Otherwise it doesn’t understand “front window”

Hi Dennis

Do you know how to reference the 2 displays I have?

Thanks

This returned monitor2_width of resolution at login window. After user is logged in, I have another resolution but the values haven’t changed in plist. :frowning:

So I played around with “call methods”:


set screensArray to call method "screens" of  class "NSScreen"
set myMainScreen to call method "mainScreen" of class "NSScreen"

if {} is not screensArray then
	repeat with displayNo from 1 thru (count screensArray)
		if  myMainScreen is equal  to (item displayNo of screensArray) then
			say "The current application is mainly on display " & (displayNo -1)
		end
	end
end

I have only tested this on my current setup… I put the above scriptlet in an idle handler so I could move
the window around and listen to it’s periodic detection.

Well while I’m at it, here’s how you can detect the current screen sizes:


set screenSize1 to call method "sizeValue" of (|NSDeviceSize| of (call method "deviceDescription" of screen1))
--> {x1, y1}


the thing I need now:
A way to pull just the last used window to the front, without covering up the one I just worked on with the last used window’s brethern.