【问题标题】:Start Google Chrome from Applescript and set position从 Applescript 启动 Google Chrome 并设置位置
【发布时间】:2019-06-17 22:55:17
【问题描述】:

我正在制作一个启动脚本来启动各种应用程序并设置它们的位置和大小。它适用于除 chrome 之外的所有内容。我需要一个脚本来检查 Chrome 是否打开,如果它打开当前窗口,如果它没有激活它。然后,设置它的大小和位置

-- Start terminal 
tell application "Terminal"
    if (exists window 1) then reopen
    activate
    do script "screenfetch"
end tell

delay .5

tell application "System Events" to tell process "Terminal"
    tell window 1
        set size to {960, 600}
        set position to {2880, 0}
    end tell
end tell

delay .5


-- Start Chrome
tell application "Google Chrome"
    if (exists window 1) then
        reopen
    else
        activate
    end if
end tell

delay .5

tell application "System Events" to tell process "Google Chrome"
    tell front window
        set size to {960, 600}
        set position to {1920, 0}
    end tell
end tell
delay .5

这适用于终端。它启动一个终端并将其放在我的第二台显示器的右上角。但是,对于 Chrome,它会:说“找不到 Chrome 的 Window 1”,或者,它会将 chrome 放在前面,并且不会设置其大小或位置。

【问题讨论】:

    标签: applescript


    【解决方案1】:

    只要有可能,最好直接告诉应用程序直接执行某项操作,而不是尝试通过系统事件对其进行路由。在这种情况下,Google Chrome 支持 AppleScript 标准套件中的基本 window 对象,因此您可以执行以下操作:

    tell application "Google Chrome"
        activate
        if ((count of windows) = 0) then
            make new window with properties {bounds:{1920, 22, 960, 622}}
        else
            set bounds of window 1 to {1920, 22, 960, 622}
        end if
    end tell
    

    【讨论】:

      猜你喜欢
      • 2012-09-30
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多