MAC定时任务:利用launchctl配合AppScript写一个定时启动某APP的任务

功能的实现,参考了几篇文章,在开篇,表示感谢!
引用了: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
写下定时任务,此处各个命令详细意思,可参考上述文章





  
  Label
  com.demo.plist
  
  ProgramArguments
  
    osascript
    /Users/mac/Downloads/AutoOpenXXApp.scpt
  
  
  StartInterval
    10
    
    StandardInPath
    /Users/mac/Downloads/testScript.log
    
    StandardOutPath
    /Users/mac/Downloads/testScript.log
    
    StandardErrorPath
    /Users/mac/Downloads/testScript.log


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

有问题,多多交流

你可能感兴趣的:(MAC定时任务:利用launchctl配合AppScript写一个定时启动某APP的任务)