我正在撰寫一個腳本來打開/關閉 macOS 上的麥克風
if input volume of (get volume settings) is 0 then
if storedInputLevel exists then
set volume input volume storedInputLevel
log "unmute it"
end if
else
tell application "System Events"
-- save the current input volume setting --
set storedInputLevel to input volume of (get volume settings)
end tell
set volume input volume 0
log "mute it"
end if
上述腳本僅在麥克風音量的初始狀態 > 0 時才有效,否則會拋出錯誤“storedInputLevel”不存在。
storedInputLevel 的目的是分配用戶用于配置的麥克風音量。我的目標是打開/關閉麥克風音量。
任何幫助表示贊賞
uj5u.com熱心網友回復:
在第一行中,您要求一個不存在的變數。exists不能用于區域變數,只能用于屬性或元素。
基本上你需要一個地方來永久存盤一個值。
在過去,您可以簡單地使用屬性來做到這一點。但是由于腳本可以進行代碼簽名并且修改屬性會破壞簽名,因此它不再可靠。
另一種方法是將值存盤在用戶默認值中。該值存盤在屬性串列檔案中 ~/Library/Preferences/com.myself.muteinput.plist
換成com.myself.muteinput你喜歡的。System Events不需要。
try
set storedInputLevel to (do shell script "defaults read com.myself.muteinput value") as integer
on error
set storedInputLevel to 0
end try
if input volume of (get volume settings) is 0 then
set volume input volume storedInputLevel
log "unmute it"
else
set storedInputLevel to input volume of (get volume settings)
do shell script "defaults write com.myself.muteinput value -int " & (storedInputLevel as text)
set volume input volume 0
log "mute it"
end if
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/440598.html
