Saturday, November 27, 2021
  • Index
  •  » Automator
  •  » Sizing and locating window for any application.

#1 2021-04-22 11:31:18 am

olddoc
Member
Registered: 2021-04-20
Posts: 3

Sizing and locating window for any application.

I use the following script in a service workflow to locate and size the active window for Safari:

on run {input, parameters}
   
    tell application "Safari"
        tell window 1
            set bounds to {0, 0, 1250, 930}
        end tell
    end tell
   
    return input
end run

I would like to have a single service that would apply to the active window of whichever application I was using at the moment.  Is this possible?

Offline

 

#2 2021-04-23 01:13:15 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Sizing and locating window for any application.

To get the name of frontmost application.

The variable currentApplication is the name of frontmost.

Applescript:

tell application "System Events" to set currentApplication to name of first application process whose frontmost is true

To test the code in Automator.

Applescript:

tell application "System Events" to set currentApplication to name of first application process whose frontmost is true
-- Only for testing
display dialog currentApplication

You code will look like this

Applescript:

tell application currentApplication
-- Your code
end tell

To make above code little more useful you could do a AppleScript record to ask the handler its name.

Applescript:

set appName to name of currentApplication()
display dialog appName

on currentApplication()
   tell application "System Events" to set nameOfCurrentApplication to name of first application process whose frontmost is true
   return {name:nameOfCurrentApplication}
end currentApplication

Last edited by Fredrik71 (2021-04-23 01:36:25 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#3 2021-04-23 11:34:31 am

olddoc
Member
Registered: 2021-04-20
Posts: 3

Re: Sizing and locating window for any application.

Thanks for your help Fredrik71.  When running the script I now get an error message "Can't set bounds of window to {0, 0, 1250, 930}".  This occurs regardless of the app from which the workflow is activated.  This is a copy of the new script:

on run {input, parameters}
   
    tell application "System Events" to set currentApplication to name of first application process whose frontmost is true
   
    tell application currentApplication
        tell window
            set bounds to {0, 0, 1250, 930}
        end tell
    end tell
   
    return input
end run

Offline

 

#4 2021-04-23 12:18:52 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 69

Re: Sizing and locating window for any application.

This variation should work for you

Applescript:

property theBounds : {{0, 0}, {1250, 930}}

tell application "System Events" to tell window 1 of (process 1 whose it is frontmost) to ¬
   set {position, size} to theBounds

Offline

 

#5 2021-04-23 03:26:00 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 917

Re: Sizing and locating window for any application.

olddoc wrote:


on run {input, parameters}
   
    tell application "System Events" to set currentApplication to name of first application process whose frontmost is true
   
    tell application currentApplication
        tell window
            set bounds to {0, 0, 1250, 930}
        end tell
    end tell
   
    return input
end run



tell window should be tell window 1, I test your code and i works on Mojave.

Last edited by Fredrik71 (2021-04-23 03:27:04 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

#6 2021-04-24 08:11:07 am

olddoc
Member
Registered: 2021-04-20
Posts: 3

Re: Sizing and locating window for any application.

Thanks again!  In addition to ignorance I was careless!  Adding "1" solved the problem and I now have the script I wanted.

Offline

 
  • Index
  •  » Automator
  •  » Sizing and locating window for any application.

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)