2011-05-03 17 views
6

我有两个显示器设置,我试图在第二个显示器中定位应用程序的窗口,但是我没有做任何事情似乎可以工作。例如,我使用我的笔记本电脑,终端窗口在屏幕上最大化。然后我插入一个外部显示器。然后,我想运行applescript并让终端在较大的第二台显示器上最大化。使用双显示器定位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: execution error: 
Microsoft Outlook got an error: Can’t make position of window 1 into type specifier. (-1700) 

我敢肯定,我只是用一套位置和大小设置完全错误:(当我使用界限它的作品...

红利问题: 我怎样才能通过打开的窗口循环,并获得他们的大小?谢谢!

回答

2

你有什么试过的?

我认为要解决这个问题,您需要计算第二个显示器的屏幕尺寸和坐标。例如,您的主监视器从位置{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,它会给你那些。根据屏幕是从左下角还是左上角测量,您可能需要对坐标进行一些调整,但这只是简单的数学运算。

我希望有帮助...

+0

感谢您的回答!我更新了你问的问题...我觉得我只是没有正确使用设置的位置:( – gdoubleod 2011-05-05 06:27:52

0

使用边界而不是位置,它的工作原理。你可以在窗口的范围是这样的:

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编辑器的底部,你会看到所有得到结果。

希望它有帮助。

1

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

-- 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