قالب وردپرس درنا توس
Home / Apple / macos – OS X – Build a bash function to send notifications via osascript / Applescript

macos – OS X – Build a bash function to send notifications via osascript / Applescript



Hope someone can point out what I'm doing wrong. After looking through several threads, plus lots of experimentation, it feels like something is simply wrong. The threads I read include
How can I trigger a notification center alert from an AppleScript or shell script?
and
How to get a notification when my commands are finished

This script is intended for minimal technical users to perform assembly and disassembly parts. Right now it only supports SMB, but will handle NFS and other external mounting later. We have the limitation that the user's system will be the base OS X installation, therefore we cannot install other tools or libraries.

The problem is something that should be simple, and an alert window pops up when an event occurs, with appropriate text. This will include events such as partitioning, sharing without assembly, etc. To simplify future modifications, the preference is to create a function that can include a series of arguments for the title, message, and subtitle. This has proven to be much more difficult than expected. The problem seems to be giving arguments from bash to osascript.

Basically, I use a piece of code from randomor on How to get an alert when my commands are executed. The Say command makes it much easier to tell when the script is working, plus will give good voicemails.

  send_notify () {
cmd = $ @
local $ @ && si & # 39; Completed! & # 39; && osascript -e "view alert " Completed: $ 2  "with the title " $ 1
"" }

As long as there is no space in the argument string sent from the call function, both the wording and the alert function. When there is white space in the argument string such as "BAR 3" or in a variable being passed, the send_notify () function throws an error.

  ./ sharemount.sh: line 138: local: `37 & # 39 ;: not a valid identifier

It is interesting to say that the command fails to execute.

Part of the problem is my lack of understanding of what role "$ @" plays in this command. If anyone has an explanation, it will be very welcome.

Any help is greatly appreciated.

  Pre_Mount_Check () {
Current_Share = "$ (df -t smbfs --output = source 2> / dev / null | sed & # 39; 1d & # 39; | awk -F / & # 39; {print tolower ($ NF)} & # 39 ;) "
Current_MountPoint = "$ (df -t smbfs --output = target 2> / dev / null | sed & # 39; 1d & # 39;)"
logger -p Info "$ DebugLog" "Sharemount: Pre_Mount_Check Check to see if $ Share_Path already exists"

if [[ (-z "$Current_Share") && (-z "$Current_MountPoint") ]]; then
logs -p Info "$ DebugLog" "Sharemount: Pre_Mount_Check $ Share_Path on $ Mount_Point is not mounted, we will try to mount it"
return 0

elif [[ ( "$Current_Share" = "$Share_Name") && ( "$Current_MountPoint" = *"$Mount_Point"*) ]]; then
logs -p Info "$ DebugLog" "Sharemount: Pre_Mount_Check $ Share_Path already mounted, nothing to do, quit"
echo "$ Share_Path already mounted, nothing to do, quit 1"
exit 0

elif
[[ ( "$Current_Share" = "$Share_Name") ]]; then
logs -p Warning "$ DebugLog" "Sharemount: Pre_Mount_Check $ Share_Path already mounted, at $ Current_MountPoint, quits"
echo "$ Share_Path already mounted, at $ Current_MountPoint, ends 2"
send_notify "FOO" "BAR2" <<< This works
exit 36

elif [[ ( "$Current_MountPoint" = *"$Mount_Point"*) ]]; then
logs -p Warning "$ DebugLog" "Sharemount: Pre_Mount_Check $ Share_Name is already mounted on $ Mount_Point exit"
echo "$ Share_Name is already mounted on $ Mount_Point exiting 3"
send_notify "FOO" "BAR 3" <<< This fails
exit 0

fi
}

send_notify () {
cmd = $ @
local $ @ && si & # 39; Completed! & # 39; && osascript -e "view alert " Completed: $ 2  "with the title " $ 1  ""
}


Source link