Applescript AeroSnap
Applescript AeroSnap
- Subject: Applescript AeroSnap
- From: Guido Tangorra <email@hidden>
- Date: Mon, 04 Aug 2014 16:53:04 +0200
I'm trying to build some sort of Aero Snap feature with Applescript.
This is the code:
# Hide this app at startup
set myAppName to "mySnap"
tell application "System Events" to tell application process myAppName to set visible to false
on idle
# Get the screen size
tell application "Finder"
set mDimensions to bounds of window of desktop
set mWidth to item 3 of mDimensions
set mHeight to item 4 of mDimensions
set halfWidth to (mWidth / 2)
set halfHeight to (mHeight / 2)
end tell
delay 2
# Get active window
tell application "System Events"
try
#keystroke "h" using command down
set frontApp to name of first application process whose (frontmost is true and visible is true)
#display dialog frontApp
on error errt number errn
display dialog "I can't get the frontmost application"
end try
end tell
# Resize window
tell application frontApp
try
activate
set app_bounds to get bounds of window 1
set left_b to (get item 1 of app_bounds)
set right_b to (get item 3 of app_bounds)
set top_b to (get item 2 of app_bounds)
set bottom_b to (get item 4 of app_bounds)
#debug actual window position
#display dialog "left_b: " & left_b & return & "tob_b: " & top_b & return & "mHeight: " & mHeight
if left_b ≤ 0 and top_b ≤ 44 and bottom_b is not equal to mHeight and bottom_b is not equal to halfHeight then
#snap top-left
set bounds of window 1 to {0, 0, halfWidth, halfHeight}
else if left_b ≤ 0 and top_b > 44 then
#snap left
set bounds of window 1 to {0, 0, halfWidth, mHeight}
else if right_b ≥ mWidth and top_b > 44 then
#snap right
set bounds of window 1 to {halfWidth, 0, mWidth, mHeight}
else if right_b ≥ mWidth and top_b ≤ 44 and bottom_b is not equal to mHeight and bottom_b is not equal to halfHeight then
#snap top-right
set bounds of window 1 to {halfWidth, 0, mWidth, halfHeight}
end if
on error errt number errn
display dialog "Impossibile ridimensionare finestra: " & errt
end try
end tell
return 0.5
end idle
In my script I've set the menu bar's height at 44 (fixed value), but this seems to work only with some apps. With a little debug, I have found that the minimum distance of a Finder's window from the top of the screen is of 44 (hence I have presumed that this was the menu bar's height). But Applescript Editor's windows gave me a minimum distance from the top of the screen of 22.
It seems a strange behaviour...there's a method to get the menu bar's height, dinamically?
Any suggestions?
Thanks.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden