Using LiveCode to shutdown, logout and restart the Mac

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Using LiveCode to shutdown, logout and restart the Mac

Post by Mag » Fri Feb 28, 2014 7:52 pm

I wonder if it is possible to use LiveCode to perform operations as log out, restart and shut down your Mac... :oops:

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Fri Feb 28, 2014 8:31 pm

Mag wrote:I wonder if it is possible to use LiveCode to perform operations as log out, restart and shut down your Mac...
Yes, it's possible!

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Mon Mar 03, 2014 8:24 am

Hello Mag,

I'm wandering if my last post was enough :)
I made it few seconds before running out of my office on Friday evening...

That said, here are some shell commands which should help you:

- halt // shutdown immediately
- reboot // restart immediately
- shutdown -h +15 // in 15 mn
- shutdown -h 22:22 // at a secific time
- shutdown -r now // reboot immediately
- shutdown -h now // shutdown immediately
- etc.

All these commands can be executed by the Livecode shell() function.
You need the sudo command as a prefix to these commands
and it will ask for your password.

Unfortunately,you can't do that only via the LC shell()
as it opens a non-interactive shell process underneath.

There, expect comes to the rescue again:

Code: Select all

      get "expect -d -c" & quote &cr& \ 
                  "set timeout 1" &cr& \ 
                  "spawn sudo cp /Users/t/x.txt /"  &cr& \ 
                  "expect Password: { send 42cheese\r\n }" &cr& \ 
                  "expect eof" &cr& \ 
                  "sleep 1" &cr& \ 
                  "exit" & quote 
      get shell( IT)
   -- verify:
      put "shell: " & IT &cr& the result &cr& shell("ls -alst /x.*")
By the way, there is a book of 640 pages:
http://shop.oreilly.com/product/9781565920903.do

Be well,

Thierry
Attachments
sunny-expect.png
Regex LiveCode sunnYrex
https://sunny-tdz.com

Klaus
Posts: 11497
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Klaus » Mon Mar 03, 2014 12:56 pm

Thierry wrote:I'm wondering if my last post was enough :)
Well, I found it quite amusing :D

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Mag » Mon Mar 03, 2014 2:13 pm

Wow, wonderful post Thierry! :lol: :D

THANK YOU
Attachments
images.jpeg
images.jpeg (15.6 KiB) Viewed 4335 times

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Mon Mar 03, 2014 2:25 pm

Klaus wrote:
Thierry wrote:I'm wondering if my last post was enough :)
Well, I found it quite amusing :D
So, I'm a happy chap if you smiled :)

Best,

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Mon Mar 03, 2014 2:31 pm

Mag wrote:Wow, wonderful post Thierry!
THANK YOU
You're welcome.

Umm, don't recognize myself in the picture.
You know, I've almost no more hair left, plus a white beard..

Regards,

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

EssoAir
Posts: 52
Joined: Thu Feb 20, 2014 7:53 pm

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by EssoAir » Thu Apr 17, 2014 6:57 pm

Mag wrote:I wonder if it is possible to use LiveCode to perform operations as log out, restart and shut down your Mac... :oops:
Wouldn't it be easier to do in AppleScript?

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Mag » Thu Apr 17, 2014 8:23 pm

EssoAir wrote:
Mag wrote:I wonder if it is possible to use LiveCode to perform operations as log out, restart and shut down your Mac... :oops:
Wouldn't it be easier to do in AppleScript?
Hi EssoAir, thank you for your post, at the end I done it with AppleScript. 8)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Fri Apr 18, 2014 8:12 am

EssoAir wrote:
Mag wrote:I wonder if it is possible to use LiveCode to perform operations as log out, restart and shut down your Mac... :oops:
Wouldn't it be easier to do in AppleScript?
Depends of your backgroung and knowledge, no?
And then Applescript is for Mac only!
Mag wrote:Hi EssoAir, thank you for your post, at the end I done it with AppleScript. 8)
Any chance to show us how you do it?

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Mag » Fri Apr 18, 2014 8:55 am

Hi Thierry,

With pleasure! Here is a sample stack.
Attachments
ShutDownComputer.livecode.zip
(1.06 KiB) Downloaded 91 times

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Fri Apr 18, 2014 9:56 am

Mag wrote:Hi Thierry,

With pleasure! Here is a sample stack.
Thanks Mag.

So, learning the finder's applescript dictionnary might be useful :)

Regards,

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Mag » Fri Apr 18, 2014 10:50 am

Hi Thierry,

Yes, and a lot of other applications... Some other samples...

Code: Select all

tell application "System Events"
    log out
end tell

Code: Select all

tell application "Finder"
    sleep
end tell
Some time ago, I made simple-sample stack for iTunes, you can find here:

http://forums.runrev.com/viewtopic.php? ... 128#p97918

And here I posted a Send Email with Mail sample:

http://forums.runrev.com/viewtopic.php? ... 48#p100224

The only problem is that if you plan to deploy the app in the Mac App Store is unlikely to be accepted because of the violation of the Sandbox.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 677
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Thierry » Fri Apr 18, 2014 11:03 am

Mag wrote:

Code: Select all

tell application "System Events" to log out

tell application "Finder" to  sleep
Umm, so for shutdown and sleep it's the Finder
and for log out, it's the system events..

Doesn't sound that easy for me :)
The only problem is that if you plan to deploy the app in the Mac App Store is unlikely to be accepted because of the violation of the Sandbox.
Well, it's the same with the shell then.
I think I've read we can use applescript in a sandbox app only to get information...
Is that true?

Regards,

Thierry
Regex LiveCode sunnYrex
https://sunny-tdz.com

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Using LiveCode to shutdown, logout and restart the Mac

Post by Mag » Fri Apr 18, 2014 1:19 pm

Hi Thierry, Database Events, Image Events, and System Events are applications that can be used by AppleScript and that run invisibly to users, every one has it's dictionary you can see them with the Applescript Editor.

The sandboxing imposes restrictions on what the application can do, I think that there si no a simple rule and the approval process perhaps it depends on who reviews the app. It may be helpful to read about the cases reported by the developers. Maybe what you say may be right. I have no experience about for now. If you find something useful to us let us know.

Post Reply

Return to “Mac OS”