Search code examples
applescript

Applescript app to change the size of the foremost app's top most window to arbitrary size


I want to write an app using applescrpt (saved with the .app extension) that whenever executed, will find out the foremost windows of the foremost application and resize/reposition it to a specific size. I have written the following, which works in Script Editor, but then it does not work on other windows, when I execute the app via spotlight.

tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set frontAppWindow to first window of frontApp
    set size of frontAppWindow to {800, 600}
end tell

This results in the error:

Can't get window 1 of «class pcap» "lhalf"' of application
"System Events". Invalid index.
System Events got an error: Can't get window 1 of application process "lhalf".
Invalid index. (-1719)
tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
    tell process frontApp
        set frontWindow to first window whose value of attribute "AXMain" is true
        set position of frontWindow to {0, 22}
        set size of frontWindow to {800, 600}
    end tell
end tell

This gives an error.

Can't get window 1 of «class pros» "lhalf" of application
"System Events" whose value of
attribute "AXMain" = true. Invalid
index.
System Events got an error: Can't get window 1 of process "lhalf" whose value
of attribute "AXMain" = true. Invalid
index. (-1719)

I have named my app lhalf.app. When I execute this app via spotlight, it think the foremost app is itself, and not the app that is currently on the screen. How can I remedy this? Or are there any other alternate better ways to code the entire thing from scratch?


Solution

  • If you want to create an AppleScript applet, this following code should work.

    I added a delay of five seconds at the beginning of the code, which allows you to bring the target application frontmost first (for example: clicking on the app's icon in the Dock)

    --The delay gives you time to bring target app to front (clicking app's icon in the Dock) 
    delay 5
    
    set thePosition to {0, 22}
    set theSize to {800, 600}
    set theBounds to {thePosition} & {theSize}
    
    tell application "System Events"
        tell window 1 of (process 1 whose it is frontmost) to ¬
            set {position, size} to theBounds
    end tell
    

    However, I think an easier and quicker solution would be to remove the delay command from the above code, save it as "lhalf.scpt" in your /Users/YOUR_SHORT_NAME/Library/Scripts folder. Then you can just directly run the "lhalf.scpt" from Scripts menu in your System Status bar.

    enter image description here

    If your Script menu in your System Status Bar is not enabled, this following AppleScript code will enable that for you.

    tell application "AppleScript Utility"
        if not Script menu enabled then
            set Script menu enabled to true
            set application scripts position to top
            set show Computer scripts to false
        end if
    end tell