Apple の脅威の通知と金銭目当てのスパイウェアへの対策について

しばらく返答が寄せられていないようです。 再度ディスカッションを開始するには、新たに質問してください。

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)

投稿日 2023/10/16 23:08

返信
返信: 3

2023/10/16 23:28 @korin への返信

少し解決したので返信欄に追記です。

リマインダーで、実行済みタスクになっていてもタスクの検索でどうやらカウントされているみたいで

・アプリの実行前に特定のリストの実行済みタスクを削除する

必要があるのですが、実行済みかの判定はどのように行うのでしょうか。

また追加したい機能として、現在充電ケーブルに接続しているかも追加したいです。(充電保留中かは関係なく、ケーブルの接続可否だけで判定したい)


また、このテストの間にリマインダーを何回も開いたのですが、ある時何回強制終了してもずっとぐるぐるでフリーズした時があったのですが、関連があるのでしょうか。再起動したら直りました。


2023/10/17 01:54 @korin への返信

う〜ん、これでいけますかね?

バッテリー残量取得についてはうまく取得できているか、バッテリー搭載機を使ってないのでわかりません。

タスクが見つからない場合かつバッテリー残量が80%未満の場合には、10秒後にリマインドを設定。

既存のタスクがある場合かつバッテリー残量が80%未満の場合には、実行済みを解除し、10秒後にリマインドを設定。みたいな..


-- バッテリー残量を取得
set batteryLevel to (do shell script "pmset -g batt | grep -o '\\d\\+%' | tr -d '%'") as integer

-- 現在の日時
set curDate to (current date)

-- オートメーションというリストを取得
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
	
	if not taskFound and batteryLevel < 80 then
		-- タスクが見つからない場合かつバッテリー残量が80%未満の場合
		set newTask to make new reminder at end of reminders of automationList with properties {name:"MacBookの電池残量低下", remind me date:curDate + 10} -- タスクを新規作成し10秒後にリマインドを設定
	else if taskFound and batteryLevel < 80 then -- 既存のタスクがある場合かつバッテリー残量が80%未満の場合
		set completed of reminder "MacBookの電池残量低下" to false -- 実行済みを解除
		set remind me date of reminder "MacBookの電池残量低下" to (curDate + 10) -- 10秒後にリマインドを設定
	end if
	quit -- アプリを終了。必要かどうか?
end tell


AppleScriptでリマインダーにタスクを自動で追加したい

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple ID を使ってご参加ください。