-
-
Save lsloan/03acfe4973ed93616cb01e95b557de18 to your computer and use it in GitHub Desktop.
bash functions using osascript to use some user interaction on macOS
This file contains 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() { | |
python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");' | |
} | |
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