HELP!: Shell Scripts/Apple Script/Terminal

Discussion in 'Mac Programming' started by tresean1, Mar 13, 2012.

  1. tresean1, Mar 13, 2012
    Last edited: Mar 13, 2012

    macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #1
    Ok so I posted this in the Mac OS X forums and got no replies (although 596!!! people viewed it), so I figured it was in the wrong forums so I am re-posting here:

    Hey guys, I am pretty new to Mac, and sort of confused about some things. I know a lot of you guys are experts/pros and I was hoping for some in-depth help.

    Here is my dilemma: I am trying to create some type of shortcut on the desktop that, when clicked on, will automatically launch Terminal and execute a command that will put my Macbook into hibernate mode (or 'safe sleep' mode as Apple calls it). I am a native Windows user, so please bear with me as I do not know/recognize many Apple terms and slang and shortcuts.

    I have read places that I can create a script or something that reads:
    sudo pmset -a hibernatemode 5
    exit
    and save it as something like 'hibernate.command'? On the contrast, if I want it to sleep as normal I would create a script saying:
    sudo pmset -a hibernatemode 0
    exit
    and save it as something like 'sleep.command'? But the exact process is still unclear.

    Do I have this right at all? Can I then save a shortcut of the terminal script (or the file itself) to the desktop---so that all I have to do is double-click it and the Terminal automatically opens and executes then closes itself (the 'exit' command?), and I can just close my lid to put my computer in safe sleep/hibernation (it saves the current session to disk and powers down)? Or is there a script I can write to tell the computer to save an image of the current session to disk then power down itself automatically? What text editor do I need/use to do this? I hope someone out there knows/understands what I am trying to accomplish. Please help! lol

    If I have this all wrong, could someone please point me in the right direction, or could one of you experts perhaps walk me step-by-step thru this (please literally walk me step by step as if I am a dummy, lol----that's what I need, because I am trying to learn this), or post exactly what I need to do or send me a link link to the script written exactly as it's supposed to be? (Please, Please no smart-ass/degrading/badgering/begrudging comments like "why on earth would you want to hibernate your Mac, why not put it to sleep as normal?" or "I have mentioned this to you in various places/other forums" or "why not just use a third party software?". I have my reasons for wanting/needing to hibernate it---remember I am newbie to OS X, and your help/expertise is GREATLY appreciated).
     
  2. balamw, Mar 13, 2012
    Last edited: Mar 13, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #2
    pmset only sets what "sleep" will do. It just changes a setting in the NVRAM, kind of like changing a BIOS setting on a typical PC.

    You still need to tell the machine to sleep in the usual way.
    Or, as I have mentioned to you in various places. Use one of the many third party tools whose aim it is to make this easier.

    B
     
  3. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #3
    ^^yeah but see I don't wan't to use any third party softwares as have I read they may cause conflicts, etc----and besides I would like to do this the natural way and learn in the process, as I stated earlier:
     
  4. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #4
    But that's why everyone keeps telling you and others who ask that there is no natural way to do this on a Mac. What you are asking for is a hack/tweak and is not standard/supported behavior, so you will be on your own. Conflicts will arise because you are deviating from "natural" Mac behavior, which is sleep progressing to safe sleep when it is ready.

    As I have already mentioned, Lion's resume feature and shutting the Mac off and on again is the closest "out of the box" Mac feature to true hibernate.

    B
     
  5. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #5
    OK, thanks---got it; that's fine and dandy---I understand all of that, you have drilled that in my head long enough :rolleyes: ...I understand I have to do this manually and as I stated before, I don't have Lion (so all that automated "out of the box" Resume feature stuff is null and void in this case; now that we have that first step out of the way, I think you are missing the next step/true point of my post.

    I am trying to find a way to write some type of script/batch/executable (or whatever you Mac folks are calling it nowadays) I can save to the desktop that, upon double-clicking on it, will:
    1) Launch Terminal/Applescript/or whatever application necessary
    2) enter in the aforementioned "sudo pmset -a hibernatemode 5" command
    3) enter in the command to "sleep" the Mac (which, in this case would actually save the current session to harddrive and "hibernate" it [shut it down] based on mode 5 above)
    4) close/exit out of Terminal, etc

    [or any necessary/required variation or simplification of the above steps that would ultimately and inclusively yield the same exact results]

    Then the only physical intervention from me would be to just potentially close the lid and go about my day; and resume where I left off upon pressing the power button again and having OS X restore my previous session. :D

    Does that further clarify/explain what I aim to accomplish?
     
  6. balamw, Mar 13, 2012
    Last edited: Mar 13, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #6
    I already specified three physical and one scripting way (with a link with a lot more information) to accomplish the "go to sleep" action in my post above. (Once pmset is set to your liking).

    I would recommend simply closing the lid, as I have occasionally seen my work Mac wake up briefly when it is already asleep and I close the lid. (I presume the magnetic switch saw an "open" instead of a close.

    EDIT: I recommend reading this fine article by Mike Ash "Getting Answers"

    B
     
  7. macrumors 68030

    Joined:
    Oct 19, 2011
    Messages:
    2,977
    Location:
    Switzerland
    #7
    You can make a small application using Automator. In Automator you can put your shell command (no need to save the script anywhere). This should give you an App that you can double click to enter the sleep mode you want.
     
  8. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #8
  9. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #9
    THANK YOU SO MUCH, Thundersteele! Finally, I am getting closer to my answer. :D :) So:
    1) I assume "Automator" is in my Apps folder?
    2) Can I put multiple commands into one "App"?
    3) So the entire strings of commands would read:
    sudo pmset -a hibernatemode 5
    osascript -e 'tell application "System Events" to sleep'
    exit terminal

    4) Do I have the above syntax exactly correct (I'm more concerned if the exit terminal command is correct or even necessary)? If not, please correct me :)

    Once again, thanks for the update/breakthrough/insight
     
  10. balamw, Mar 13, 2012
    Last edited: Mar 13, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #10
    From the 10.6.8 man page for pmset:

    The "exit terminal" will run once you come out of sleep to clean up after itself. EDIT: You need to get things in to Terminal first.

    EDIT 2:
    Terminal isn't launched this way, so no exit terminal is needed.

    The problem you will have is that sudo requires interactive access to get your password in order to get admin rights. This results in an error dialog box. Getting admin rights to do the pmset within Automator or Applescript is probably more work than you want.

    B
     
  11. macrumors G4

    Joined:
    Jan 5, 2006
    Messages:
    11,012
    Location:
    Redondo Beach, California
    #11
    Is this just a programming exercise so you can learn how this works? In that case you write a small app that does nothing but put the mac to sleep mode then you drag that app to the dock. You can write your app in "anything" for Objective C to Apple script to Shell script.

    If there is already a short cut to do this with one mouse click. Look under the apple menu. If this is a notebook then simply close the lid.

    When you select a method to write the app, some are faster. Using a shell script that runs in a terminal is slow to start up because it has to first start the terminal app and then run a shell to read your script. This takes 20 times longer than other methods
     
  12. macrumors 68030

    Joined:
    Oct 19, 2011
    Messages:
    2,977
    Location:
    Switzerland
    #12
    This will be a problem. I don't know how to get around it, and one should be careful!


    @OP
    Automator is an app that is included in OSX. You can do all sorts of things there, and I only used it once to do something console related, I think it was to launch a program with a set of command line options.
     
  13. balamw, Mar 13, 2012
    Last edited: Mar 13, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #13
    Funny, I already mentioned that earlier in the thread.

    ;)

    Once the right pmset mode has been selected (25 on my MBP running 10.6.8). Sleep becomes hibernate.

    Apple is making it harder for scripts and apps to make use of elevated privileges, this is partly why SmartSleep has not been updated in the MAS

    This is good for preventing malware. So I do not recommend bypassing security with SUID on pmset or the like, but that is an option.

    EDIT: If you absolutely have to implement it this way, you can take a look at this: http://aricsblog.blogspot.com/2006/09/running-shell-commands-with-automator.html which gives some examples of how to get the password in a GUI and pass it to sudo. The article is old (2006), but the instructions may still work.

    B
     
  14. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #14
    1) I read somewhere that the newer Intel Macs have something called Secure Virtual Memory or something, that requires a 5 instead of a 3. Am I right on this, could you please verify that?

    2) So are you saying that I can completely omit the exit terminal command altogether when I write the App in Automator?

    3) I did not know the sudo command requires a password. So is there an alternative command that would do the same thing? Or is there something I can incorporate into the script that would automatically insert my password (my password is blank anyway) like for example:
    if 'administrative password' then insert ********?

    I know that syntax is probably not right, but you guys know what I'm trying to say...lol

    Well, actually this isn't an exercise "just to see how this works"...it is essentially something that I will be utilizing, as I often utilize the Hibernate feature on my PC.

    And yes I want to write an "App" or script in something, at this point I don't care which method I use, I just want to do it the most effective and simplest way. So you're saying using Terminal is slow....so which method do you suggest? And how would I word the syntax? Which program would I use? I want to creat an executable that is click-able that would complete the aforementioned multiple steps all by itself.

    And yes I have an Intel Macbook Pro, and closing the lid will simply put it in Sleep mode which I do not want, I want it to "hibernate".
     
  15. macrumors 68030

    Joined:
    Oct 19, 2011
    Messages:
    2,977
    Location:
    Switzerland
  16. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #16
    Sorry I am just seeing this, I must have been typing my response as you replied. You are saying you have Snow Leopard and have your mode set to 25, but according to what I have read in different places "hibernatemode change hibernation mode, 0 for RAM powered while sleeping; 1 for RAM contents written to disk and system totally unpowered; 3 for RAM both powered and written to disk; 5 is the same as mode 1, but for use with secure virtual memory; 7 is the same as mode 3, but for use with secure virtual memory; 25 for hibernation in OS X Lion, with RAM contents written to disk and system totally unpowered", 25 is only for Lion, and I would need to use 1 or 5. But since you say are currently using it, I'm assuming it will still work in the earlier 10.6.8 too??? :confused:

    Yes...but Google, and the results thereof, mean nothing if I don't even understand the basis/language/slang/syntax of what the search results are trying to help with. :cool: That'd be like telling a person who has never worked on an engine, to refer to a Bentley or Haynes manual, lol.
     
  17. balamw, Mar 13, 2012
    Last edited: Mar 13, 2012

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #17
    This is why you need to verify this for yourself on your own Mac. Read the man page, heed the warnings. My work Mac on 10.6.8 has secure virtual memory on (EDIT: verified in Settings) and is currently in hibernatemode = 3.

    You have to be careful about looking at random 6 year old guides. Like: http://www.macworld.com/article/1053471/sleepmode.html

    It serves no purpose and is not a shell command. "exit" is enough to terminate a shell script.

    As per the link I provided you can use:

    Code:
           -S          The -S (stdin) option causes sudo to read the password from the standard input instead of
                       the terminal device.
    
    to send the password to sudo via echo, but it means you have to hard code your password in the script (bad idea) or do more work to collect the password (again read the link I provided). Or you can SUID pmset so it always runs as root. (again bad idea).

    All but the "do more work" diminish the security on your Mac.

    Again, just because you are used to doing something one way, doesn't make it necessary or even really useful. You can set sleep to become like hibernate with only a single call to pmset. Your script doesn't have to do both things. Once you've told your Mac to hibernate instead of sleep, just use the usual sleep commands when you want it to hibernate.

    Writing a script to do something which is already part of the OS in the Apple menu, power button, magnetic switch is going to be inherently slow. Writing it in Objective-C 2.0 might make it faster/fast enough but will require a large learning curve.

    Opening Terminal (manually) to enter
    Code:
    sudo pmset -a hibernatemode=XX
    once in a while when you want to change what sleep does isn't slow, because you should not have to do it often. (You can save that command in a file ~/hibmode.sh and run that instead of trying to wrap that in GUI-ness.)

    Something like (from Terminal):
    Code:
    echo "#!/bin/bash
    sudo pmset -a hibernatemode 25" > ~/hibmode.sh
    chmod +x ~/hibmode.sh
    echo "#!/bin/bash
    sudo pmset -a hibernatemode 3" > ~/slpmode.sh
    chmod +x ~/slpmode.sh
    
    would create a pair of scripts that would allow you to toggle back and forth by running ~/hibmode.sh from Terminal and will prompt you for a password.

    Unless, you have told it ONCE using pmset that you would rather that it hibernate every time. For my MacPookPro8,2 running 10.6.8 that seems to be hibernatemode=25 according to the documentation Apple provides in the form of the man page.


    ----------

    Open Terminal on your Mac.

    type "man pmset"

    read the man page for your mac

    What does it say?

    I can only tell you what my MacbookPro8,2 running 10.6.8 tells me, and Apple advises you not to mess with.

    B
     
  18. tresean1, Mar 13, 2012
    Last edited: Mar 13, 2012

    macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #18
    WHOA!!! Information overload....lol :eek: :confused: :eek:
    Although I do appreciate greatly the in-depth response.

    Please, please remember I am an absolute newbie to Mac, so please bear with me and try not to throw too much at me at one time...lol. Seems I am highly competent in everything, except Mac language...lol. I am trying to grasp all of this. So as a recap:
    1) I am currently using my work PC to type this so I am not in front of my Macbook to check what my current mode is set to, but I assume it is set to 0 or 3 (or 0 or 7, depending on whether I have Secure Virtual Memory?) because when I close the lid it just goes into regular sleep [but I can verify this by typing man pmset and then pmset -g | grep hibernatemode when I get home, right?].
    2) Upon checking my current mode (whatever it is set at), according to you I can just run a script that says sudo pmset -b hibernatemode 25 -c hibernatemode 0 (to have different actions for battery power and charger power---tells my laptop to immediately hibernate if I am on battery power and close the lid, and to just behave as normal sleep if I am using my charger and close the lid) and enter my password by using the command line???.....(ok this is where you lost me with all of this [especially the bolded stuff]...lol):
    OK, so first of all I have no clue who or what sudo is :confused: Next, provided the above information and the fact that I don't have a password (in other words, my password is blank), what exact commands steps would I have to use to write a part of the script to get my password (which is blank/non-existent) into sudo and then tell sudo to automatically supply my password (which is blank/non-existent) and then carry out the task as instructed?

    Ok, I understand, a little, the basis of where you're going with this but I'm still having trouble deciphering the echo, bin, bash, chmod +x, >~/, #!, and all of that terminology; I have no idea what all of that stuff means and what those commands are actually telling the computer to do. But yes essentially that's what I would like (an "on" and an "off" mode, so to speak). :D

    EDIT: Actually, after thinking about it, that's not exactly what I want...it works in theory, but I still want some type of click-able automated script-thingy that runs all the commands in the background. I want 2 executable/clickable files/apps that serve the purpose of 1st file/app) setting seperate hibernate parameters for battery power and charger power 2nd file/app) revert the original sleep modes for both batter power and charger power. So I would use them as toggle swiches so to speak, when I want my laptop to sleep normally I would just click the file named 'normal sleep'; and when I want specific hibernation modes, I would just click the file named 'hibernate'.
     
  19. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #19
    This is why everyone's first reaction to this is "don't".

    Yes, and these will be the correct settings for your Mac.

    There be dragons.

    sudo is a tool to temporarily get administrative rights on a Unix machine. It generally will not let you use it (for security reasons) if your account does not have a password set. (At least this was the case on Linux when I used that on my desktop 10 years ago. Also seems to be the case for Macs e.g. http://hintsforums.macworld.com/showthread.php?t=80118

    Read the link I provided earlier. http://aricsblog.blogspot.com/2006/09/running-shell-commands-with-automator.html, read the man page for sudo and learn a bit about bash scripts.

    Just creating the files with tools you can execute in one go instead of telling you to open a text editor, enter code, save it, etc...

    Well then you've got plenty of learning to do.

    Read all the links that have been provided. Learn how to do things manually from Terminal and then try and make that into a clickable script.

    This is exactly why the third party tools exist and also why they are dangerous and can cause conflicts etc...

    It isn't trivial.

    B
     
  20. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #20
    :confused: :confused:
     
  21. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #21
    I didn't say it's impossible, but you are making it far more difficult than it needs to be with the constraints you place on the implementation.

    If you want to do what you want you've got to learn how to do it. Step by patient step.

    B
     
  22. macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #22
    Ok so it says my options are 0, 3, and 25. Mine is currently set to 3.

    Read all the link and tried to mess around in Terminal, but still confused as hell...lol :mad: :( :mad:

    <----Grabs his hair and yanks hard, pulling clumps of hair out in the process! :confused: :( :confused:
     
  23. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,829
    Location:
    New England
    #23
    In what way does running
    Code:
    sudo pmset -b hibernatemode=25
    from Terminal.app do, or not do, what you expect?

    This is why I suggested:
    Again. What behavior did you expect. What did you get. What is the particular thing you are struggling with understanding, ... I can't read your mind.

    B
     
  24. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    Messages:
    471
    #24
    I think it would help everyone if you clearly stated your end goal. Do you want your computer to always hibernate? Only hibernate at certain times? Or something else?

    Also it would be useful to know WHY you want this behavior.

    When dealing with computers it is often easy to get bogged down with the "How do I do this?" part and forget about what or why you wanted it done in the first place. The answer is most often simpler than you think it is.
     
  25. tresean1, Mar 15, 2012
    Last edited: Mar 15, 2012

    macrumors member

    Joined:
    Mar 12, 2012
    Messages:
    41
    #25
    Yes I have stated that (the end goal and the "WHY") many, many times throughout. Not really sure how you missed that---but I will re-state it once again. Please give me a sec to compile details...brb.

    EDIT: here goes...
    That ^ answers the "WHY"....

    And these ^ answer the "end goal"... :D Hope this in-depth compilation helps explain things clearer. Once again, thanks in advance/retrospect to you and anyone who has contributed or will contribute.
     

Share This Page