AppleScript专注于预览应用程序中的窗口

时间:2018-03-18 18:56:29

标签: window focus automator

我试图使用Automator进行预览操作。我在预览中打开了几个窗口,其中两个是感兴趣的。一个名为“Markup Badges.png”,一个名为“Screenshot.png”

我想将焦点设置为“Screenshot.png”,复制图像并关闭窗口,然后我想关闭“Markup Badges.png”窗口。

我在使用它时遇到了很多麻烦。

作为我的实验的一部分,我创建了两个试图让窗口成为​​焦点的脚本,因此我可以对它们执行其他操作

脚本1:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<div id="myDiv">
  <div class="form-horizontal">

    <div class="form-group">
      <label for="BusinessName" class="control-label col-lg-2 col-sm-4">Business</label>
      <div class="col-lg-10 col-sm-8">
        <input id="BusinessName" class="form-control" placeholder="Business Name" name="BusinessName" style="max-width:75%;" is-required validate-string />
      </div>
    </div>

    <div class="form-group">
      <label for="BusinessDescription" class="control-label col-lg-2 col-sm-4">Description</label>
      <div class="col-lg-10 col-sm-8">
        <input id="BusinessDescription" class="form-control" placeholder="Business Description" style="max-width:75%;" />
      </div>
    </div>

    <div class="form-group">
      <label for="BusinessDBResidence" class="control-label col-lg-2 col-sm-4">Database</label>
      <div class="col-lg-10 col-sm-8">
        <input id="BusinessDBResidence" class="form-control" placeholder="Business DB Residence" name="DBResidence" style="max-width:75%;" is-required validate-string />
      </div>
    </div>

    <div class="form-group">
      <label for="BusinessConnection" class="control-label col-lg-2 col-sm-4">Connection</label>
      <div class="col-lg-10 col-sm-8">
        <input id="BusinessConnection" class="form-control" placeholder="Business ConnectionString" name="ConnectionString" style="max-width:75%;" is-required validate-string />
      </div>
    </div>
  </div>
</div>

脚本2:

on run {input, parameters}

    tell application "Preview"
        set visible of every window whose visible is true to false
    end tel

    tell application "Preview"
        try
            set theWindow to 1st window whose name begins with "Screenshot.png"
            set index of theWindow to 1
            activate
        end try
    end tell

    -- stuff
    delay 1

    tell application "Preview"
        set visible of every window whose visible is false to true
    end tell

    return input
end run

如果我自己运行脚本1,它会按照我的预期运行,会显示Screenshot.png窗口并具有焦点。

如果我自己运行脚本2,它会按照我的预期运行,显示Markup Badges.png窗口并具有焦点。

如果我运行两个脚本(自动播放器中的播放按钮,脚本1运行,然后是脚本2),则脚本2不能像它自己那样工作。显示窗口,但它没有焦点意味着我无法向窗口发送任何按键。

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

我发现将代码更改为下面修复了问题。

on run {input, parameters}

    tell application "Preview"
        set visible of every window whose visible is true to false
    end tell


    tell application "Preview"
        try
            set theWindow to 1st window whose name begins with "Markup Badges.png"
            set index of theWindow to 1
            activate
            tell application "System Events" to tell process "Preview" to perform action "AXRaise" of window 1
        end try
    end tell

    -- stuff
    delay 1

    tell application "Preview"
        set visible of every window whose visible is false to true
    end tell
    return input
end run