我想要做的是(在 Mac OS X / MacOS 中)使用 osascript 創建一個對話框。我想給它添加一個圖示。這部分很容易并且有效。
然而,問題是如果用戶處于暗模式或亮模式,我的圖示檔案看起來會不正確。因此,我有兩個 .icns 檔案,它們存盤在我環境中所有 Mac 上的特定目錄中(該目錄位于 /Library/ctxsit/)。我可以使用變數呼叫一個或另一個,但是當我添加一個函式來確定要使用的檔案時,它失敗了。
首先,起作用的部分是:
icon=(/Library/ctxsit/CompanyIconB.icns)
title="Company IT Notification"
echo $title
prompt=$(osascript -e "display dialog \"Click Continue to logout from current user and set this Mac back to the Welcome Screen. This will leave the current user directory and profile intact.
Please make sure your work is saved. After Continuing, the device will appear to be in a Fresh OS state.
\" buttons {\"Cancel\", \"Continue\"} with title \"$title\" default button 2 with icon \"$icon\" ")
if [[ $prompt == "button returned:Continue" ]]
then
sleep 2
prompt=$(osascript -e "display dialog \"Are you sure? Clicking Continue will run the script and Restart this Mac.
\" buttons {\"Continue\", \"Cancel\"} with title \"$title\" default button 2 with icon \"$icon\" ")
fi
如果 Mac 處于黑暗模式,那么我的代碼的第一行就會有
icon=(/Library/ctxsit/CompanyIconW.icns)
以上所有內容都在我的腳本的“作業”版本中。所以,我在“非作業”版本中添加了一個功能。
接下來,不起作用的部分是:
#!/bin/bash
currentOS=$(sw_vers -productVersion)
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )
#mode=$(sudo -u "$currentUser" defaults read -g AppleInterfaceStyle)
OScheck=$([[ (($currentOS < 10.14)) ]]; echo $?)
function setIcon()
{
if [[ $OScheck == "1" ]]; then
echo "Mojave or newer"
if [[ $mode == "Dark" ]]; then
echo "Dark mode enabled"
icon="CompanyIconW.icns"
else
echo "Light mode enabled"
icon="CompanyIconB.icns"
fi
else
echo "Older than Mojave"
icon="CompanyIconB.icns"
fi
}
setIcon
echo $icon
title="Company IT Notification"
echo $title
prompt=$(osascript -e "display dialog \"Click Continue to logout from current user and set this Mac back to the Welcome Screen. This will leave the current user directory and profile intact.
Please make sure your work is saved. After Continuing, the device will appear to be in a Fresh OS state.
\" buttons {\"Cancel\", \"Continue\"} with title \"$title\" default button 2 with icon \"$icon\" ")
if [[ $prompt == "button returned:Continue" ]]
then
sleep 2
prompt=$(osascript -e "display dialog \"Are you sure? Clicking Continue will run the script and Restart this Mac.
\" buttons {\"Continue\", \"Cancel\"} with title \"$title\" default button 2 with icon \"$icon\" ")
fi
最后,當我從終端運行這個腳本時,我得到了這個結果:
bash-3.2$ ./Scratch2.sh
Mojave or newer
Light mode enabled
CompanyB.icns
Company IT Notification
0:393: execution error: A resource wasn’t found. (-192)
0:134: execution error: A resource wasn’t found. (-192)
我能想到的唯一部分是在我的“作業”版本中,我有一個實際的檔案路徑,而我的“非作業”版本不包括那部分。這引出了我的主要問題:
如何根據函式的回傳/結果/退出代碼/設定變數?
例如,無論計算機處于哪種模式(亮、暗),使用檔案 1 表示亮,檔案 2 表示暗。
uj5u.com熱心網友回復:
您分配值圖示的方式是“正確的”,但我認為您的條件運算式是錯誤的:
OScheck=$([[ (($currentOS < 10.14)) ]]; echo $?)
除了您使用<而不是-lt,十進制算術在 Bash 中不起作用,因此您將不得不依賴外部工具,例如bc:
OScheck=$(echo "$currentOS < 10.14" | bc) # 1 = true, 0 = false
相應地更新您的案例陳述。
作為旁注,除了第一個是對陣列的顯式分配,第二個不包含目錄路徑之外,icon=(/Library/ctxsit/CompanyIconB.icns)幾乎與其他相同icon="CompanyIconB.icns"。
還要避免使用命令替換從函式中“回傳值”,因為它非常低效。改用臨時全域變數。
# Don't do this!
function x { echo "This is my result."; }
my_var=$(x)
# Do this instead.
function x { __="This is my result."; }
x
my_var=$__
更新
正如評論中提到的,將版本號比較為小數是不準確的,所以我建議使用一個比較它們的函式。這需要檢查輸入引數的格式。
function compare_versions {
local a=${1%%.*} b=${2%%.*}
[[ "10#${a:-0}" -gt "10#${b:-0}" ]] && return 1
[[ "10#${a:-0}" -lt "10#${b:-0}" ]] && return 2
a=${1:${#a} 1} b=${2:${#b} 1}
[[ -z $a && -z $b ]] || compare_versions "$a" "$b"
}
uj5u.com熱心網友回復:
一些 bash 的技巧
這是執行此操作的其他可靠方法......
考慮為什么避免使用 subshel??l是一個很好的做法,這里除了蘋果二進制檔案沒有其他分支。
填充變數:
read -r currentOS < <(exec sw_vers -productVersion)
while IFS=$' :\t\r\n' read -r lhs rhs ;do
[ "$lhs" = "Name" ] && currentUser=$rhs
done < <(exec Scutil <<<"show State:/Users/ConsoleUser")
比較版本號:
compareOSver() {
local osMaj osMin osSub cStr1 cStr2
local -i retVal
IFS=. read -r osMaj osMin osSub _ <<<"$1"
printf -v cStr1 d "$osMaj" "$osMin" "$osSub"
IFS=. read -r osMaj osMin osSub <<<"$2"
printf -v cStr2 d "$osMaj" "$osMin" "$osSub"
retVal="10#${cStr1} > 10#${cStr2} ? 1 : 10#${cStr2} > 10#${cStr1} ? 2 : 0"
return $retVal
}
將變數從函式傳遞到腳本:
function setIcon() {
local returnVar=${1:-iconRes}
compareOSver "$2" "$3"
case $? in
1 ) printf -v $returnVar 'Newer_OS.icn' ;;
2 ) printf -v $returnVar 'Older_OS.icn' ;;
* ) printf -v $returnVar 'Same_OS.icn' ;;
esac
}
So you could:
setIcon icon "$currentOS" 10.14
我第一次嘗試使用osascript:
DialogStr='Click Continue to logout from current user and set this Mac back to \
the Welcome Screen. This will leave the current user directory and profile intact.
Please make sure your work is saved. After Continuing, the device will appear to \
be in a Fresh OS state.'
title="Company IT Notification"
printf -v osaCmd 'display dialog "%s" buttons {"Cancel","Continue"} \
with title "%s" default button 2 with icon %d' \
"${DialogStr//$'\\\\\n'}" "$title" 0
IFS=: read -r _ userAnswer < <(exec osascript -e "${osaCmd//$'\\\\\n'}")
好的,我在這里找到:使用 osascript 時更改通知圖示是一種使用備用圖示的方式,正確的語法是icon alias "file:path:in:mac:format:icon.icn"
testIcon=/Volumes/Macintosh\ HD/Applications/Utilities/Terminal.app/Contents/Resources/term_icon.icns
然后
testIcon=${testIcon#/Volumes/}
printf -v osaCmd 'display dialog "%s" buttons {"Cancel","Continue"} \
with title "%s" default button 2 with icon alias "%s"' \
"${DialogStr//$'\\\\\n'}" "$title" "${testIcon//\//:}"
IFS=: read -r _ userAnswer < <(exec osascript -e "${osaCmd//$'\\\\\n'}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401007.html
