AppleScript:如何从 if() 语句内调用的事件返回 bool 值

标签 applescript

嗨——Applescript 核心在这里。

实际上,我本身并没有尝试运行下面的脚本,但我正在尝试找到一种可以实现此效果的 AppleScript 语言构造(在我熟悉的语言中效果很好,哈哈):

set adoc to choose file
tell application "Finder"
    tell application "TextEdit"
        open adoc
    end tell
    tell application process "Textedit" of application "System Events"
        if (click menu item "Print…" of menu "File" of menu bar item "File" of menu bar 1 of application process "TextEdit" of application "System Events") then
            display dialog "It worked boss!"
        end if
    end tell
end tell

基本上,我正在为旧应用程序编写 GUI 脚本,并且需要在每一步都知道事件是否发生。例如,我知道我可以通过询问“打印”窗口是否存在来推断事件的成功,但由于我不会进入的原因,我不想从预期的结果中推断事件,我想知道是否它发生了。有没有办法在 AppleScript 中做到这一点?谢谢!


有趣的是,出于我愚蠢而深奥的目的,所提供的两个答案的组合让我克服了编写一些非常旧的应用程序的麻烦。在某些情况下,可能存在具有类似按钮的两个可能窗口之一,其中 {x,y} 解决方案(就我的目的而言,在某些情况下更有效)不起作用,因为我仍然可以正确单击错误的按钮,其中尝试...错误策略的应用(我实际上觉得没有考虑到这一点是愚蠢的),这并没有给我同样的精度,因为我正在使用的一些 UI 元素很奇怪并且不t 表现如预期(或具有预期的属性),至少克服了这个问题。感谢大家将我从这场噩梦中拯救出来!

最佳答案

只是另一种方法。对于操作和点击,成功时它将返回该对象或另一个对象。匹配这些对象以确保除目标之外没有其他对象收到该操作。

tell application "System Events"
    tell process "Safari"
        if (count of windows) < 1 then return --there are no windows, no reason to continue
        tell window 1
            tell checkbox 1 of group 1
                if (click it) is not it then
                    --click has failed; stop 
                    return
                end if
            end tell
        end tell
    end tell
end tell

--编辑:为 adayzdone 添加了一些示例代码,以向他展示它如何与打印一起使用

tell application "Safari" to activate --comment//uncomment this line
tell application "System Events"
    tell process "Safari"
        set theTarget to menu bar item 3 of menu bar 1
        set {xPos, yPos} to position of theTarget
        if (click at {xPos, yPos}) is not theTarget then
            return false
        end if
        set theTarget to last menu item of menu 1 of menu bar item 3 of menu bar 1
        set {xPos, yPos} to position of theTarget
        if (click at {xPos, yPos}) is not theTarget then
            return false
        end if
        return true
    end tell
end tell

关于AppleScript:如何从 if() 语句内调用的事件返回 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400240/

相关文章:

applescript - AppleScript-Safari帮助

macos - iTunes Application Loader-自动化

macos - 在 macOS AppleScript 中,如何检查属性是否存在?

applescript - 使用 AppleScript 在 Mac OSX Lion 上单击菜单项

c++ - 使用 applescript 启动应用程序时,我没有看到启动画面

iphone - 自动化 XCode build设置

javascript - 如何为我的 AppleScript SCPT 文件选择 JavaScript?

AppleScript 中的 Ruby 风格字符串插值

applescript - 截屏并以当前时间作为名称保存到桌面

macos - 使用 AppleScript 在新的 Safari 选项卡中打开 URL