Running Shell Script with Automator

Hi,

I’ve created a Linux shell script that builds and names folders based on a user’s input (code provided below), my question is, is there any way I can create a automator workflow using that code as the backend and a graphical user interface frontend ? I am totally new to Automator and Xcode so any advice would be greatly appreciated, thanks !

—Begin Code—
#!/bin/bash

newcase.sh

Create New CIF Case Folders

echo Video Case Folders Creation Utility v 1.0 (LINUX)
echo Enter 2 Digit Case Year::;read Y
echo Enter 3 Digit Case Number::;read N
mkdir -v “CIF${Y}-${N} Video Analysis Notes”
mkdir -v “CIF${Y}-${N} Render”
mkdir -v “CIF${Y}-${N} Video”
mkdir -v “CIF${Y}-${N} Text”
mkdir -v “CIF${Y}-${N} Images”
mkdir -v “CIF${Y}-${N} FCP”
mkdir -v “CIF${Y}-${N} Avid”
mkdir -v “CIF${Y}-${N} Audio”
echo Folders Creation Successful !
read -p “Press Any Key to Continue…”
exit
—end code—

Model: PowerBook G4
Browser: Safari 2.0.4
Operating System: Mac OS X (10.4)

Hi,

I’m not very familiar with shell scripts,
but I guess, this script does the same

property folderNames : {"Video Analysis Notes", "Render", "Video", "Text", "Images", "FCP", "Avid", "Audio"}

set theFolder to choose folder with prompt "Choose location to create folders"
-- tell application "Finder" to set theFolder to insertion location as alias
set theYear to get_data("Enter 2-digit year", 2)
set theNumber to get_data("Enter 3-digit number", 3)

repeat with i in folderNames
	make_new_folder(theFolder, ("CIF" & theYear & "-" & theNumber & space & i))
end repeat
display dialog "Folders Creation Successful !" buttons {"OK"} default button 1

on get_data(prompt, n)
	repeat
		try
			set t to text returned of (display dialog prompt default answer "" buttons {"OK"} default button 1)
			t as integer
			if (count t) is not n then error
			return t
		on error
			display dialog ("please enter only " & n as string) & " numbers" buttons {"OK"} default button 1 giving up after 2
		end try
	end repeat
end get_data

on make_new_folder(theFolder, fName)
	try
		return ((theFolder as Unicode text) & fName) as alias
	on error
		tell application "Finder" to return (make new folder at theFolder with properties {name:fName}) as alias
	end try
end make_new_folder

Note: The script asks at the beginning for the folder to create the subfolders in,
but you can also save the script as application and drag it into the toolbar of any Finder window
just to the left of the search bar. The folder wil be created in the current folder (window)
Therefore change the choose folder line into the commented out tell application “Finder” line

Wow, thanks for the hard work, really appreciate it, just a few minor tweaks I like to make:

  1. I am setting this script up as a workflow so I would like to eliminate the choose folder option and just have it create folders whereever I right click and run the work flow

  2. I need to set the permissions on those folders to read/write/execute/ etc., (equivelant of command line CHMOD 777)

I will do some research myself but any guidance from you would be greatly appreciated.

Thanks again for such great help you have provided !

Define a new workflow in Automator

  1. Get Selected Finder Items
  2. Run AppleScript

Copy the script into the text field replacing the existing lines.
Then you must select a folder and the subfolders will be created in there.
The chmod command will be executed too unless you haven’t the access privileges to do it

property folderNames : {"Video Analysis Notes", "Render", "Video", "Text", "Images", "FCP", "Avid", "Audio"}

on run {input, parameters}
	try
		set theFolder to (item 1 of input) as alias
		if folder of (info for theFolder) then
			set theYear to get_data("Enter 2-digit year", 2)
			set theNumber to get_data("Enter 3-digit number", 3)
			
			repeat with i in folderNames
				set f to make_new_folder(theFolder, ("CIF" & theYear & "-" & theNumber & space & i))
				try
					do shell script "chmod 777 " & quoted form of POSIX path of f
				on error
					display dialog "Can't change access privileges" buttons {"Go on"} default button 1
				end try
			end repeat
			display dialog "Folders Creation Successful !" buttons {"OK"} default button 1
		else
			display dialog "No folder selected" buttons {"Cancel"} default button 1
		end if
	end try
	return input
end run

