我在这里晚饭新来了,无论是Javascript还是JXA,所以请问我一些愚蠢的问题。但是我试图找出一种使用JXA从高亮显示的文本中获取字符串的方法-自动化的JavaScript,因为优胜美地可以在Automator中识别Javascript,所以我认为我可以对这些window.getSelection
进行处理:
function getSelectedText() {
if (window.getSelection) {
txt = window.getSelection();
} else if (window.document.getSelection) {
txt =window.document.getSelection();
} else if (window.document.selection) {
txt = window.document.selection.createRange().text;
}
return txt;
}
这段代码不是我的,有人发布了。但是我发现我无法在Automator中使用window
或document
在此处进行Mac OS的更改,因此有人可以向我展示如何将此Javascript代码转换为Automator可以理解的JXA吗?
非常感谢!
通常,您可以使用“系统事件”应用程序复制和粘贴任何应用程序。
'use strict';
//--- GET A REF TO CURRENT APP WITH STD ADDITONS ---
var app = Application.currentApplication()
app.includeStandardAdditions = true
var seApp = Application('System Events')
//--- Set the Clipboard so we can test for no selection ---
app.setTheClipboardTo("[NONE]")
//--- Activate the App to COPY the Selection ---
var safariApp = Application("Safari")
safariApp.activate()
delay(0.2) // adjust the delay as needed
//--- Issue the COPY Command ---
seApp.keystroke('c', { using: 'command down' }) // Press ⌘C
delay(0.2) // adjust the delay as needed
//--- Get the Text on the Clipboard ---
var clipStr = app.theClipboard()
console.log(clipStr)
//--- Display Alert if NO Selection was Made ---
if (clipStr === "[NONE]") {
var msgStr = "NO Selection was made"
console.log(msgStr)
app.activate()
app.displayAlert(msgStr)
}
有关更多信息,请参见:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句