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
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>
Name the file .command or .sh.
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.
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 user#mysite.sitename.com'
Now all you need to do is type the alias from terminal and the command will execute.
Hope you find the above useful
Related
I am completely new to bash scripting, and would like to know how I can write a script to create a shortcut to go on a mac osx doc.
This is to enable the user to open a specific file by clicking a shortcut on the mac osx doc. In my case, the file in question is .command file that launches an application with the required arguments.
Hope somebody can help.
Thats easy, just dump the .command file into the terminal window and it will give you the path to the file, then
ln -s /Users/username/path/.command ~/Desktop/command
If you double click this the file will open. If you have not set .command to be opened by a given program just use " " and set the program you want it to be opened in.
On macOS, bash scripts do not work right out of the box. You can use shell script instead.
defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>**/Applications/Google Chrome.app**</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'
killall Dock
Make sure that you replace the string /Applications/Google Chrome.app with whatever application you want to put into Dock.
You can just open the terminal app and you can copy paste (with editing the name of the application you want to put into dock) and then killall Dock this should work for you.
I have a Terminal cmd running that executes a function from the .bash_profile file (as described here).
From the description, I added the function to the ~/.bash_profile (can be found here /home/user/.bash_profile)
From my Terminal everything works !
But how do I create a shortcut in Mac OS that executes this Terminal cmd ??
I tried Apple Automator (--> but error: Cmd not found)
I tried Alfred (--> but same error: Cmd not found)
How can I make this Terminal cmd execute from a keyboard-shortcut ??
Do I need to place the script-functions in another file (other than bash_profile ?) - or what is here to do ?
Here is an image of the Automator trial:
Place the functions in separate files, such as suspend-script.sh, resume-script.sh, and kill-script.sh; put them in a common directory like /home/user/scripts. To keep using them in your .bash_history, do:
# in .bash_history
source "suspend-script.sh"
source "resume-script.sh"
source "kill-script.sh"
Make sure you set the executable bit on each script (chmod +x /home/user/scripts/*.sh). Then, use the following for shortcut keybindings:
/home/user/scripts/suspend-script.sh thing_to_suspend
/home/user/scripts/resume-script.sh thing_to_resume
/home/user/scripts/kill-script.sh thing_to_kill
Since it's a shortcut to a function, you have to pick one script to suspend/resume/kill (the thing* above).
For example, if you wanted to suspend/resume/kill the top process, you would use the following:
/home/user/scripts/suspend-script.sh top
/home/user/scripts/resume-script.sh top
/home/user/scripts/kill-script.sh top
Open Automator, choose Application, add a Run Shell Script action and put in your Shell command between quotes (if you have a file, you can just drag and drop it).
Other than playing it, now you can save it (as an app anywhere) and even set the icon. Then you got an app and then you can define a keyboard shortcut for it in system-preferences.app
This should work, at least it does for me.
make sure your script is executable: sudo chmod u+x <your-scriptname>
and also add the filename extension .command to your filename. So the name of the script should be filename.command.
When you then click the script in Finder, it gets executed.
I hope that works!
I'm on a Mac, and have a bash script that works very nicely.
I'd like to make it so that a double-click will run it, but I don't know the "open with" operand. Please, what am I missing?
You'll need to make the file an executable.
On the first line, before any of your code put in a shebang
#!/usr/bin/env bash
REST OF YOUR CODE HERE
Next, you'll need to change the permissions. On the terminal run:
chmod +x your_bash_file
Finally, you will need to make sure OS X opens the file using the Terminal and not the application that created the file e.g. your favourite text editor. You can accomplish this in 1 of two ways:
Save the file with no file extension (eg. bash_file, instead of bash_file.sh)
Or, choose File -> Get Info and set Open with: to Terminal.app
You should now be able to click on the script to execute it!
By default it is not possible to see .gitignore files in osx. What is command to reveal these files?
Open the terminal and type
on OS X 10.8:
defaults write com.apple.Finder AppleShowAllFiles TRUE
on OS X 10.9:
defaults write com.apple.finder AppleShowAllFiles TRUE
Then you must relaunch finder:
killall Finder
Any file name in OS X prefixed with a '.' is considered "hidden".
You can use the shortcut in Finder:
Command + Shift + .
It will show the hidden files.
To hide the files again, use the same shortcut.
⌘⇧. will toggle the AppleShowAllFiles setting.
This key combo will work from open/save dialogue boxes in all apps, not just the finder. Use this and you’ll never be confused when on someone else’s Mac or a new Mac, and you can avoid mucking around with defaults write.
I use the nemonic of “use a dot to show a dot file” to remember it, because of hidden dot files in unix.
if you just want to look at them you can always use the command line:
ls -al path/to/dir
If you want to always view all files from the finder you can do:
defaults write com.apple.Finder AppleShowAllFiles YES
If you just want to view a .gitignore from the finder you can:
chflags nohidden /path/to/dir/.gitignore
But youll have to call that command on every .gitignore its not global.
(more recent, for 10.10.2:)
The above commands didn't work for me. I'm using OSX Yosemite: 10.10.2.
This worked though:
defaults write com.apple.finder AppleShowAllFiles -boolean true;
killall Finder;
Source:
http://www.idownloadblog.com/2014/08/04/how-to-show-hidden-files-folders-finder-mac/
You can edit hidden file in terminal using this command
open -a TextEdit .gitignore
If you just want to view a .gitignore from the console just type "nano .gitignore" in that directory. This command "nano" simply opens any textfile in nano console environment for viewing or editing
In addition to the accepted answer, you can create an alias to easily show/hide the hidden files in Terminal. This is how I set it up (tested/working on macOS Mojave 10.14.1).
In my user directory I created a new file .custom_aliases and wrote this in:
# Show/hide files
alias showall='defaults write com.apple.finder AppleShowAllFiles -boolean true; killall Finder'
alias hideall='defaults write com.apple.finder AppleShowAllFiles -boolean false; killall Finder'
Next I opened .bash-profile (should also be in your user directory, if not just create it there) and added this to the top of the file:
# Load custom aliases
source ~/.custom_aliases
And that's it! Now whenever I need to view the hidden files I just type showall in Terminal and hideall when I'm done. You could also define the aliases directly in the .bash_profile, but I have some other stuff so I like to keep all the aliases together in a separate file.
Show hide file and folder on MacOs Mojave 10.14.4
Apply at Terminal
defaults write com.apple.finder AppleShowAllFiles -boolean true;
killall Finder;
It's possible you might just not have a .gitignore file. If you don't have one, you can create it like this:
>touch ~/.gitignore
And then edit it however you'd like. Git will automatically check this file, without any additional configuration!
How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?
You could create a Automator workflow with a single step - "Run Shell Script"
Then File > Save As, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
The benefit to this is it's really simple to do, and you can very easily get user input (say, selecting a bunch of files), then pass it to the input of the shell script (either to stdin, or as arguments).
(Automator is in your /Applications folder!)
If you don't need a Terminal window, you can make any executable file an Application just by creating a shell script Example and moving it to the filename Example.app/Contents/MacOS/Example. You can place this new application in your dock like any other, and execute it with a click.
NOTE: the name of the app must exactly match the script name. So the top level directory has to be Example.app and the script in the Contents/MacOS subdirectory must be named Example, and the script must be executable.
If you do need to have the terminal window displayed, I don't have a simple solution. You could probably do something with Applescript, but that's not very clean.
On OSX Mavericks:
Create your shell script.
Make your shell script executable:
chmod +x your-shell-script.sh
Rename your script to have a .app suffix:
mv your-shell-script.sh your-shell-script.app
Drag the script to the OSX dock.
Rename your script back to a .sh suffix:
mv your-shell-script.app your-shell-script.sh
Right-click the file in Finder, and click the "Get Info" option.
At the bottom of the window, set the shell script to open with the terminal.
Now when you click on the script in the dock, A terminal window will pop up and execute your script.
Bonus: To get the terminal to close when your script has completed, add exit 0 to the end and change the terminal settings to "close the shell if exited cleanly" like it says to do in this SO answer.
I know this is old but in case it is helpful to others:
If you need to run a script and want the terminal to pop up so you can see the results you can do like Abyss Knight said and change the extension to .command. If you double click on it it will open a terminal window and run.
I however needed this to run from automator or appleScript. So to get this to open a new terminal the command I ran from "run shell script" was "open myShellScript.command" and it opened in a new terminal.
As long as your script is executable and doesn't have any extension you can drag it as-is to the right side (Document side) of the Dock and it will run in a terminal window when clicked instead of opening an editor.
If you want to have an extension (like foo.sh), you can go to the file info window in Finder and change the default application for that particular script from whatever it is (TextEdit, TextMate, whatever default is set on your computer for .sh files) to Terminal. It will then just execute instead of opening in a text editor. Again, you will have to drag it to the right side of the Dock.
In the Script Editor:
do shell script "/full/path/to/your/script -with 'all desired args'"
Save as an application bundle.
As long as all you want to do is get the effect of the script, this will work fine. You won't see STDOUT or STDERR.
I think this thread may be helpful: http://forums.macosxhints.com/archive/index.php/t-70973.html
To paraphrase, you can rename it with the .command extension or create an AppleScript to run the shell.
As joe mentioned, creating the shell script and then creating an applescript script to call the shell script, will accomplish this, and is quite handy.
Shell Script
Create your shell script in your favorite text editor, for example:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(this runs the w32 executable, using the mono framework)
Save shell script, for my example "StartKeepass.sh"
Apple Script
Open AppleScript Editor, and call the shell script
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script - applescript command to call external shell commands
"sh ...." - this is your shell script (full path) created in step one (you can also run direct commands, I could omit the shell script and just run my mono command here)
user name - declares to applescript you want to run the command as a specific user
"<enter username here> - replace with your username (keeping quotes) ex "josh"
password - declares to applescript your password
"<enter password here>" - replace with your password (keeping quotes) ex "mypass"
with administrative privileges - declares you want to run as an admin
Create Your .APP
save your applescript as filename.scpt, in my case RunKeepass.scpt
save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case
Copy your app file to your apps folder
Exact steps to achieve that in macOS Monterey 12.3
Open Automator
File -> New
Choose Application
Go to Library -> Utilities
Double-click Run Shell Script
Type in whatever command you want to run. For example, try the command to toggle Dark Mode:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
File -> Save
Drag the saved file to the Dock, done!
pip install mac-appify
I had trouble with the accepted solution but this command worked for me.
Install
pip install mac-appify
Run
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/appify ~/bin/webex_start.sh ~/Desktop/webex.app
Adding to Cahan's clear answer ... to open a shell script from the dock without passing any arguments to it, try:
open [name of your script].scpt"
example:
open "//Users/user/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/myScript.scpt"
Someone wrote...
I just set all files that end in ".sh" to open with Terminal. It works
fine and you don't have to change the name of each shell script you
want to run.