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

查看:134
本文介绍了在运行的.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.

我尝试将自动程序与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. 通过在保存面板中将文件格式"更改为应用程序"并启用在运行处理程序后保持打开状态"选项,将脚本另存为applet.
  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:".实施这些方法是为了使用标准添加中的显示警报"功能.

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.

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

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天全站免登陆