本篇主要记录 Applescript 基础语法,以及利用 applescript 生成 omnifocus 每日报告

从 windows 转换到 macos,最近一直在不断折腾,这两天浏览 github 过程中,看到 omnifocus 可以搭配 applescript 开发出一些自动化脚本,因此想开发一个 omnifocus 每日生成 Markdown 报告的脚本。

Applescript 快速入门

脚本编写 Eg.

applescript 最大的特点就是语法简单,编写一个清理废纸篓的脚本如下:

tell application "Finder"
empty the trash
end tell

变量 及 运算

变量的赋值和运算方法如下:

-- 变量赋值
set width to 8
set height to 9 -- 变量运算
set area to width * height -- 支持运算符
-- + - * / ^;

文本处理

文本处理的方法同样简单易懂

set str1 to ""
set str2 to "test"

set result to "YK"

-- 拼接
set resultingString to nameOfActress & actressRating

-- 长度
set theLength to the length of "Neal"

-- 类型转换
set strToNumber to "16" as number
set numToStr to 12 as string

弹窗

set stringToBeDisplayed to "say hi"
display dialog "stringToBeDisplayed"
display dialog stringToBeDisplayed

数据类型:list

set exampleList to {1,2,3,"hahah",9}

-- 列表拼接
同样用 & 操作符

-- 变更元素
set item 2 of theList to "yang"
set the second item of theList to "yang"
set the 2nd item of theList to "yang"

-- 取全部列表
get exampleList

-- 取最后一个元素
set valueOfLastItem to item -1 of myList

-- 一次取多个元素
set shortList to items 2 through 5 of myList

-- 翻转数组
set reversedList to reverse of myList

-- 获取数组的长度
set theListLength to the length of {"ds",1,2,3}
set theListLength to the count of {"ds",1,2,3}

-- 随机取数
set x to some item of {1,2,3}

-- 自动类型转换
set myList to {"a"}
set myString to "b"

set result to myList & myStringls

set itemized to every character to "Nealyang"

-- 切分字符串时,可以自定义delimiters
set AppleScript's text item delimiters to " "
set AppleScript's text item delimiters to oldDelimiters

注释

-- 注释一行
# 注释一行
(xxx) 注释多行

条件语句

set ageEntered to 73
set myAge to 24
if ageEntered is myAge then
display dialog "You are as old as I as"
end if
say "this sentence is spoken anyway"

错误捕获

try
set x to 1 / 0
on error
display dialog "Error:" & the error_number & "." & the error_message buttons {"OK"}
end try

路径

路径分隔符用 :

函数

可定义需要参数和不需要参数的函数定义

on test(la)
display dialog lala
end test

至此,AppleScript 的基本语法已逐一学习,下面就利用上面的知识开(gai)发(xie)一个 OmniFocus 每日生成 report 的脚本,并利用 crontab 布置成定时任务每日执行。

OmniFocus 每日 Report 脚本

脚本生成的 Markdown 效果如下:

总体框架参考了https://github.com/mygeekdaddy/OF-Markdown-Task-List,细节上根据需求更改了 report 的结构

每日 report 配合检查能更好的对 GTD 进行实践

更改后的 AppleScript 如下:

