Last active
December 10, 2022 15:51
-
-
Save scriptingosx/c086e8ffb77fe1525d179e8648e5ee02 to your computer and use it in GitHub Desktop.
bash functions using osascript to use some user interaction on macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export PATH=/usr/bin:/bin:/usr/sbin:/sbin | |
consoleUser() { | |
echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' | |
} | |
displaydialog() { # $1: message | |
message=${1:-"Message"} | |
user=$(consoleUser) | |
if [[ $user != "" ]]; then | |
uid=$(id -u "$user") | |
launchctl asuser $uid /usr/bin/osascript <<-EndOfScript | |
button returned of ¬ | |
(display dialog "$message" ¬ | |
buttons {"OK"} ¬ | |
default button "OK") | |
EndOfScript | |
fi | |
} | |
displaynotification() { # $1: message $2: title | |
message=${1:-"Message"} | |
title=${2:-"Script Notification"} | |
user=$(consoleUser) | |
if [[ $user != "" ]]; then | |
uid=$(id -u "$user") | |
launchctl asuser $uid /usr/bin/osascript <<-EndOfScript | |
display notification "$message" with title "$title" | |
EndOfScript | |
fi | |
} | |
displayfortext() { # $1: message $2: default text | |
message=${1:-"Message"} | |
defaultvalue=${2:-"default value"} | |
user=$(consoleUser) | |
if [[ $user != "" ]]; then | |
uid=$(id -u "$user") | |
launchctl asuser $uid /usr/bin/osascript <<-EndOfScript | |
text returned of ¬ | |
(display dialog "$message" ¬ | |
default answer "$defaultvalue" ¬ | |
buttons {"OK"} ¬ | |
default button "OK") | |
EndOfScript | |
else | |
exit 1 | |
fi | |
} | |
# run all functions | |
displaydialog "Hello" | |
name=$(displayfortext "Enter your name:" "$(consoleUser)") | |
displaynotification "Hello, $name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment