在 .app 运行时触发脚本(AppleScript 或 JXA)? [英] Trigger a script (AppleScript or JXA) on .app running?

查看:49
本文介绍了在 .app 运行时触发脚本(AppleScript 或 JXA)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型计算机实验室,供学生在不受监督的情况下使用,并在网络上连接了一台打印机.我正在尝试实现一个简单的脚本添加警报对话框,其中包含有关打印机的所有规则,当他们从任意数量的不同应用程序中选择打印时,我需要弹出这些规则.

I have a small computer lab for students to use fairly unsupervised, with a printer attached on the network. I am trying to implement a simple scripting additions alert dialog with all the rules about the printer that I need to pop up when they select print from any number of different applications.

我正在尝试将脚本直接附加到用户/库/打印机目录(xxx.xxx.xxx.xxx.app)中的打印机本身,以便任何浏览器或 pdf 查看器等都会显示消息当他们尝试运行打印机时.

I am trying to attach the script directly to the printer itself in the User/Library/Printer directory, (xxx.xxx.xxx.xxx.app) so any browser, or pdf viewer, etc. will get the message displayed when they try to run the printer.

我曾尝试将 automator 与 applescript 结合使用,我曾尝试重命名打印机并将 applescript 称为打印机的名称,但到目前为止还不行.

I have tried using automator with applescript, I have tried renaming the printer and calling the applescript the name of the printer, so far no good.

我错过了什么?

推荐答案

在这个答案中,我将展示如何创建一个 JavaScript for Automation (JXA) 小程序,该小程序侦听应用启动和屏幕保护程序停止通知,然后显示警报当它收到一个时,从而产生问题中描述的预期结果.我还描述了如何调整这种方法来触发 AppleScript 脚本,从而产生问题标题中描述的特定行为.

In this answer I will show how to create a JavaScript for Automation (JXA) applet that listens for app-launch and screensaver-stop notifications and then displays an alert when it receives one, thereby producing the desired outcome described in the question. I also describe how this approach can be adapted to trigger an AppleScript script, which would produce the specific behavior described in the title of the question.

  1. 打开脚本编辑器应用并创建一个新文档
  2. 从窗口左上角附近的弹出窗口中,选择 JavaScript 而不是 AppleScript
  3. 粘贴下面提供的代码
  4. 通过在保存面板中将文件格式"更改为应用程序"并启用运行处理程序后保持打开状态"选项,将脚本保存为小程序.
  5. 通过从脚本"菜单中选择运行应用程序"来运行小程序
  6. 启动应用程序并通知提醒
  7. 启动然后停止屏幕保护程序并注意警报

代码

var me = Application.currentApplication(); me.includeStandardAdditions = true

ObjC.import('Cocoa')

ObjC.registerSubclass({
  name: 'MainController',
  methods: {
    'appDidLaunch:': {
      types: ['void', ['id']],
      implementation: function(notification) {
        var appName = notification.userInfo.objectForKey('NSApplicationName').js
        me.activate()
        me.displayAlert(`Hello, ${appName}!`, {message: 'Nice to meet you.'})
        Application(appName).activate()
      }
    },
    'screensaverDidStop:': {
      types: ['void', ['id']],
      implementation: function(notification) {
        me.activate()
        me.displayAlert('Goodbye, screensaver!', {message: 'It was nice knowing you.'})
      }
    }
  }
})

var controller = $.MainController.new

$.NSWorkspace.sharedWorkspace.notificationCenter.addObserverSelectorNameObject(controller, 'appDidLaunch:', $.NSWorkspaceDidLaunchApplicationNotification, undefined)

$.NSDistributedNotificationCenter.defaultCenter.addObserverSelectorNameObject(controller, 'screensaverDidStop:', 'com.apple.screensaver.didstop', undefined)

讨论

首先,小程序代码创建了一个名为MainController"的新类,它实现了appDidLaunch:"和screensaverDidStop:"两个方法.实现这些方法是为了使用 Standard Additions 中的显示警报"功能.

Discussion

First, the applet code creates a new class named 'MainController', which implements two methods, 'appDidLaunch:' and 'screensaverDidStop:'. These methods are implemented to use the 'display alert' functionality from Standard Additions.

接下来,applet 代码实例化此类的一个对象,并将该实例注册为应用启动和屏幕保护程序停止时发布的通知的观察者.

Next, the applet code instantiates an object of this class, and registers that instance as on observer of the notifications that are posted when apps are launched, and when the screensaver stops.

JXA 代码执行后小程序继续运行,当事件发生时,调用 JXA 函数.

The applet continues to run after the JXA code executes, and when the events occur, the JXA functions are invoked.

如果你想从 JXA 运行 AppleScript 脚本,你可以参考 这个问题.

If you want to run an AppleScript script from JXA, you can refer to the answer to this question.

如果您想让小程序更难以意外退出,您可以通过在小程序的 Info.plist 中将 LSUIElement 键设置为true"来使小程序成为UI 元素".

If you want to make it harder to quit the applet accidentally, you can make the applet a 'UI Element' by setting the LSUIElement key to 'true' in the applet's Info.plist.

最后,您可能希望将小程序添加到用户的登录项中,以便在重新启动后自动启动.

Finally, you might want to add the applet to the user's Login Items so that it starts automatically after a reboot.

这篇关于在 .app 运行时触发脚本(AppleScript 或 JXA)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