set CurrDatetxt to short date string of date (short date string of (current date))
set dateYeartxt to year of (current date) as integer if (month of (current date) as integer) < 10 then
set dateMonthtxt to "0" & (month of (current date) as integer)
else
set dateMonthtxt to month of (current date) as integer
end if if (day of (current date) as integer) < 10 then
set dateDaytxt to "0" & (day of (current date) as integer)
else
set dateDaytxt to day of (current date) as integer
end if set str_date to "" & dateYeartxt & "-" & dateMonthtxt & "-" & dateDaytxt set theFilePath to "Macintosh HD:Users:Insomnia:Desktop:效率小结:小结" & str_date & ".md" set due_Tasks to my OmniFocus_task_list()
my write_File(theFilePath, due_Tasks) on OmniFocus_task_list()
set CurrDate to date (short date string of (current date))
set TomDate to date (short date string of ((current date) + days))
set TomTomDate to date (short date string of ((current date) + days * 2))
set Tom7Date to date (short date string of ((current date) + days * 7))
set CurrDatetxt to short date string of date (short date string of (current date))
set bigReturn to return & return
set smallReturn to return
set strText to "## " & CurrDatetxt & " 效率小结:" & smallReturn
tell application "OmniFocus" -- 1. 列出今日已完成的任务
tell default document
set refDueTaskList to a reference to (flattened tasks where (completion date > (CurrDate) and completion date ≤ TomDate and completed = true))
set {lstName, lstProject, lstContext, lstDueDate} to {name, name of its containing project, name of its primary tag, due date} of refDueTaskList
set strText to strText & bigReturn & "### 今日已完成 <span style="\"color:green\"">" & length of lstName & " 项任务 </span>" & ":" & bigReturn
set strText to strText & "|项目|任务|dateDue|" & smallReturn & "|--|--|--|" & smallReturn
repeat with iTask from 1 to count of lstName
set {strName, varProject, varContext, varDueDate} to {item iTask of lstName, item iTask of lstProject, item iTask of lstContext, item iTask of lstDueDate}
if (varDueDate < (current date)) then
set strDueDate to "**<span style="\"color:red\"">" & short date string of varDueDate & "</span>**"
else
try
set strDueDate to short date string of varDueDate
on error
set strDueDate to " " as string
end try
end if
set strText to strText & "|" & "`" & varProject & "`" & "|" & " " & strName & " " & "|" & strDueDate & "|" & smallReturn
end repeat
end tell -- 2. 列出今日未完成的任务
tell default document
set refDueTaskList to a reference to (flattened tasks where due date ≤ TomDate and completed = false)
set {lstName, lstProject, lstContext, lstDueDate} to {name, name of its containing project, name of its primary tag, due date} of refDueTaskList
set strText to strText & bigReturn & "### ️ 今日未完成 <span style="\"color:orange\"">" & length of lstName & " 项任务 </span>" & ":" & bigReturn
set strText to strText & "|项目|任务|dateDue|" & smallReturn & "|--|--|--|" & smallReturn
repeat with iTask from 1 to count of lstName
set {strName, varProject, varContext, varDueDate} to {item iTask of lstName, item iTask of lstProject, item iTask of lstContext, item iTask of lstDueDate}
if (varDueDate < (current date)) then
set strDueDate to "**<span style="\"color:red\"">" & short date string of varDueDate & "</span>**"
else
try
set strDueDate to short date string of varDueDate
on error
set strDueDate to " " as string
end try
end if
set strText to strText & "|" & "`" & varProject & "`" & "|" & " " & strName & " " & "|" & strDueDate & "|" & smallReturn
end repeat
end tell -- 3. 列出过期的任务
tell default document
set refDueTaskList to a reference to (flattened tasks where due date < CurrDate and completed = false)
set {lstName, lstProject, lstContext, lstDueDate} to {name, name of its containing project, name of its primary tag, due date} of refDueTaskList
set strText to strText & bigReturn & "### ️ 已有 <span style="\"color:red\"">" & length of lstName & " 项任务严重延期 </span>" & ":" & bigReturn
set strText to strText & "|项目|任务|dateDue|" & smallReturn & "|--|--|--|" & smallReturn
repeat with iTask from 1 to count of lstName
set {strName, varProject, varContext, varDueDate} to {item iTask of lstName, item iTask of lstProject, item iTask of lstContext, item iTask of lstDueDate}
if (varDueDate < (current date)) then
set strDueDate to "**<span style="\"color:red\"">" & short date string of varDueDate & "</span>**"
else
try
set strDueDate to short date string of varDueDate
on error
set strDueDate to " " as string
end try
end if
set strText to strText & "|" & "`" & varProject & "`" & "|" & " " & strName & " " & "|" & strDueDate & "|" & smallReturn
end repeat
end tell -- 4. 列出明日已计划的任务
tell default document
set refDueTaskList to a reference to (flattened tasks where due date > TomDate and due date ≤ TomTomDate and completed = false)
set {lstName, lstProject, lstContext, lstDueDate} to {name, name of its containing project, name of its primary tag, due date} of refDueTaskList
set strText to strText & bigReturn & "### 明日已计划 <span style="\"color:blue\"">" & length of lstName & " 项任务 </span>" & ":" & bigReturn
set strText to strText & "|项目|任务|dateDue|" & smallReturn & "|--|--|--|" & smallReturn
repeat with iTask from 1 to count of lstName
set {strName, varProject, varContext, varDueDate} to {item iTask of lstName, item iTask of lstProject, item iTask of lstContext, item iTask of lstDueDate}
if (varDueDate < (current date)) then
set strDueDate to "**<span style="\"color:red\"">" & short date string of varDueDate & "</span>**"
else
try
set strDueDate to short date string of varDueDate
on error
set strDueDate to " " as string
end try
end if
set strText to strText & "|" & "`" & varProject & "`" & "|" & " " & strName & " " & "|" & strDueDate & "|" & smallReturn
end repeat
end tell -- 5. 列出未来七天的紧急和重要的任务
tell default document
set refDueTaskList to a reference to (flattened tasks where due date ≤ Tom7Date and completed = false)
set {lstName, lstProject, lstContext, lstDueDate} to {name, name of its containing project, name of its primary tag, due date} of refDueTaskList
set icount to 0
repeat with iTask from 1 to count of lstName
set varContext to item iTask of lstContext
if varContext = "*Important" or varContext = "+Urgent" then
set icount to icount + 1
end if
end repeat
set strText to strText & bigReturn & "### 7️⃣ 未来七天共 <span style="\"color:purple\"">" & icount & " 项**紧急/重要**任务 </span>" & ":" & bigReturn
set strText to strText & "|项目|任务|dateDue|" & smallReturn & "|--|--|--|" & smallReturn
repeat with iTask from 1 to count of lstName
set {strName, varProject, varContext, varDueDate} to {item iTask of lstName, item iTask of lstProject, item iTask of lstContext, item iTask of lstDueDate}
if varContext = "*Important" or varContext = "+Urgent" then
if (varDueDate < (current date)) then
set strDueDate to "**<span style="\"color:red\"">" & short date string of varDueDate & "</span>**"
else
try
set strDueDate to short date string of varDueDate
on error
set strDueDate to " " as string
end try
end if
set strText to strText & "|" & "️`" & varProject & "`" & "|" & " " & strName & " " & "|" & strDueDate & "|" & smallReturn
end if
end repeat
end tell -- 6. 列出未指定日期且未完成的任务
tell default document
set refDueTaskList to a reference to (flattened tasks where due date = missing value and completed = false)
set {lstName, lstProject, lstContext, lstDueDate} to {name, name of its containing project, name of its primary tag, due date} of refDueTaskList
set strText to strText & bigReturn & "### 仍有 <span style="\"color:turquoise\"">" & length of lstName & " 项任务未指定到期日 </span>" & ":" & bigReturn
set strText to strText & "|项目|任务|dateDue|" & smallReturn & "|--|--|--|" & smallReturn
repeat with iTask from 1 to count of lstName
set {strName, varProject, varContext, varDueDate} to {item iTask of lstName, item iTask of lstProject, item iTask of lstContext, item iTask of lstDueDate}
if (varDueDate < (current date)) then
set strDueDate to "**<span style="\"color:red\"">" & short date string of varDueDate & "</span>**"
else
try
set strDueDate to short date string of varDueDate
on error
set strDueDate to " " as string
end try
end if
set strText to strText & "|" & "`" & varProject & "`" & "|" & " " & strName & " " & "|" & strDueDate & "|" & smallReturn
end repeat
end tell end tell
strText
end OmniFocus_task_list --Export Task list to .MD file
on write_File(theFilePath, due_Tasks)
set theText to due_Tasks
set theFileReference to open for access theFilePath with write permission
write theText to theFileReference as «class utf8»
close access the theFileReference
end write_File tell application "Typora"
open file theFilePath
end tell

