功能的实现,参考了几篇文章,在开篇,表示感谢!
引用了:Mac中的定时任务利器:launchctl
osx – 如果一个应用程序正在运行,而不启动它 – 通过osascript实用程序如何检查AppleScript
1.打开launchpad->脚本编辑器,写入如下代码
set appName to "XXApp(此处为app名称)"
if application appName is not running then
tell application "Finder"
activate
open application file "XXApp.app" of folder "Applications" of startup disk
end tell
return "Running"
else
return "Not running"
end if
保存到合适的路径后
2.进入~/Library/LaunchAgents文件夹,创建新的plist文件com.autoOpenXXApp
写下定时任务,此处各个命令详细意思,可参考上述文章
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Label唯一的标识 -->
<key>Label</key>
<string>com.demo.plist</string>
<!-- 指定要运行的脚本 -->
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Users/mac/Downloads/AutoOpenXXApp.scpt</string>
</array>
<!-- 指定要运行的时间 -->
<key>StartInterval</key>
<integer>10</integer>
<!-- 标准输入文件 -->
<key>StandardInPath</key>
<string>/Users/mac/Downloads/testScript.log</string>
<!-- 标准输出文件 -->
<key>StandardOutPath</key>
<string>/Users/mac/Downloads/testScript.log</string>
<!-- 标准错误输出文件 -->
<key>StandardErrorPath</key>
<string>/Users/mac/Downloads/testScript.log</string>
</dict>
</plist>
3.打开终端
cd ~/Library/LaunchAgents
挂载任务
launchctl load -w com.autoOpenXXApp.plist
开启任务
launchctl start com.autoOpenXXApp.plist
PS:如果更改了appleScript内容,需要重新挂载任务,先
launchctl unload -w com.autoOpenXXApp.plist
再重新挂载任务
launchctl load -w com.autoOpenXXApp.plist
4.停止任务
launchctl stop com.autoOpenXXApp.plist
不过我用这个好像没有停下来,所以我还用了
launchctl unload -w com.autoOpenXXApp.plist
有问题,多多交流