Programmatically shut down or sleep sandboxed application Mac

大城市里の小女人 提交于 2020-01-06 12:43:35

问题


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.


回答1:


There is no way to do this. You may try to get a temporary-exception to run "shutdown" or "halt" thru BSD-layer but I doubt that such an app will pass the App Store review as these are tasks that require superuser / admin rights.




回答2:


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

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


来源:https://stackoverflow.com/questions/21975699/programmatically-shut-down-or-sleep-sandboxed-application-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!