可以使用启动器应用程序(launchd)来在后台执行AppleScript,而不必将其直接嵌入主应用程序中。以下是一个示例启动器应用程序的AppleScript代码:
on run
repeat
-- Add your AppleScript code here
end repeat
end run
on idle
return 5 -- This will cause the script to run every 5 seconds
end idle
将此代码保存为应用程序并使用launchd注册即可在后台执行。您可以使用以下终端命令添加它:
launchctl submit -l com.example.myapp /path/to/myapp.app
其中“com.example.myapp”是应用程序的标识符,而“/path/to/myapp.app”是应用程序的路径。您可以运行以下命令来启动应用程序:
launchctl start com.example.myapp
如果需要停止它,请运行:
launchctl stop com.example.myapp
这样,您的AppleScript将在后台运行,不会影响主应用程序的稳定性。