使Applescript Editor记录按下的键

时间:2016-01-27 02:07:35

标签: macos applescript

我对applecript很新(我知道一两件事)但我无法用它来做事。好的,问题是我需要一个脚本来记录/监控我按下的键。但不能直接在这个脚本输入框中输入:

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"感谢您创建此脚本的任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您只想要在浏览器中输入的URL(使用Safari),您只需执行以下操作:

tell application "Safari" to get URL of first document

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

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