无法让 bash / apple 脚本使用调整打开窗口大小

问题描述 投票:0回答:1

我正在尝试编写一个 bash 脚本来创建一个新桌面,一次打开 4 个程序,然后移动这些程序并调整其大小以转到屏幕的左上角、右上角、左下角和右下角(在我的 Mac)。

到目前为止,我能够使前两个部分正常工作,但我无法正确调整窗口大小。

这是我尝试过的。

#!/bin/bash

app1="Zotero"
app2="Google Chrome"
app3="GoodNotes"
app4="Finder"

# Create a new desktop
osascript <<END
tell application "System Events"
  do shell script quoted form of "/System/Applications/Mission Control.app/Contents/MacOS/Mission Control"
  click button 1 of group "Spaces Bar" of group 1 of group "Mission Control" of process "Dock"
  do shell script quoted form of "/System/Applications/Mission Control.app/Contents/MacOS/Mission Control"
end tell
END

# Wait for Mission Control to open and switch to the new desktop
sleep 1
osascript -e 'tell application "System Events" to key code 124 using control down' # Press Control+→ to switch to the new desktop

# Open apps
open -a "$app1"
open -na "$app2" --args --new-window "https://example.com" # Replace with the desired URL for app2
open -a "$app3"
open -a "$app4" ~/Desktop

# Wait for a short time to ensure that the applications have started
sleep 2

# Get the process IDs of the applications
app1_pid=$(pgrep -i "$app1")
app2_pid=$(pgrep -i "$app2")
app3_pid=$(pgrep -i "$app3")
app4_pid=$(pgrep -i "$app4")

# Get the screen dimensions
screen_width=$(system_profiler SPDisplaysDataType | awk '/Resolution:/ {print $2}')
screen_height=$(system_profiler SPDisplaysDataType | awk '/Resolution:/ {print $4}')

# Calculate the width and height for each quarter
quarter_width=$((screen_width / 2))
quarter_height=$((screen_height / 2))

# Set the positions and sizes of the applications
osascript -e "tell application \"System Events\" to set position of window 1 of process $app1_pid to {0, 22}"              # Top-left
osascript -e "tell application \"System Events\" to set size of window 1 of process $app1_pid to {$quarter_width, $quarter_height}" # Resize to quarter size

osascript -e "tell application \"System Events\" to set position of window 1 of process $app2_pid to {$quarter_width, 22}" # Top-right
osascript -e "tell application \"System Events\" to set size of window 1 of process $app2_pid to {$quarter_width, $quarter_height}" # Resize to quarter size

osascript -e "tell application \"System Events\" to set position of window 1 of process $app3_pid to {0, $quarter_height}" # Bottom-left
osascript -e "tell application \"System Events\" to set size of window 1 of process $app3_pid to {$quarter_width, $quarter_height}" # Resize to quarter size

osascript -e "tell application \"System Events\" to set position of window 1 of process $app4_pid to {$quarter_width, $quarter_height}" # Bottom-right
osascript -e "tell application \"System Events\" to set size of window 1 of process $app4_pid to {$quarter_width, $quarter_height}" # Resize to quarter size

有人可以帮我修复这个脚本的后半部分吗?

bash applescript
1个回答
0
投票

试试这个代码:

osascript << EOF
tell application "Google Chrome"
    activate
    delay .5
end tell
tell application "System Events"
    tell process "Google Chrome"
        set frontWindow to the front window
        if exists frontWindow then
            set position of frontWindow to {0, 0}
            set size     of frontWindow to {1000, 500}
        end if
    end tell
end tell
EOF
© www.soinside.com 2019 - 2024. All rights reserved.