如何从屏幕右侧计算窗口位置以确保其完整显示(AppleScript)
解决Xcode窗口定位到屏幕右半部分的问题
你遇到的问题是因为直接用屏幕宽度的一半作为X坐标时,Xcode窗口的实际渲染区域会超出屏幕边界。正确的做法是用屏幕总宽度减去窗口宽度来计算X坐标,确保窗口右边缘对齐屏幕右侧,完整显示在可见区域内。
以下是修正后的AppleScript代码:
tell application "System Events" -- 获取主屏幕的边界尺寸 set screenBounds to bounds of screen 1 set screenWidth to item 3 of screenBounds set screenHeight to item 4 of screenBounds -- 计算窗口尺寸(屏幕宽度的一半,高度匹配屏幕) set windowWidth to screenWidth / 2 set windowHeight to screenHeight tell application process "Xcode" activate tell window 1 set size to {windowWidth, windowHeight} -- 关键:用屏幕宽度减去窗口宽度得到X坐标 set position to {screenWidth - windowWidth, 0} end tell end tell end tell
代码说明:
bounds of screen 1:获取主屏幕的完整边界,包含屏幕的实际像素宽度和高度screenWidth - windowWidth:确保窗口的右边缘刚好对齐屏幕右侧,避免超出可见区域- 如果需要固定窗口高度为814,只需把
windowHeight替换为814即可
内容的提问来源于stack exchange,提问作者user4150758
相关产品推荐
相关产品推荐

