AppleScriptでリマインダーにタスクを自動で追加したい
JavaやCなら分かるのですが、AppleScriptについては詳しくわからず、ネットの情報を頼りに以下の.appを作成しましたが、うまく実行しませんでした。
・MacBookの充電を忘れることがあるので毎日決まった時間に以下の.appをカレンダーのカスタム通知で実行するようにしました。
・.appの内容は、バッテリー残量を取得してその値が80未満である且つ、タスクが重複していなければ、充電を促すタスクを作成するというものです。
・バッテリー残量の取得方法、タスクを追加するにはアプリが実行されていなければいけないなどの条件を知りたいです。
-- バッテリー残量を取得
set batteryLevel to (do shell script "pmset -g batt | grep -o '\\d\\+%' | tr -d '%'") as integer
-- オートメーションというリストを取得
tell application "Reminders"
set automationList to list "オートメーション"
set taskFound to false
-- タスクを検索
repeat with i from 1 to (count of reminders in automationList)
set taskName to name of reminder i of automationList
if taskName is equal to "MacBookの電池残量低下" then
set taskFound to true
exit repeat
end if
end repeat
-- タスクが見つからない場合かつバッテリー残量が80%未満の場合は、新しいタスクを作成
if not taskFound and batteryLevel < 80 then
set newTask to make new reminder at end of reminders of automationList with properties {name:"MacBookの電池残量低下"}
end if
end tell
MacBook Pro (2021)