Ian Vink
Ian Vink

Reputation: 68740

Mac 'batchfile' script solution?

In Windows I would create a .bat file to run this script from my desktop, on my Mac how do I create something similar that can be run from the desktop to execute this:

 defaults write com.apple.finder AppleShowAllFiles TRUE
 killall Finder

Upvotes: 3

Views: 4505

Answers (4)

HappyCoder
HappyCoder

Reputation: 6155

I used to use batch files but the pain was having to find the folder where the scripts where kept. This became a pain so now I use alias's which work from any location in terminal and do not require finding your script.

Of course you can use these together nicely.

To get started you need to access your bash_profile.

A bash profile is an invisible file that lives on your machine, I believe it is homeless without a particular location.

So how do you find it?

Well it either exists or it does not, so to access the file simply write:

sudo nano ~/.bash_profile 

This command will either create it or open it up

Once inside, add the following lines:

alias showFiles='defaults write com.apple.finder AppleShowAllFiles YES; killall Finder /System/Library/CoreServices/Finder.app'
alias hideFiles='defaults write com.apple.finder AppleShowAllFiles NO; killall Finder /System/Library/CoreServices/Finder.app'

Thats it...

ctrl + O to save
ctrl + X to exit

And finally refresh the bash profile:

source ~/.bash_profile

How do you use the commands? Well now to show files, simply write: showFiles and to hide files just write: hideFiles... voila!

I use this to make life easier while using terminal. For instance to ssh to websites, or open help files, or to access mysql etc. etc.

Here are some further uses you may find useful:

alias goWebsite='open http://www,google.com'
alias goDoc='open -a TextEdit /users/myusername/documents/mydocument.txt'
alias goLocation='cd /applications/mamp/htdocs/workspaces/general/website.com/trunk'
alias sshToServer='ssh [email protected]'

Now all you need to do is type the alias from terminal and the command will execute.

Hope you find the above useful

Upvotes: 0

Gordon Davisson
Gordon Davisson

Reputation: 125708

Shoan's instructions for making a shell script will work fine, but you need to run it from within Terminal. If you add jtbandes suggestion of giving the filename a .command suffix (.sh doesn't work for me) the file becomes double-clickable in the Finder -- but it still opens a Terminal window, and leaves it open when it finishes. If you don't want to be bothered with this, there are a couple of ways of doing the job without any extraneous UI stuff:

1- Create an AppleScript in the AppleScript Editor (which is either /Applications/Utilities/AppleScript Editor.app or /Applications/AppleScript/Script Editor.app, depending on which version of OS X you have). Enter this as your script:

do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder"

... and then save the script in Application format so it's double-clickable (if you save it as a "Script", double-clicking it will open the script editor instead).

2- Create an Automator workflow using /Applications/Automator. Use the Application template (again, to make it double-clickable), find the "Run Shell Script" action in the second column (it's a huge list, so I just type "shell" in the search field at the top) and drag it into the workflow space at the right. Make sure it's set to use a reasonable shell, paste in your commands, and save.

Upvotes: 2

Shoan
Shoan

Reputation: 4078

Create a file with the following content

#!/bin/bash
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

From the terminal.app, run chmod o+x <filename> to make the file executable.

To run the file simply open the terminal.app and ./<filename>

Upvotes: 4

jtbandes
jtbandes

Reputation: 118651

Name the file .command or .sh.

Upvotes: 4

Related Questions