【问题标题】:AppleScript to focus on a window in Preview appAppleScript 专注于预览应用程序中的窗口
【发布时间】:2018-03-18 18:56:29
【问题描述】:

我正在尝试使用 Automator 对预览进行一些操作。我在预览中打开了几个窗口,其中两个很有趣。一个名为“Markup Badges.png”,一个名为“Screenshot.png”

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

我在让它工作时遇到了很多麻烦。

作为我实验的一部分,我创建了两个脚本,试图让窗口成为​​焦点,这样我就可以对它们执行额外的操作

脚本 1:

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

脚本 2:

    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
        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 将无法正常工作。窗口已显示,但没有焦点,这意味着我无法向窗口发送任何按键。

任何帮助表示赞赏

【问题讨论】:

  • 发错了。

标签: window focus automator


【解决方案1】:

我发现将代码更改为以下解决了问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多