First off, this is my first go at shell scripting. I have only experience in HTML and CSS :]
What I want to do is setup a simple folder structure and prompt the user in a dialog box to set a name for the root folder. I'll trigger this shell script via an OS X Service or a Keyboard Maestro hotkey.
This is what I've come up with so far:
#!/bin/sh
echo -n "Enter the project name:"
read -e NAME
mkdir -p ~/Desktop/$NAME
mkdir -p ~/Desktop/$NAME/subfolder1
mkdir -p ~/Desktop/$NAME/subfolder2
Obviously there's some error - the variable won't get passed on and the root folder isn't created. I also read that I should use "dialog" to ask for the input, but I wasn't capable of writing something that works.
Your help is greatly appreciated. Thanks.
Create a New Directory ( mkdir ) The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd . Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
The echo
and read
commands both work via a text interface, such as the Terminal window the script is running in. If you run a script in an OS X Service, there's no Terminal-like interface, so neither command does anything useful. (I don't know about Keyboard Maestro, but I assume it's similar.) The simplest way to interact from a situation like this is generally to use AppleScript via the osascript
command:
name="$(osascript -e 'Tell application "System Events" to display dialog "Enter the project name:" default answer ""' -e 'text returned of result' 2>/dev/null)"
if [ $? -ne 0 ]; then
# The user pressed Cancel
exit 1 # exit with an error status
elif [ -z "$name" ]; then
# The user left the project name blank
osascript -e 'Tell application "System Events" to display alert "You must enter a project name; cancelling..." as warning'
exit 1 # exit with an error status
fi
mkdir -p ~/Desktop/$name
mkdir -p ~/Desktop/$name/subfolder1
mkdir -p ~/Desktop/$name/subfolder2
(Note: I prefer to use lowercase variable names in the shell to avoid possible conflicts with special variables like PATH etc...)
Note: @Gordon Davisson's answer deserves the credit, but here are some improvements that make the code more robust (I tried to edit the original post, but it was rejected).
Improvements:
Code:
while :; do # Loop until valid input is entered or Cancel is pressed.
name=$(osascript -e 'Tell application "System Events" to display dialog "Enter the project name:" default answer ""' -e 'text returned of result' 2>/dev/null)
if (( $? )); then exit 1; fi # Abort, if user pressed Cancel.
name=$(echo -n "$name" | sed 's/^ *//' | sed 's/ *$//') # Trim leading and trailing whitespace.
if [[ -z "$name" ]]; then
# The user left the project name blank.
osascript -e 'Tell application "System Events" to display alert "You must enter a non-blank project name; please try again." as warning' >/dev/null
# Continue loop to prompt again.
else
# Valid input: exit loop and continue.
break
fi
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With