Opening and closing System Prefpanes with command line actions in a shell script

I am trying to automate a process for my wife and my daughter to backup their Time Capsules (5th Gen, Seagate drives) to an external hard disk *before* the TC disk dies (mine died recently after about 4 years of normal use).


Carbon Copy Cloner 5, which I have decided to use, can run preprocess and postprocess bash scripts.


The process I want to automate is:

WHEN MacBook is present and connected to Time Capsule AND external hard disk

then as CCC preprocess script

open Time Machine Preferences (to show progress)

backup MacBook (to Time Capsule) and wait for finish [tmutil startbackup -b]

close Time Machine Preferences

let CCC run and backup

in postprocess script, send an iMessage to one or the other iPhone.


CCC handles the Time Machine and external disk both present check.


in the preprocess script I can open and display Time Machine Preferences with


open -b com.apple.systempreferences /System/Library/PreferencePanes/TimeMachine.prefPane


MY QUESTION:

but HOW DO I CLOSE the PrefPane (its been a while, as an 80-year old, that I did any serious Mac programming)


for the postprocess script to send iMessage to phone, I have been able to steal some scripts from the Web.


Any help on closing Time Machine Preferences in a shell script would be appreciated


Trevor



MacBook Air 13″, macOS 10.13

Posted on Jan 16, 2023 9:20 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 17, 2023 2:32 PM

Thanks for the tip: when I found out how to run an Applescript from a bash script, I was able to run this bash command as a PreProcess in Carbon Copy Cloner;


osascript ~/Desktop/Scripts/BackupandWait.applescript


which does:


if running of application "System Preferences" then

quit application "System Preferences"

delay 1

end if

set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"

tell application "System Preferences"

open thePathPref & "TimeMachine.prefPane"

delay 1

do shell script "tmutil startbackup -b"

delay 30

end tell

if running of application "System Preferences" then

quit application "System Preferences"

delay 1

end if


Similarly, when Carbon Copy Cloner has finished backing up the Time Capsule, the Postprocess script does


osascript ~/Desktop/Scripts/iMessageSend.applescript +41XXXXXXXXX "Your MacBook has done a Time Machine backup to your Time Capsule, and Carbon Copy Cloner has backed up your Time Capsule to your USB external disk Drive."


(the two arguments are Phonenumber in International format, and Text message in quotes).

The Phonenumber needs to be in your contacts, and not associated with yourself. Family member can forward the iMessage to you. CCC will email you on completion if so set.


The script is


on run {targetBuddyPhone, targetMessage}

tell application "Messages"

set targetService to 1st service whose service type = iMessage

set targetBuddy to buddy targetBuddyPhone of targetService

send targetMessage to targetBuddy

end tell

end run


For unfortunate family members in your Contacts who have Android phones, change servicetype = SMS



My question thus answered: hope others may find this useful


cheers, Trevor



Similar questions

8 replies
Question marked as Top-ranking reply

Jan 17, 2023 2:32 PM in response to BDAqua

Thanks for the tip: when I found out how to run an Applescript from a bash script, I was able to run this bash command as a PreProcess in Carbon Copy Cloner;


osascript ~/Desktop/Scripts/BackupandWait.applescript


which does:


if running of application "System Preferences" then

quit application "System Preferences"

delay 1

end if

set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"

tell application "System Preferences"

open thePathPref & "TimeMachine.prefPane"

delay 1

do shell script "tmutil startbackup -b"

delay 30

end tell

if running of application "System Preferences" then

quit application "System Preferences"

delay 1

end if


Similarly, when Carbon Copy Cloner has finished backing up the Time Capsule, the Postprocess script does


osascript ~/Desktop/Scripts/iMessageSend.applescript +41XXXXXXXXX "Your MacBook has done a Time Machine backup to your Time Capsule, and Carbon Copy Cloner has backed up your Time Capsule to your USB external disk Drive."


(the two arguments are Phonenumber in International format, and Text message in quotes).

The Phonenumber needs to be in your contacts, and not associated with yourself. Family member can forward the iMessage to you. CCC will email you on completion if so set.


The script is


on run {targetBuddyPhone, targetMessage}

tell application "Messages"

set targetService to 1st service whose service type = iMessage

set targetBuddy to buddy targetBuddyPhone of targetService

send targetMessage to targetBuddy

end tell

end run


For unfortunate family members in your Contacts who have Android phones, change servicetype = SMS



My question thus answered: hope others may find this useful


cheers, Trevor



Jan 17, 2023 3:09 PM in response to BDAqua

Anybody know how to get one's own Mac to iMessage oneself? Would be useful for debugging scripts and not waking up the spouse or child at ungodly hours. Messages App does not allow self-buddying. Straight use of any old international phone number would be an open invitation to script kiddies and worse....

Trevor

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Opening and closing System Prefpanes with command line actions in a shell script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.