【问题标题】:Make Applescript Editor record keys that were press制作 Applescript Editor 记录按下的键
【发布时间】:2016-01-27 02:07:34
【问题描述】:

我对 applescript 还很陌生(我知道一两件事),但我无法用它做一些事情。好的,问题是我需要一个脚本来记录/监控我按下的键。但不是通过直接将其输入到像这个脚本这样的输入框中:

display dialog "" default answer "" buttons {"OK"} default button 1
set answer to text returned of the result
display dialog answer buttons {"OK"} default button 1

例如,如果我在浏览器中键入“www.google.com”,然后返回到 applescript,它会记录“www.google.com” 感谢您在创建此脚本时提供的任何帮助。

【问题讨论】:

  • AppleScript 无法做到这一点
  • @vadian 好的,但是你确定绝对没有办法做我上面提到的事情。或者有什么可以接近它的吗?
  • 不适用于普通 AppleScript,因为 AS 基本上在单线程上运行。要监视键盘事件,您至少需要第二个线程以避免阻塞 UI。也许 AppleScriptObjC 或 AS 结合 Cocoa 可以做到

标签: macos applescript


【解决方案1】:

如果您只想要在浏览器中输入的 URL(使用 Safari),您只需这样做:

tell application "Safari" to get URL of first document

但是,我相信您想获取网页上特定输入字段中的内容,对吧?然后你可以这样做: 1)将“qwerty”写入您有兴趣阅读的输入字段。 2) 在 AppleScript Editor 上运行此脚本:

tell application "Safari"
    tell first document
        set numberOfInputFields to do JavaScript "document.getElementsByTagName(\"input\").length;"
        repeat with i from 0 to numberOfInputFields - 1
            set inputValueScript to "document.getElementsByTagName(\"input\")[" & i & "].value"
            set inputValue to (do JavaScript inputValueScript)
            if inputValue is equal to "qwerty" then
                display alert "Index of input field: " & i
                return
            end if
        end repeat
    end tell
end tell

然后你会得到输入字段的索引。将该数字放入此脚本并点击运行:

tell application "Safari"
    tell first document
        set indexOfInputField to "place-the-number-here"
        set inputValueScript to "document.getElementsByTagName(\"input\")[" & indexOfInputField & "].value"
        set inputValue to (do JavaScript inputValueScript)
    end tell
    display alert "Text in input field " & indexOfInputField & ": " & inputValue
end tell

您将获得输入到该字段中的内容(“qwerty”,如果您没有从上一个脚本更改它)。如您所见,这些脚本使用 JavaScript 从网页中读取。如果我的答案不是您想要的,您可以尝试其他 JavaScript 命令,例如找到 here

【讨论】:

  • 谢谢,你们太棒了。希望我能理解所有这些编码/脚本的一半含义。 :) 但我希望有一天我能到达那里......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2011-04-02
  • 1970-01-01
相关资源
最近更新 更多