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?

To get the name of frontmost application.

The variable currentApplication is the name of frontmost.

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

To test the code in Automator.

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

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.

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

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

This variation should work for you

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

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

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