【问题标题】:Positioning a window with AppleScript using dual monitors使用双显示器使用 AppleScript 定位窗口
【发布时间】:2011-08-17 11:35:52
【问题描述】:

我设置了两台显示器,我试图将应用程序的窗口定位在第二台显示器中,但我所做的似乎没有任何效果。例如,我正在使用我的笔记本电脑,并且终端窗口在屏幕上最大化。然后我插上外接显示器。然后我想运行 applescript 并让终端在更大的第二个监视器上最大化。

这是我现在拥有的:

set monitorTwoPos to {1050, -600}
set monitorTwoSze to {1200, 1920}

tell application "Microsoft Outlook"
    set position of window 1 to monitorTwoPos
    set size of window 1 to monitorTwoSze
end tell

这是我得到的错误:

/Users/vcutten/AppleScripts/SpacesWork.scpt:1291:1332:执行错误: Microsoft Outlook 出现错误:无法将窗口 1 的位置设置为类型说明符。 (-1700)

我很确定我只是使用设置位置和设置大小完全错误:(当我使用边界时它有点工作......

奖金问题: 如何循环打开打开的窗口并获取它们的大小?谢谢!

【问题讨论】:

    标签: macos bash applescript multiple-monitors


    【解决方案1】:

    你试过什么?

    我认为要解决这个问题,您需要计算第二台显示器的屏幕尺寸和坐标。例如,您的主监视器从位置 {0,0} 开始。所以第二台显示器的起始位置必须不同,你需要找到它。幸运的是,我编写了一个工具,可以为您提供显示器的起始坐标和屏幕尺寸。一旦有了大小和位置,就很简单了。系统事件可以设置窗口的大小和位置,这样你就可以做这样的事情......

    set monitorSize to {800, 600}
    set monitorPosition to {-800, 0}
    
    tell application "System Events"
        tell process "Terminal"
            set frontWindow to first window
            set position of frontWindow to monitorPosition
            set size of frontWindow to monitorSize
        end tell
    end tell
    

    所以从上面的脚本中你只需要大小和位置变量。你可以得到我的工具here,叫做hmscreens,它会给你这些。您可能需要根据屏幕是从左下角还是从左上角测量坐标来调整坐标,但这只是简单的数学运算。

    希望对你有帮助……

    【讨论】:

    • 感谢您的回答!我用你问的内容更新了问题......我觉得我只是没有正确使用设置位置:(
    【解决方案2】:

    使用边界而不是位置,它有效。您可以像这样获得窗口的边界:

    tell application "Microsoft Outlook"
        get bounds of first window
    end tell
    

    奖励问题的答案:

    tell application "Microsoft Outlook"
        repeat with nextWindow in (get every window)
            get bounds of nextWindow
        end repeat
    end tell
    

    如果您打开 Applescript 编辑器底部的 Replies 选项卡,您将看到所有结果。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      这是一个脚本,用于处理保存和恢复多个显示配置的大小和位置。全屏应用程序可能存在一些问题,但似乎可以正常工作。

      -- allSettings is a list of records containing {width:? height:? apps:{{name:? pos:? size:?},...} 
      -- for each display setup store the apps and their associated position and size
      property allSettings : {}
      
      -- create a variable for the current settings
      set currentSettings to {}
      
      display dialog "Restore or save window settings?" buttons {"Restore", "Save"} default button "Restore"
      set dialogResult to result
      
      tell application "Finder"
      
          -- use the desktop bounds to determine display config
          set desktopBounds to bounds of window of desktop
          set desktopWidth to item 3 of desktopBounds
          set desktopHeight to item 4 of desktopBounds
          set desktopResolution to desktopWidth & "x" & desktopHeight
      
          -- find the saved settings for the display config
          repeat with i from 1 to (count of allSettings)
              if (w of item i of allSettings is desktopWidth) and (h of item i of allSettings is desktopHeight) then
                  set currentSettings to item i of allSettings
              end if
          end repeat
      
          if (count of currentSettings) is 0 then
              -- add the current display settings to the stored settings
              set currentSettings to {w:desktopWidth, h:desktopHeight, apps:{}}
              set end of allSettings to currentSettings
              --say "creating new config for " & desktopResolution
          else
              --say "found config for " & desktopResolution
          end if
      
      end tell
      
      tell application "System Events"
      
          if (button returned of dialogResult is "Save") then
              say "saving"
              repeat with p in every process
                  if background only of p is false then
                      tell application "System Events" to tell application process (name of p as string)
      
                          set appName to name of p
      
                          if (count of windows) > 0 then
                              set appSize to size of window 1
                              set appPosition to position of window 1
                          else
                              set appSize to 0
                              set appPosition to 0
                          end if
      
                          set appSettings to {}
      
                          repeat with i from 1 to (count of apps of currentSettings)
                              if name of item i of apps of currentSettings is name of p then
                                  set appSettings to item i of apps of currentSettings
                              end if
                          end repeat
      
                          if (count of appSettings) is 0 then
                              set appSettings to {name:appName, position:appPosition, size:appSize}
                              set end of apps of currentSettings to appSettings
                          else
                              set position of appSettings to appPosition
                              set size of appSettings to appSize
                          end if
      
                      end tell
                  end if
              end repeat
          end if
      
          if (button returned of dialogResult is "Restore") then
              if (count of apps of currentSettings) is 0 then
                  say "no window settings were found"
              else
                  say "restoring"
              repeat with i from 1 to (count of apps of currentSettings)
                  set appSettings to item i of apps of currentSettings
                  set appName to (name of appSettings as string)
                  try
                      tell application "System Events" to tell application process appName
                          if (count of windows) > 0 then
                              set position of window 1 to position of appSettings
                              set size of window 1 to size of appSettings
                          end if
                      end tell
                  end try
              end repeat
              end if
          end if
      end tell
      

      https://gist.github.com/cmackay/5863257

      【讨论】:

      • 没有找到窗口设置。脚本行为和属性的变化?
      • 我已经很多年没用过那个脚本了。我不确定它是否仍然有效。我目前使用一个名为 moom 的应用程序来管理我的窗口位置。 manytricks.com/moom
      猜你喜欢
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 2014-03-11
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      相关资源
      最近更新 更多