参考资料

https://segmentfault.com/a/1190000011273388

https://sspai.com/post/46912

Applescript快速入门及OmniFocus每日md报告开发的更多相关文章

  1. AppleScript 快速入门

    AppleScript 快速入门 AppleScript 顾名思义是苹果开发的一套脚本语言,利用 AppleScript 在 macOS 系统上可以对其他程序进行操作,点击按钮.发送消息.模拟自动化执 ...

  2. 快速入门系列--MVC--07与HTML5移动开发的结合

    现在移动互联网的盛行,跨平台并兼容不同设备的HTML5越来越盛行,很多公司都在将自己过去的非HTML5网站应用渐进式的转化为HTML5应用,使得一套代码可以兼容不同的物理终端设备和浏览器,极大的提高了 ...

  3. 程序员带你十天快速入门Python,玩转电脑软件开发(四)

    本系列文章立志于从一个已经习得一门编程语言的基础之上,全面介绍Python的相关开发过程和相关经验总结.本篇文章主要是基于上一篇的程序员带你十天快速入门Python,玩转电脑软件开发(三)的基础之上, ...

  4. 程序员带你十天快速入门Python,玩转电脑软件开发(三)

    声明:本次教程主要适用于已经习得一门编程语言的程序员.想要学习第二门语言.有梦想,立志做全栈攻城狮的你 . 如果是小白,也可以学习本教程.不过可能有些困难.如有问题在文章下方进行讨论.或者添加QQ群5 ...

  5. 程序员带你十天快速入门Python,玩转电脑软件开发(二)

    关注今日头条-做全栈攻城狮,学代码也要读书,爱全栈,更爱生活.提供程序员技术及生活指导干货. 如果你真想学习,请评论学过的每篇文章,记录学习的痕迹. 请把所有教程文章中所提及的代码,最少敲写三遍,达到 ...

  6. 程序员带你十天快速入门Python,玩转电脑软件开发(一)

    关注今日头条-做全栈攻城狮,学代码也要读书,爱全栈,更爱生活.提供程序员技术及生活指导干货. 如果你真想学习,请评论学过的每篇文章,记录学习的痕迹. 请把所有教程文章中所提及的代码,最少敲写三遍,达到 ...

  7. AndroidStudio快速入门四:打造你的开发工具,settings必备

    http://blog.csdn.net/jf_1994/article/details/50085825 前言:这里是使用AS的基本设置,适合新入手的朋友阅读,将这里介绍的设置完基本使用无忧啦. 1 ...

  8. 快速入门PaddleOCR,并试用其开发一个搜题小工具

    介绍 PaddleOCR 是一个基于百度飞桨的OCR工具库,包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别.竖排文本识别.长文本识别.同时支持多种文本检测.文本识别的训练算法. ...

  9. 快速入门系列--MVC--01概述

    虽然使用MVC已经不少年,相关技术的学习进行了多次,但是很多技术思路的理解其实都不够深入.其实就在MVC框架中有很多设计模式和设计思路的体现,例如DependencyResolver类就包含我们常见的 ...

