I need help with Apple script. I don't expert in this matter. The script is this:
(*
This Apple script will resize any program window to an exact size and the window is then moved to the center of your screen.
Specify the program name, height and width below and run the script.
Written by Amit Agarwal on December 10, 2013
*)
set theApp to "Finder"
set appHeight to 412
set appWidth to 678
tell application "Finder"
set screenResolution to bounds of window of desktop
end tell
set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution
tell application theApp
activate
reopen
set yAxis to (screenHeight - appHeight) / 2 as integer
set xAxis to (screenWidth - appWidth) / 2 as integer
set the bounds of the first window to {xAxis, yAxis, appWidth + xAxis, appHeight + yAxis}
tell application "Finder" to set the sidebar width of every Finder window to 142
end tell
I would like to resize Finder, but without center it in the screen. Is it possible?
The logic is, you get the current bounds of the front window. The first two items in the bounds are the starting horizontal point on the x axis and the starting vertical point on the y axis. (in other words, the first two items are the coordinates of the top left corner)
Once you have those, you set new bounds, keeping the first two items in the bounds description the same, and then adding the desired width and height to the second two points to resize the window.
set theApp to "Finder"
set appHeight to 412
set appWidth to 678
tell application theApp
activate
set {startHoriz, startVert, endHoriz, endVert} to bounds of the first window
set the bounds of the first window to {startHoriz, startVert, startHoriz + appWidth, startVert + appHeight}
end tell
tell application "Finder" to set the sidebar width of every Finder window to 142
If you wanted to move the window all the way to the left, you could change the startHoriz to 1 before setting the bounds of the window.
Related
Goal: Ensure the app's window is moved to the destination's position and size and also make sure the entire window is showed on the screen.
I have following script, it sometimes works and sometimes don't.
For the sake of simplicity I hardcoded values. Two things script is doing,
It sets position & size based on a certain parameters then it retrieve final size value. If calculated size is less than app's window minimum size then take minimize size(winWidth, winHeight) of the window and use this for positioning and sizing of the window to ensure the complete window is shown on the screen.
screen size - 1920 X 1080,
menu bar height - 24,
dock height - 61
visible empty space screen height - 1835 and the script is suppose to show the window bottom half
tell application "System Events"
tell application process "Craft"
delay 0.1
tell window 1 to set position to {1835 / 2, 25}
tell window 1 to set size to {(1835) / 2, 994}
end tell
delay 0.1
set {winWidth, winHeight} to size of the first window of application process "Craft"
tell application process "Craft"
delay 0.1
tell window 1 to set position to {1835 - winWidth, 25}
tell window 1 to set size to {(1835) / 2, 994}
end tell
delay 0.1
return winWidth
end tell
firstly I am not quite sure if this is how to calculate it but looking at Math it should work. Secondly, no idea when why it stops working but rarely.
Work around - I am running the above script more than once to fix the issue but still have some hiccups with it.
I am currently using Applescript to calculate the size and position of a window from left to right which works great. Here is an example where I am trying to position of a window as "Left half of the screen"
tell application "System Events"
tell application process "Xcode"
activate
tell window 1 to set size to {(1600) / 2, 814}
tell window 1 to set position to {0, 0}
end tell
end tell
I am trying to work on positioning of the window "Right half of the screen" (technically I can calculate this by setting the X = screenWidth/2 but the issue is, the window of Xcode app some part of the screen is not visible the user where as if I calculate it from right I can ensure the entire window is on the screen visible to the user.
Actual result: https://share.cleanshot.com/COYgKD
Expected result: https://share.cleanshot.com/BIELCA
Goal: Show right half of the window that ensure the entire window is on the screen visible to the user
Any help is appreciated
To obtain your your screen's dimensions:
tell application id "com.apple.finder" to set ¬
[null, null, screenW, screenH] to the ¬
bounds of the desktop's window
XCode is scriptable, I believe, so there's no need to invoke System Events to get or set its size and position. However, scriptable application windows offer this by way of the bounds property just like the desktop's window above:
tell application id "com.apple.Xcode" to tell ¬
window 1 to tell (a reference to its bounds)
set [X, Y, W, H] to its contents
set |𝚫𝓍| to W - screenW
set its contents to [X - |𝚫𝓍|, Y, W - |𝚫𝓍|, H]
end tell
This will right-align the window flush with the right edge of the screen, even if it's already fully visible on your screen. To have it only effect the move if it's required, then insert a condition such that |𝚫𝓍| > 0.
The following is a functionally identical version of the last code block, but utilising statements that are formulated in a more explicit manner:
tell application id "com.apple.Xcode" to tell ¬
the front window to if (it exists) then
set [X, Y, W, H] to the bounds
set |𝚫𝓍| to W - screenW
set the bounds to [X - |𝚫𝓍|, Y, W - |𝚫𝓍|, H]
end tell
I added a check to make sure the front window (which is identical to and equivalent terminology for window 1) exists, so in cases where there are no windows, the script won't throw an error.
Here, the bounds property is explicitly accessed by value whenever we need to retrieve or set its value, compared to the version above where the property was being accessed by reference. In this situation, there's no tangible difference between the two methods.
There is a script that lets you resize any app in mac. This is the code:
set theApp to "Application Name"
set appHeight to 1080
set appWidth to 1920
tell application "Finder"
set screenResolution to bounds of window of desktop
end tell
set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution
tell application theApp
activate
reopen
set yAxis to (screenHeight - appHeight) / 2 as integer
set xAxis to (screenWidth - appWidth) / 2 as integer
set the bounds of the first window to {xAxis, yAxis, appWidth + xAxis, appHeight + yAxis}
end tell
I want to change the size of a java application opened by a launcher. When I insert the name of any app, it works. However when I insert the name of the app that I want to resize it doesn't work. I know the process id of the app that I want to resize. Is there a way I can change this line set theApp to "Application Name" to use PID instead of Application name?
Thanks.
Not all apps are AppleScript scriptable and some that are do not support the bounds property, they use position property and size property. Also, sometimes you'll need System Events to position and size an app's window.
I use a keyboard shortcut assigned in FastScripts with the following example AppleScript code to automatically adjust the frontmost app's window. You can adjust the code to suite your needs.
If the frontmost app can't use the bounds property it silently errors, and then System Events does it.
tell application "System Events"
set frontmostProcess to name of process 1 whose frontmost is true
end tell
try
tell application frontmostProcess
set bounds of window 1 to {0, 22, 1136, 844}
end tell
on error
tell application "System Events" to tell application process frontmostProcess
set position of window 1 to {0, 22}
set size of window 1 to {1136, 822}
end tell
end try
Note: I am not affiliated with the developer of FastScript, just a satisfied user. It's also free for the first ten keyboard shortcuts.
I'm trying to do a screencast and for that, I want to record the VSCode window at the same size every time. For that I've tried to use an Automator script to resize the window. It works with all the applications that I've tested but not with VSCode.
This is the script:
(* This AppleScript will resize the current application window to the height specified below and center it in the screen. Compiled with code from Natanial Wools and Amit Agarwal *)
set the_application to (path to frontmost application as Unicode text)
set appHeight to 720
set appWidth to 1280
tell application "Finder"
set screenResolution to bounds of window of desktop
end tell
set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution
tell application the_application
activate
reopen
set yAxis to (screenHeight - appHeight) / 2 as integer
set xAxis to (screenWidth - appWidth) / 2 as integer
set the bounds of the first window to {xAxis, yAxis, appWidth + xAxis, appHeight + yAxis}
end tell
And this is the error that happens when I try to resize the VSCode. Does anyone know what this error could be? Or does anyone know any other way to enforce the window size?
The bounds property and the window element only exist for applications that are scriptable. Most applications, including Microsoft Visual Studio Code, are not scriptable, so your current script will not work for any of these.
To manipulate the window of a non-scriptable application, you need to script the UI using System Events:
tell application "System Events"
set [[screenW, screenH]] to process "Finder"'s scroll areas's size
tell (the first process where it is frontmost)
set [[null, menuH]] to the size of its menu bars
tell the front window
set [appW, appH] to its size
set its position to [¬
(screenW - appW) / 2, ¬
(screenH - appH + menuH) / 2]
end tell
end tell
end tell
I'm using the following Applescript to set up a .dmg folder's settings, icon spacing, and so on:
on run argv
tell application "Finder"
set diskname to item 1 of argv
tell disk diskname
open
set current view of container window to icon view
set toolbar visible of container window to false
set statusbar visible of container window to false
set bounds of container window to {400, 100, 900, 699}
set theViewOptions to the icon view options of container window
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 100
set background picture of theViewOptions to file "background.png"
set file_list to every file
repeat with i in file_list
if the name of i is "Applications" then
set the position of i to {368, 135}
else if the name of i ends with ".app" then
set the position of i to {134, 135}
end if
-- Change the 7 to change the color: 0 is no label, then red,
-- orange, yellow, green, blue, purple, or gray.
set the label index of i to 7
end repeat
update without registering applications
delay 4
end tell
end tell
end run
The window's being set up correctly, with one exception: the 500x600 background.png image is shrunk to about a third of the original size and placed in the upper left corner. I can't explain why this is happening. Any suggestions?
Boy, this is really st00pid.
I ran into this problem on OS X Lion. It turns out that Lion requires that the background image for a folder be set to 72 DPI. The image I was using was 200 DPI, so Lion scaled it down from its actual pixels.
I re-saved the image with a declared resolution of 72 DPI, and it works perfectly.