以编程方式关闭或睡眠沙盒应用程序Mac

时间:2014-02-23 22:45:20

标签: macos shutdown appstore-sandbox

我正在尝试为Mac App Store制作一个应用程序,该应用程序将在用户设置的时间后关闭/休眠Mac。我曾尝试使用AppleScript,但如果我要在沙盒模式下使用它,那将无法工作,我已经尝试过谷歌寻求解决方案,但我似乎无法弄明白。

希望有人可以给我一个提示或链接到相关文档。

编辑:让我更加精确地完成我想要完成的任务。

2 个答案:

答案 0 :(得分:0)

没有办法做到这一点。您可能尝试通过BSD层运行“shutdown”或“halt”来获取临时异常,但我怀疑这样的应用程序是否会通过App Store审核,因为这些任务需要超级用户/管理员权限。

答案 1 :(得分:0)

我曾写过一次剧本。但我不知道它是否仍然有效。也许它至少给你一个线索。睡眠仍然有效 - 如果程序阻止关机,关机可能会导致问题。

(不是最漂亮的代码,但它对我有用。)

#/bin/sh
#v1.0.0
#by Sapphire

echo "-----------------------------------------------------"
echo "*                    Sleep Timer                    *"
echo "-----------------------------------------------------"
echo "Please enter option: Sleep '1' or shutdown '0'"
echo -n "Option: "
read option
while  :
do
if [ $option -eq 1 -o $option -eq 0 ]
    then
        break
fi
echo "<<ERROR: Only number: 1 or 0 allowed...>>"
echo "Please rnter option: Sleep '1' or shutdown '0'"
echo -n "Option: "
read option
done
echo "Please enter countdown (min):"
echo -n "Minutes: "
read shutDown
while  :
do
    if [  $shutDown -gt 0 -a $var -eq $var 2> /dev/null  ]
        then
            break        
    fi
    echo "<<ERROR: Positive number expected...>>"
    echo "Please enter countdown (min):"
    echo -n "Minutes: "
    read shutDown
done

echo "*****************************************************"

echo "Counter has been started"
echo "'/!\ Kill countdown with CTRL-C /!\'"
echo  -n "Envoking command in: "
date +"%H:%M %Z"
echo "*****************************************************"
if [ $option -eq 0 ]
then
    echo "Shutdown in: "
fi
if [ $option -eq 1 ]
then
    echo "Sleep in: "
fi
echo -e "\n       *------------------------------*       "

barCounter=0;
counter=0
((timeBarSize=$shutDown * 2))
bar="||||||||||||||||||||||||||||||"
barEmpty="                              "
((flag=0))
for ((i=shutDown*60;i>=0;i--));
do
    if ((counter >= timeBarSize ))
    then
    ((counter=0))
    ((barCounter=$barCounter + 1))
    fi
    ((counter=$counter + 1))
    ((timehh=$i/3600))
    ((timemm=$i/60 - timehh*60))
    ((timess=$i - timemm*60 - timehh*3600))
    if (( flag == 1 ))
    then

    echo $(tput cuu1)$(tput el)$(tput cuu1)$(tput el)$(tput cuu1)$(tput el)$(tput cuu1)$(tput el)$(tput cuu1)

    fi
    ((flag=1))
    echo -e "       |           $timehh:$timemm:$timess            |"
    echo -e "\r       *------------------------------*       "

    echo -e "\r       *${bar:0:$barCounter}${barEmpty:$barCounter:30}*       "
    echo -e "\r       *------------------------------*       "

    sleep 1;
done

if [ $option -eq 1 ]
then
    echo "Going to sleep..."
    sleep 1
    osascript -e 'tell application "System Events" to sleep'
elif [ $option -eq 0 ];
then
    echo "Shutting down..."
    sleep 1
    osascript -e 'tell application "System Events" to shut down'
else
    echo "No valid command found... doing nothing!"
fi

我还必须在我的'/ etc / sudoers'文件中添加此内容(注意,如果您不知道如何处理此文件! - &gt; google):

# Custom privilege
myUser ALL=NOPASSWD: /Users/myUser/Documents/sleepTimer.sh