Time Machine. Backup now and notify of start/end

Here’s a script that starts TM and notifies when it started and ended (within delay) using terminal-notifier.
It also opens TM preferences in the background and refreshes a geeklet from GeekTool that parses the syslog

set TMDestinationVolume to do shell script "tmutil destinationinfo | awk '(/Name/) {print $3}'"
tell application "System Events" to set OSMountedVolumes to name of every disk

on TMStatusNotification(TMStatusMessage)
	do shell script "/opt/local/bin/terminal-notifier -sender com.apple.backup.launcher -activate com.apple.backup.launcher -title \"Time Machine\" -subtitle \"Status\" -message " & TMStatusMessage
end TMStatusNotification

if TMDestinationVolume is in OSMountedVolumes then
	do shell script "tmutil startbackup"
	tell application "System Preferences" to set the current pane to pane "Time Machine"
	TMStatusNotification("\"Started at $(date '+%Y.%m.%d %H:%M')\"")
	set TMStatus to 1
	repeat while TMStatus is 1
		tell application "GeekTool Helper" to tell shell geeklet "time machine" to refresh
		delay 30
		set TMStatus to (do shell script "/usr/bin/tmutil status | grep -c \"Running = 1\"") as integer
	end repeat
	TMStatusNotification("\"Finished at $(date '+%Y.%m.%d %H:%M')\"")
else
	TMStatusNotification("\"Volume not mounted\"")
end if

Unfortunately, for some very obvious error I don’t see, it never notifies of the end of operation and gives this error

tell current application
	do shell script "/usr/bin/tmutil status | grep -c \"Running = 1\""
		--> error "0" number 1

On a side note, I use only one disk, so I don’t know how I should parse in case you have more disks (see man tmutil for destinationinfo and > mark).

Operating System: Mac OS X (10.8.5)

Any help, please?

It seems that you don’t understand correctly what the puzzling instruction does.

do shell script "tmutil status"
(*
"Backup session status:
{
    ClientID = \"com.apple.backupd\";
    Running = 0;
}"
*)
set TMStatus to (do shell script "/usr/bin/tmutil status | grep -c \"Running = 1\"")
# returns 1 if the status contains "Running = 1"
# returns error "0" number 1 if the status doesn't contain "Running = 1"
# Use :
try
	(do shell script "/usr/bin/tmutil status | grep -c \"Running = 1\"")
	set TMStatus to 1
on error
	set TMStatus to 0
end try

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) dimanche 25 septembre 2016 11:22:15

Thanks Yvan. I reread TN 2065 on do shell script. In this particular case -c inverts exit code and return value, which made it a little confusing.

set TMDestinationVolume to do shell script "tmutil destinationinfo | awk '(/Name/) {print $3}'"
tell application "System Events" to set OSMountedVolumes to name of every disk

on TMStatusNotification(TMStatusMessage)
	do shell script "/opt/local/bin/terminal-notifier -sender com.apple.backup.launcher -activate com.apple.backup.launcher -title \"Time Machine\" -subtitle \"Status\" -message " & TMStatusMessage
end TMStatusNotification

if TMDestinationVolume is in OSMountedVolumes then
	do shell script "tmutil startbackup"
	tell application "System Preferences" to set the current pane to pane "Time Machine"
	TMStatusNotification("\"Started at $(date '+%Y.%m.%d %H:%M')\"")
	set TMRunning to true
	repeat while TMRunning
		tell application "GeekTool Helper" to tell shell geeklet "time machine" to refresh
		delay 10
		try
			do shell script "/usr/bin/tmutil status | grep -c \"Running = 1\""
		on error
			set TMRunning to false
		end try
		--log TMRunning
	end repeat
	TMStatusNotification("\"Finished at $(date '+%Y.%m.%d %H:%M')\"")
else
	TMStatusNotification("\"Volume not mounted\"")
end if

Browser: Safari 536.5
Operating System: Mac OS X (10.8.5)