gpt4 book ai didi

macos - 调整所有正在运行的应用程序的所有窗口

转载 作者:行者123 更新时间:2023-12-03 18:42:35 26 4
gpt4 key购买 nike

我正在尝试编写一个 applescript 脚本,它允许我在选择时调整所有正在运行的应用程序的所有窗口的大小(我目前使用 Stay ,但有时发现它有问题,所以我想“重新发明”它)
我一直在关注一些 Applescripting 教程,并提出了以下代码来这样做,但它有问题:

tell application "Finder"
set rect to bounds of window of desktop
end tell

property excludedApplicationNames : {"Finder"}
tell application "System Events"
say "a"
repeat with theProcess in processes
say "b"
if background only of theProcess is false then
say "c"
set theProcessName to name of theProcess as string
if theProcessName is not in excludedApplicationNames then
say theProcessName
tell application theProcess
set bounds of windows of process theProcess to rect
end tell
end if
end if
end repeat
end tell
say "done"

问题是当这段代码遇到我唯一的终端窗口(有几个打开的选项卡)时,它会出错: System Events got an error: Can’t set application (item 2 of every process) to {0, 0, 1280, 900}.System Events got an error: Can’t set application (item 2 of every process) to {0, 0, 1280, 900}.
tell application theProcesstell application theProcessName没有帮助(相同的错误),也没有将其更改为 tell application "System Events" (错误: System Events got an error: Can’t make item 2 of every process into type integer.)

有趣的是,这按预期工作:
tell application "Finder"
set rect to bounds of window of desktop
end tell

tell application "Terminal"
repeat with theWindow in windows
set bounds of theWindow to rect
end repeat
end tell

所以我很困惑。
我究竟做错了什么?我怎样才能解决这个问题?

最佳答案

tell application "Finder"
set {0, 0, dtw, dth} to bounds of window of desktop
end tell
tell application "System Events"
repeat with p in (processes where background only is false)
tell p
if name is not in {"Finder"} then
set position of windows to {0, 0}
set size of windows to {dtw, dth}
end if
end tell
end repeat
end tell
  • 在我的 Mac 上花了大约 3 秒钟
  • 最大化终端窗口以填充屏幕(Dock 占用的 4px 区域除外)

  • tell application "Finder"
    set dtb to bounds of window of desktop
    end tell
    tell application "System Events"
    bundle identifier of processes where background only is false
    end tell
    repeat with bid in result
    tell application id bid
    try
    if name is not in {"Finder"} then
    set (bounds of windows where visible is true) to dtb
    end if
    end try
    end tell
    end repeat
  • 在我的 Mac 上花了大约 0.3 秒
  • 不适用于所有应用程序,例如 Preview 或 Reeder
  • 使用捆绑标识符,因为一些应用程序具有不同的进程和应用程序名称
  • 调整终端窗口的大小,使其上方和下方有几个像素的空白空间

  • 我使用这个脚本来最大化窗口:
    try
    tell application "Finder" to set dtb to bounds of window of desktop
    tell application (path to frontmost application as text)
    if name is in {"Terminal"} then
    error
    else
    set bounds of window 1 to dtb
    end if
    end tell
    on error
    tell application "System Events" to tell (process 1 where it is frontmost)
    try
    click (button 1 of window 1 where subrole is "AXZoomButton")
    end try
    end tell
    end try

    在许多不支持基本 AppleScript 的应用程序中,缩放按钮还会最大化窗口以填充屏幕。

    关于macos - 调整所有正在运行的应用程序的所有窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595066/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com