How would I make a script that will run a terminal command?

Discussion in 'Mac Programming' started by ideal.dreams, Jul 19, 2010.

  1. ideal.dreams macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #1
    Hi all,

    When people use my computer, specifically my parents when they're too lazy to get theirs, they tend to have a pair of wandering eyes. To keep some of my personal data private, I put a '.' in front of the title so it will be hidden by OS X.

    Hiding and unhiding the files is quite the pain, though, as I have to open terminal each time, and type in the two commands required to show and hide the files, which are as follows...

    Code:
    defaults write com.apple.finder AppleShowAllFiles TRUE
    Code:
    killall Finder
    I am curious if it would be possible to implement these two commands into a script, so that when I double-click the file, it opens in Terminal, checks the status of whether the files are hidden or not (whether it's TRUE or FALSE), and then does the opposite of what the check returns. Like if it checks it, and it returns TRUE, it would make it false, and run it, followed by the killall Finder.

    I do have a lot of experience with HTML and PHP, but outside that I'm pretty useless.

    Can anyone help me?

    If anything I said above doesn't make sense, please tell me and I will clarify it.

    Thanks,
    ~ideal.dreams
     
  2. qtx43 macrumors 6502a

    Joined:
    Aug 4, 2007
    #2
    Simplest is to create two different text files with a text editor:
    Code:
    #!/bin/bash 
    defaults write com.apple.finder AppleShowAllFiles TRUE
    killall Finder
    and second one with FALSE instead of TRUE. I'm assuming your commands are correct! Then on the command line:
    Code:
    chmod +x nameofyourfile
    
    for each file. This makes them executable scripts. Read "man bash" for more info, shell scripting can get pretty involved, but just simple stuff is simple. You could probably do something with Automator too. Or, you know, just create a second account for others to use, and have yours require a log-in on awake from sleep.
     
  3. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    If you want to hide stuff, just create a password-protected disk image. Also, make them a separate account and enable fast-user-switching.
     
  4. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #4
    I agree on both points. Why bother with shell scripts when you can just block them out of your account entirely?
     
  5. AutolycusX macrumors newbie

    Joined:
    Jul 18, 2010
    #5
    This is the way to go
     
  6. ideal.dreams thread starter macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #6
    @qtx43: Thank you, I will try that. Is there a way to make the script check what the defaults write is set to already though? This way if it saw it was set to TRUE it would make it FALSE, so I don't have to use two files.

    @GorillaPaws: It is a folder, and files are being put in and taken out. I do not want to have to make a new disk image every time I want view or add contents to the folder. I also made them a guest account, but they thought I was trying to hide stuff and made me log in to mine.

    Thanks for the replies.
     
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    You can make a read-write disk image, you don't need to re-create it to add new items (just make it big enough to hold whatever you need, and overestimate this). If you run backups, make it a sparse disk image, this way only the changed parts will have to be backed up, not the whole thing.

    Heh, well technically you are trying to hide something, aren't you? But yeah, if you can't use separate accounts, the protected disk image is the way to go. And don't name it "Secret" or "P0rn", something more like "Test Data" would probably work better. ;)
     
  8. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #8
    Is the OP a legal adult? If so, his parents have no right to make him log into his own account. It's his computer not theirs.
     
  9. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #9
    The problem is, those passwords are stored in your keychain by default. Perhaps you can tell disk utility to NOT do that?
     
  10. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #10
    When you log into the disk image, there's a checkbox to remember your password. As long as you don't check that, you're OK (but you do have to enter the password each time you mount the image, of course).
     
  11. ideal.dreams thread starter macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #11
    @HiRez: How would I do that? That seems like a better solution. The reason I never wanted to do a disk image before was because I thought it was not possible to write to it after it's been created.

    @mac2x: No, I'm 16, hahaha. Do you know if it's possible to not have the password stored?
     
  12. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #12
    It's easy, just open /Applications/Utilities/Disk Utility.app and click on New Image. Name it, choose a location, choose an encryption (128-bit should be fine for you, this triggers password protection), choose an appropriate size (I recommend larger than you think you will need), and then read/write disk image type or sparse bundle type, which I would recommend if your image will be very large or have lots of files in it. It will ask you for the password and will ask if you want to remember the password -- here is where you DESELECT that checkbox so it doesn't go into your keychain. That's it. Just double click to mount it, enter password and again make sure the remember box is unchecked, then read and write to it as you would any disk or folder. And of course when you are done with it, you have to remember to eject it or your private info will be sitting there easily accessible.
     
  13. ideal.dreams thread starter macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #13
    So once the image is created, I'd just drag the folder in to it?
     
  14. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #14
    Yes. The image will be created blank. Once mounted (double click on it to mount it), you can drag anything you want into it, or create new folders and files in it, delete or copy things, just like any other disk. The only limitation being you're limited to the size of the image you created, even though it lives on your larger hard drive. That's why I say overestimate the size when you create it. Think of it as a password protected USB flash drive, which actually, come to think of it, is another option for you.
     
  15. ideal.dreams thread starter macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #15
    What option would I select for the partitions choice?
     
  16. Wondercow macrumors 6502a

    Joined:
    Aug 27, 2008
    Location:
    Toronto, Canada
    #16
    There's no need to make it particularly large if it's a Sparse image. A Sparse image will grow as needed to accommodate the contents. Don't waste hard drive space by making a 10 GB image to hold 1 GB of data; make a 1 GB Sparse image and it will grow automagically when you add more data to it.

    Edit: For thoroughness I'll add my method for the original question. Open AppleScript Editor and type:

    Code:
    Do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE"
    Do shell script "killall Finder"
    Save that as an application.
     
  17. ideal.dreams thread starter macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #17
    How do I make a sparse image? That seems a lot more logical, hahaha.
     
  18. Corndog5595 macrumors 65816

    Joined:
    Jul 16, 2010
    #18
    Don’t download porn...?

    but to answer your question:
    - Disk Utility
    - - New Image
    - - - Image Format: sparse disk image

    Edit: I recommend using ‘sparse bundle disk image’ because it not only grows, but also shrinks.

    [​IMG]
     
  19. misee macrumors member

    Joined:
    Jul 4, 2010
    #19
    I actually find HiRez option to use a usb stick quite good. Since they don't want you to have your own account because they think (or know?) you hide stuff, they will definitely want to see the contents of a password protected disk image (at least I would). So either USB stick, or the disk image which I would also hide by prefixing it with the . or putting it in a place like /etc, or some other appropriate directory that is hidden. When you put it in a hidden directory, you can navigate to it with the finders "Go" -> "Go To Folder..." (or command - shift - G) and type the path in the dialog. That path will be stored, so don't forget to change it to some other path before you leave your computer.

    Also, remove the recent folders in the finders menu "Go" -> "Recent Folders" and the :apple: -> "Recent Items" menu. All hiding won't help if your parents find an entry like "hot blonde doing-you-know-what.avi" in your recent items menu :cool:.
     
  20. Corndog5595 macrumors 65816

    Joined:
    Jul 16, 2010
    #20

    who let you on my computer? :rolleyes:
     
  21. Wondercow macrumors 6502a

    Joined:
    Aug 27, 2008
    Location:
    Toronto, Canada
    #21
    Just follow HiRez' instructions. Under "Image Format" select either of the Sparse types.

    I need to correct myself on one thing: I misspoke in my last post; the size that you set will be the largest possible size of the Sparse image. So, even if you have 1 GB of data set the size to 10, 20, even 100 GB to give it room to grow, but rest assured that it'll still only use as much room as the data within it needs. It's also worth noting that it won't shrink when files are removed—use Terminal to compact it if you delete some data:
    Code:
    hdiutil compact MyFile.sparseimage
    It can also be resized if you hit the limit:
    Code:
    hdiutil resize -size 50g MyFile.sparseimage
    Would set the max size to 50 GB.
     
  22. Wondercow macrumors 6502a

    Joined:
    Aug 27, 2008
    Location:
    Toronto, Canada
    #22
    A Sparse bundle won't do that. It would still need to be compacted using Terminal.
     
  23. Corndog5595 macrumors 65816

    Joined:
    Jul 16, 2010
    #23
    oh, really?
    what is the difference between a sparse and a sparse bundle then?
    sarcasm not intended.
     
  24. Wondercow macrumors 6502a

    Joined:
    Aug 27, 2008
    Location:
    Toronto, Canada
    #24
    The Sparse bundle is actually a package, like an application package, that contains many small bits and pieces that make up the whole. You can see this yourself—if you control-click on a Sparse bundle image file (not the disk) you'll see that you can choose "show package contents". In the window that opens there will be a folder named "Bands"; it's these bands that actually hold the data contained on the disk.

    The point of this is that for backup software—Time Machine, really—would backup an image file then backup the entire file again if anything on it changed. Not so good when your image is, say, 50 GB in size. So, enter the Sparse bundle and its bands—each band is 8 MB in size (max). So Time Machine doesn't need to backup the entire file again, only the bands that have changed, or been added.
     
  25. ideal.dreams thread starter macrumors 68020

    ideal.dreams

    Joined:
    Jul 19, 2010
    Location:
    OH
    #25
    My parents can't find their way around my mac. They're windows users, and they have trouble with even that. They can just see what's in front of them. They would have no intentions of looking at a file called "system tests.dmg", as it doesn't look suspicious.

    @Wondercow: Thanks for the help, I will make it a sparse image so I'm not allocating space that I don't need. Also, with the AppleScript thing, would there be a way to make it check the status of the defaults write to see if it's TRUE or FALSE? This way I could have it do the opposite of what it returns, so I won't have to have two files. EDIT: Would I still set the size for like 8GB? I set it to 100MB and it wouldn't let me copy files to it because there was not enough free space.

    @Missee: That's what the 'clear recents' is for, hahaha. My parents wouldn't know how to get there anyways. A USB stick wouldn't help too much either, as I might lose it :p.

    Thank you all for the helpful replies!
     

Share This Page