以编程方式关闭或休眠沙盒应用程序Mac [英] Programmatically shut down or sleep sandboxed application Mac

查看:216
本文介绍了以编程方式关闭或休眠沙盒应用程序Mac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Mac App Store创建一个应用程序,该应用程序将在用户设置的时间后关闭/休眠Mac.我曾尝试使用AppleScript,但如果我要在沙盒模式下使用它,那将无法正常工作,我曾尝试向Google寻找解决方案,但似乎无法弄清楚.

I am trying to make an application for the Mac App Store that will shut down/sleep the Mac after a user-set time. I have tried to use AppleScript, but that won't work if I am going to use it in Sandbox mode, I have tried to Google for a solution, but I cant seem to figure it out.

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

Hope someone can give me a hint or link to relevant documentation.

使其更加精确地满足我的期望.

edit: made it more precise to what I desire to accomplish.

推荐答案

我曾经写过一个脚本.但是我不知道它是否仍然有效.也许它至少为您提供了一条线索.睡眠仍然有效-如果程序阻止了关机,则关机可能会导致问题.

I wrote a script once. But I don't know whether it still works. Maybe it gives you at least a clue. Sleep still works - shutdown maybe causes problems if programs are blocking the shutdown.

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

(Not the prettiest code but it worked for me.)

#/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"文件中添加此文件(注意,如果您不知道如何处理此文件!-> google):

I also had to add this in my '/etc/sudoers' file (CAUTION, if you don't know how to handle this file! -> google):

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

这篇关于以编程方式关闭或休眠沙盒应用程序Mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