随机推荐

  1. 如何使用原生的Ribbon

    什么是Ribbon 之前分析了如何使用原生的Feign,今天我们来研究 Netflix 团队开发的另外一个类库--Ribbon. Ribbon 和 Feign 有很多相似的地方,首先,它们本质上都是 ...

  2. 手写vue-router & 什么是Vue插件

    博文分享 这篇文章你可以学习到: 实现一个自己的vue-router 了解什么是Vue的插件 学习b站大佬后做的笔记整理和源码实现 1.1.3一步一步带你弄懂vue-router核心原理及实现哔哩哔哩 ...

  3. IDA*、剪枝、较难搜索、扫描——DNA sequence HDU - 1560

    万恶之源 翻译 题意就是给出N个DNA序列,要求出一个包含这n个序列的最短序列是多长 这是一道搜索题,为什么呢?从样例可以感受到,我们应该从左往右"扫描",从n个DNA序列中取出某 ...

  4. 两个栈实现队列 牛客网 程序员面试金典 C++ Python

    两个栈实现队列 牛客网 程序员面试金典 C++ Python 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. C++ //run:5ms memeory ...

  5. Cesium实现右键框选

    思路 1.先取消地图的右键事件 2.右键框选事件,屏幕坐标转为经纬度坐标 取消地图的右键事件 //此处容易犯一个错误:以为右键事件绑定了缩放功能,伪代码即 Cesium.MouseEvent.右键事件 ...

  6. k8s入坑之路(15)kubernetes共享存储与StatefulSet有状态

    共享存储 docker默认是无状态,当有状态服务时需要用到共享存储 为什么需要共享存储: 1.最常见有状态服务,本地存储有些程序会把文件保存在服务器目录中,如果容器重新启停则会丢失. 2.如果使用vo ...

  7. win10 vscode安装babel

    第一步:安装 babel-cli cd进入项目根目录,执行命令: npm install --global babel-cli 第二步:检测第一步是否成功,输入命令 babel --version,若 ...

  8. js中的特数值-null-undefined-NaN

    一.补充 1.js中的三大特殊数据:undefined.null.NaN NaN :非法的数值运算得到的结果 特殊之处: 是一个数值型数据,但不是一个数字 NaN不等于任何值,和任何数据都不相等,Na ...

  9. 1组-Alpha冲刺-3/6

    一.基本情况 队名:震震带着六菜鸟 组长博客:https://www.cnblogs.com/Klein-Wang/p/15544334.html 小组人数:7人 二.冲刺概况汇报 王业震 过去两天完 ...

  10. 我個人喜歡的一些Ubuntu的相關配置

    1.vim vim安裝: sudo apt-get install vim-gtk vim美化:刚安装的VIM,可能界面并不是十分友好,我们可以更改vim的配置文件,按照我们的需求去修改它.在命令行下 ...