on get_data(prompt, n)
	repeat
		try
			set t to text returned of (display dialog prompt default answer "" buttons {"OK"} default button 1)
			t as integer
			if (count t) is not n then error
			return t
		on error
			display dialog ("please enter only " & n as string) & " numbers" buttons {"OK"} default button 1 giving up after 2
		end try
	end repeat
end get_data

on make_new_folder(theFolder, fName)
	try
		return ((theFolder as Unicode text) & fName) as alias
	on error
		tell application "Finder" to return (make new folder at theFolder with properties {name:fName}) as alias
	end try
end make_new_folder

Hi Stefan,

Thanks for all your help, after playing around with the codes a bit, I came up with the following script which creates a case folder and subfolders for the different areas, now I like to add some additional things to my script:

  1. a Cancel button that gives the user the option to cancel the script
  2. a warning that the folders already exist if one with the same name is already there

if you could provide some guidance I would love to do it myself so you won’t be doing all the work for me, thanks again for all your help


property folderNames : {"Video Analysis Notes", "Render", "Video", "Text", "Images", "FCP", "Avid", "Audio"}

tell application "Finder" to set theFolder to insertion location as alias

set theYear to get_data("Enter 2-digit year", 2)
set theNumber to get_data("Enter 3-digit number", 3)
set b to make_new_folder(theFolder, ("CIF" & theYear & "-" & theNumber))
try
	do shell script "chmod 777 " & quoted form of POSIX path of b
on error
	display dialog "Can't change access privileges" buttons {"Go on"} default button 1
end try

tell the application "Finder" to set theFolder to b

repeat with i in folderNames
	
	set f to make_new_folder(theFolder, ("CIF" & theYear & "-" & theNumber & space & i))
	try
		do shell script "chmod 777 " & quoted form of POSIX path of f
	on error
		display dialog "Can't change access privileges" buttons {"Go on"} default button 1
	end try
end repeat

display dialog "Folders Creation Successful !" buttons {"OK"} default button 1

on get_data(prompt, n)
	repeat
		try
			set t to text returned of (display dialog prompt default answer "" buttons {"OK"} default button 1)
			t as integer
			if (count t) is not n then error
			return t
		on error
			display dialog ("please enter only " & n as string) & " numbers" buttons {"OK"} default button 1 giving up after 2
		end try
	end repeat
end get_data

on make_new_folder(theFolder, fName)
	try
		return ((theFolder as Unicode text) & fName) as alias
	on error
		tell application "Finder" to return (make new folder at theFolder with properties {name:fName}) as alias
	end try
end make_new_folder

Stefan,

I figured out the cancel buttons, the only thing I need now is a warning that folders with the same name exists, thanks.

This should do it:

property folderNames : {"Video Analysis Notes", "Render", "Video", "Text", "Images", "FCP", "Avid", "Audio"}

tell application "Finder" to set theFolder to insertion location as alias

set theYear to get_data("Enter 2-digit year", 2)
if result is false then return
set theNumber to get_data("Enter 3-digit number", 3)
if result is false then return
try
	tell application "Finder" to set mainFolder to make new folder at theFolder with properties {name:("CIF" & theYear & "-" & theNumber)}
	chmod(mainFolder)
on error
	display dialog "Folder already exists" buttons {"Cancel"} default button 1 with icon stop
end try
repeat with i in folderNames
	tell application "Finder" to set subFolder to make new folder at mainFolder with properties {name:("CIF" & theYear & "-" & theNumber & space & i)}
	chmod(subFolder)
end repeat

display dialog "Folders Creation Successful !" buttons {"OK"} default button 1

on get_data(prompt, n)
	repeat
		try
			set t to text returned of (display dialog prompt default answer "" buttons {"Cancel", "OK"} default button 2)
			t as integer
			if (count t) is not n then error
			return t
		on error e number num
			if num is -128 then return false
			display dialog ("please enter only " & n as string) & " numbers" buttons {"OK"} default button 1 giving up after 2
		end try
	end repeat
end get_data

on chmod(f)
	try
		do shell script "chmod 777 " & quoted form of POSIX path of (f as alias)
	on error
		display dialog "Can't change access privileges" buttons {"Go on"} default button 1
	end try
end chmod

Hi Stefan, coming across your generous and patient expertise in this post, I’m tempted to ask you:

  • what would be the Applescript to create 3 sub-folders (“Raw”, “Processed”, “Trash”)
    – with current date (format: “20071219_Raw”)
    — in a folder (and be prompted to select the latter each time)
    ---- and then to create 2 more sub-folders (“high res” and “low res”) within the “processed” sub-folder?
    this would help my workflow big time… cheers,