我有一本关于AppleScript的书,我想知道我是否应该保留.我已经了解JavaScript.
据我所知,Apple最近在OS X中添加了JavaScript脚本支持.
这是否意味着我不再需要学习AppleScript来为OS X应用程序编写脚本,还是比它更多呢?你的想法将不胜感激.
上述问题是从想要让应用程序自动执行操作的应用程序用户的角度出发的.
关于应用程序开发人员(Cocoa)的事情怎么样?如果他们想让他们的应用程序可编写脚本,他们可以不学习AppleScript吗?
更新:添加了赏金.问题被重新定义(对AppleScript及其用户没有任何不尊重):鉴于我已经了解JavaScript,我还有什么理由学习AppleScript吗?您对此事的详细想法将不胜感激.
我正在尝试将一些旧的Applescript移植到新的JavaScript语法中.
事情似乎非常直接,所以:
tell application "System Events" to keystroke "t" using command down
Run Code Online (Sandbox Code Playgroud)
成为:
System = Application('System Events');
System.keystroke("t", {using: "command down"})
Run Code Online (Sandbox Code Playgroud)
但是我不能为我的生活找出如何列出特定位置的文件.在AppleScript中,要返回目录中的文件列表/usr,您可以:
tell application "System Events" to set fileList to name of items in folder "/usr"
-- > {"bin", "include", "lib", "libexec", "local", "sbin", "share", "standalone", "X11"}
Run Code Online (Sandbox Code Playgroud)
但是我不能为我的生活弄清楚如何在Javascript中做到这一点.
System = Application('System Events')
myPath = Path("/usr")
fileList = System.items(myPath)
-- > message not understood
fileList = System.folder(myPath)
-- > message not understood
fileList = System.diskItems(myPath)
-- > []
fileList …Run Code Online (Sandbox Code Playgroud) javascript applescript automation osx-yosemite javascript-automation
在Yosemite中,现在可以使用JavaScript进行自动化以及Applescript.我在使用某些StandardAdditions命令时遇到问题.例如,从Contacts应用程序,我可以使用displayAlert,但不能使用displayNotification.两者都在StandardsAdditions字典中.通过ScriptEditor运行这些命令时,我没有遇到这些问题.
对于在运行时失败的命令:错误-10004:发生了权限违规.
JavaScript中的示例代码:
ScriptEditor = Application("Script Editor");
ScriptEditor.includeStandardAdditions = true;
app = Application("Contacts"); // or e.g. "Calendar", "System Events", "Finder"
app.includeStandardAdditions = true;
// -- testing: displayAlert()
ScriptEditor.displayAlert("Hello world!");
app.displayAlert("Hello world!"); // success, no privilege error
// -- testing: displayNotification()
ScriptEditor.displayNotification("Hello world!");
//app.displayNotification("Hello world!"); // Error -10004: A privilege violation occurred.
// --- testing: say()
ScriptEditor.say("Hello world!");
//app.say("Hello world"); // Error -10004: A privilege violation occurred.
// --- testing: beep()
ScriptEditor.beep(1); …Run Code Online (Sandbox Code Playgroud) 如果我使用standardadditions编写文本文件,我显然可以在参数包中配置编码.在AppleScript中,我会编写«class utf8»但在JXA中使用哪个值?我试过Strings"UTF8","utf8","class utf8"但没有成功.错误总是:"错误:无法转换类型.( - 1700)".如果我使用"text",则该文件是用MACROMAN编写的.
下面的独立示例:
var exportFileWriter = fileWriter('/Users/norman/mini.txt');
exportFileWriter.write('äöü');
exportFileWriter.close();
function fileWriter(pathAsString) {
'use strict';
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var path = Path(pathAsString);
var file = app.openForAccess(path, {writePermission: true});
/* reset file length */
app.setEof(file, {to: 0});
return {
write: function(content) {
/* How can I write UTF-8? */
app.write(content, {to: file, as: 'text'});
},
close: function() {
app.closeAccess(file);
}
};
}
Run Code Online (Sandbox Code Playgroud)
在foo回答后添加:
和
改为使用ObjectiveC桥.这个例子有效
str = $.NSString.alloc.initWithUTF8String('foo')
str.writeToFileAtomically('/tmp/foo', true)
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,下面的例子应该可行,因为约定如何在桥中命名ObjectiveC方法(删除冒号,添加camelcase),但事实并非如此.没有写入文件且返回值为false. …
我想在OS X Yosemite中使用Javascript for Automation在Mail.app中创建一个新的电子邮件消息并将文件附加到电子邮件中.这是我的代码:
Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "abc@example.com" }))
message.subject = "Test subject"
message.content = "Lorem ipsum dolor sit"
Run Code Online (Sandbox Code Playgroud)
它工作正常,直到这一点.我看到一个新的消息窗口,其中正确填写了收件人,主题和正文.但我无法弄清楚如何在邮件中添加文件附件.Mail.app的脚本字典表明contents属性(一个实例RichText)可以包含附件,但我不知道如何添加附件.
我试过这个,但是我收到一个错误:
// This doesn't work.
attachment = Mail.Attachment({ fileName: "/Users/myname/Desktop/test.pdf" })
message.content.attachments = [ attachment ]
// Error: Can't convert types.
Run Code Online (Sandbox Code Playgroud)
我在网上找到了几个例子,说明如何做到这一点的AppleScript中,比如这一个:
tell application "Mail"
...
set theAttachmentFile to "Macintosh HD:Users:moligaloo:Downloads:attachment.pdf"
set msg to make new outgoing message with properties {subject: theSubject, content: theContent, visible:true} …Run Code Online (Sandbox Code Playgroud) 我正在使用OS X JavaScript for Automation(JXA),我希望能够捕获"开放位置"的Apple Event.
根据http://www.macosxautomation.com/applescript/linktrigger/,我设置了一个客户URL处理程序.我该怎么做相同的
on open location this_URL
...
end open location
Run Code Online (Sandbox Code Playgroud)
与JXA?我尝试了以下所有方法,但无法执行任何操作:
app = Application.currentApplication();
app.includeStandardAdditions = true;
function run() {
app.displayDialog(JSON.stringify(arguments));
}
function openLocation() {
app.displayDialog(JSON.stringify(arguments));
}
function openDocuments() {
app.displayDialog(JSON.stringify(arguments));
}
function onOpenLocation() {
app.displayDialog(JSON.stringify(arguments));
}
Run Code Online (Sandbox Code Playgroud)
Apple的JXA文档(https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW15)don '讨论如何处理开放位置事件.我的脚本将被打开,因为如果我将其添加到函数之外,我可以显示警告.我只是无法获得一个函数来执行并在URL中传递.
我正在通过使用AppleScript处理程序来解决这个问题,然后调用我的JXA代码,但这肯定不太理想.
我也没有在JXA Cookbook(https://github.com/dtinth/JXA-Cookbook)中看到任何相关内容.
我正在尝试列出JXA对象的所有方法.我已经尝试了几种在浏览器中使用JavaScript的方法,但没有一种方法有效:
>> Object.getOwnPropertyNames(Application('Finder').selection()[0]);
=> ["__private__"]
>>
>> JSON.stringify(Application('Finder').selection()[0])
=> undefined
>>
>> console.dir(Application('Finder').selection()[0])
!! Error on line 1: TypeError: console.dir is not a function. (In 'console.dir(Application('Finder').selection()[0])', 'console.dir' is undefined)
>>
>> for(var m in Application('Finder').selection()[0]) { console.log(m); }
=> undefined
>>
>> console.log(Application('Finder').selection()[0])
2017-01-27 16:51:16.331 osascript[18617:633276] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFStringappendString:]: nil argument'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff77feb0db __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff8cc7da2a objc_exception_throw + 48
2 CoreFoundation 0x00007fff780689c5 +[NSException …Run Code Online (Sandbox Code Playgroud) 如果我有一个带有 JXA 源代码的字符串变量,有没有办法从 swift 运行它?看来 NSAppleScript 只适用于 AppleScript 源。
我想要这个 AppleScript 片段的 JXA 等效项:
tell application "Finder"
# Get path
set currentTarget to target of window 1
set posixPath to (POSIX path of (currentTarget as alias))
# Show dialog
display dialog posixPath buttons {"OK"}
end tell
Run Code Online (Sandbox Code Playgroud)
我得到的最接近的是使用该url属性来初始化Foundation NSURL对象并访问其fileSystemRepresentation属性,如下所示:
// Get path
var finder = Application('Finder')
var currentTarget = finder.finderWindows[0].target()
var fileURLString = currentTarget.url()
// I'd like to get rid of this step
var fileURL = $.NSURL.alloc.initWithString(fileURLString)
var posixPath = fileURL.fileSystemRepresentation
// Show dialog …Run Code Online (Sandbox Code Playgroud) 在哪里可以找到包含所有可用类、函数和参数的 JXA 文档?到目前为止发现只是随机代码示例和简短教程。
applescript ×6
osx-yosemite ×4
javascript ×3
automation ×2
macos ×2
cocoa ×1
finder ×1
macos-sierra ×1
scripting ×1
swift ×1
utf-8 ×1