The way to create nested folder structures is probably AppleScript, not Automator. I don't think Automator can create nested folder structures as easily as AppleScript.
The following AppleScript creates a folder structure of the form
- My first folder
- A folder within my first folder
- Another folder within both of those
within the frontmost folder in Finder. Here it is:
try
tell application "Finder" to set FOLDER0 to (folder of the front window as alias)
end try
tell application "Finder"
set folder1 to make new folder at FOLDER0 with properties {name:"My first folder"}
set folder2 to make new folder at folder1 with properties {name:"A folder within my first folder"}
set folder3 to make new folder at folder2 with properties {name:"Another folder within both of those"}
end tell
I've deliberately tried to keep it basic. All the structure is declared in the second tell
block, and hopefully it should be easy to see how to extend it. If you want to test if before attaching it to a service, copy and paste it into AppleScript Editor (in the Utilities folder).
This script is fairly naive, and throws an error if the folders already exist. If that's a case you might be working with, then this question on Stack Overflow explains how to modify a script like this and only create folders that don't already exist.
If you want to create lots of folders with similar names (folder001, folder002, ..., folder999), then this forum thread on Apple Insider may be helpful.
You then want to bind this to a service in Automator (which I see from the question you know how to do). The service should receive folders in Finder, and you use the Run AppleScript action to run the script.
Once you've got that service installed, you can right-click on a folder in the Finder, and there'll be a service which creates this structure for you.
To get the different templates, I can think of two methods.
You could create multiple services ("Create nested folders 1", "Create nested folders 2", …). But that seems rather messy. Again, AppleScript is our friend. We can make a dialog that looks like this appear:
and which option we choose dictates which folder structure gets created. Here's the relevant code:
set choice to choose from list {"template1", "template2", "template3"}
try
tell application "Finder" to set FOLDER0 to (folder of the front window as alias)
end try
if choice is "template1" then
-- create some folders
end if
if choice is "template2" then
-- create some different folders
end if
if choice is "template3" then
-- create a third set of folders
end if
Replace the lines starting with --
with the folder generating code that we discussed above. The list in the first line gives you a list of choices, and then you create an if
block for each choice that generates the appropriate folder structure.
If you use this AppleScript in Automator, then the procedure is exactly the same, except now you get the choice when you run the service in Finder.
If you simply want to pass a file name to the Spotlight action
The pass the file name from an action to a set variable Action
- set the Spotlight Action to ignore the any above action.
Done by right clicking on it's title bar and using the menu.
- drag and drop the variable token into the search text field.
THIS IS AN UPDATE
Using Automator actions and Applescript to store properties for later retrieval:
The idea here is the first action writes out a applescript file to the documents folder.
The script will be used to store information we get along the way and then retrieve it later on in the final Applescript action.
We do this because we need to pass the final script multiple bits of information. Which we cannot do with the normal variables in Automator.
The Actions.
- Run Applescript: Write out a a storage script to the documents folder
The script.
set script_text to MakeScript()
my RunMaker(script_text)
on RunMaker(script_text)
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt"
set file_path to POSIX path of file_path as string
store script script_text in file_path with replacing
end RunMaker
on MakeScript()
script
property theBrokenAliasFolderPath : ""
property broken_alias_file_Paths : {}
property theOriginalFolderPath : ""
property Original_file_Paths : {}
property SearchfileNames : {}
end script
end MakeScript
2, Ask for Finder Items: This is for the Broken Alias folder.
- Set to Ignore Input
- set your start at: at your broken alias folder.
- set your Type to: Folder
3,Run Applescript:
- Writes out broken alias folder path to a property in the storage script file.
- pass the path on to the next action
( The write outs are done by the Action Applescript loading the storage file script. This will be a version of sorts. It will then change properties in its version and re-write the file out again replacing the old one)
--WRITE OUT BROKEN ALIAS FOLDER PATH
on run {input, parameters}
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt" as alias
set script_text to load script file_path
set theBrokenAliasFolderPath of script_text to (POSIX path of (item 1 of input))
my RunMaker(script_text)
return input
end run
on RunMaker(script_text)
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt"
set file_path to POSIX path of file_path as string
store script script_text in file_path with replacing
end RunMaker
4,Get Folder contents:
- The action receives the "broken alias folder path" and gets all the contents of the folder.
5, Filter Finder Items:
- Filter the items so they only contain alias files.
All: Kind : is : other : alias
- Pass the list to the next Action
6, Run Applescript:
Writes out broken alias PATHS to a property in the storage script file.
on run {input, parameters}
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt" as alias
set script_text to load script file_path
set broken_alias_file_Paths of script_text to input
my RunMaker(script_text)
return input
end run
on RunMaker(script_text)
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt"
set file_path to POSIX path of file_path as string
store script script_text in file_path with replacing
end RunMaker
7, Ask for Finder Items: This is for the Original files folder.
- Set to Ignore Input
- set your start at: at your Original files folder.
- set your Type to: Folder
8,Run Applescript:
Writes out the Original files folder to a property in the storage script file.
on run {input, parameters}
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt" as alias
set script_text to load script file_path
set theOriginalFolderPath of script_text to (POSIX path of (item 1 of input))
my RunMaker(script_text)
return input
end run
on RunMaker(script_text)
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt"
set file_path to POSIX path of file_path as string
store script script_text in file_path with replacing
end RunMaker
9, Run Applescript:
This retrieves the information from the storage script.
This will take the list of paths of the dead alias files.
Try and Find a matching file in the Original files folder. It will look at all matches but ignore aliases.
Remove the old Alias file (checking it is a alias file first)
Create a symlink in the folder the old alias was in from the matching file found.
Only alias files should be deleted. If no matching file is found then the alias file will not be deleted. Nor will a symlink be created.
.
set file_path to (path to documents folder as Unicode text) & "MyProperties.scpt" as alias
set theScript to load script file_path
--choose a search folder
set searchPath to quoted form of theOriginalFolderPath of theScript
set folderPath to quoted form of theBrokenAliasFolderPath of theScript
set input to broken_alias_file_Paths of theScript
repeat with i from 1 to count of items of input
set this_item to item i of input
set aliasPath to this_item
#Get the the file name
set theFileName to "\\\"" & (do shell script "basename " & quoted form of (POSIX path of (this_item))) & "\\\"" as string
log theFileName
#search the searchPath for a matching file with the same name.
#NOTE: this will find all files with he same name. So We use last paragraph to get what should be the first one it finds.
set theOrigFilePath to paragraphs of (do shell script "mdfind -onlyin " & searchPath & " kMDItemFSName == \"" & theFileName & "\"")
if theOrigFilePath is not quoted form of "" then
repeat with i from 1 to count of items in theOrigFilePath
set this_item to item i of theOrigFilePath
log this_item
tell application "Finder"
#make sure we are pointing to an alais that will be deleted and not another symlink file. Otherwise the original file will be deleted.
set theKind to kind of ((POSIX file this_item) as alias)
if theKind is not equal to "Alias" then
set this_item to quoted form of (item i of theOrigFilePath)
my symLink(aliasPath, this_item)
end if
end tell
end repeat
end if
end repeat
on symLink(aliasPath, aOrigFilePath)
#move to trash the old alias file
set theOldAlias to aliasPath
tell application "Finder"
#make sure we are pointing to an alais that will be deleted and not another symlink file. Otherwise the original file will be deleted.
set theKind to kind of theOldAlias
set theNewFilePath to (POSIX path of (aliasPath)) as string
if theKind is equal to "Alias" then
delete theOldAlias
log "ln -s " & aOrigFilePath & space & (quoted form of theNewFilePath)
#create the symlink
do shell script "ln -s " & aOrigFilePath & space & (quoted form of theNewFilePath)
end if
end tell
end symLink
Test first.. use at your own risk and all that..
Best Answer
Why not just use the Create Archive action rather than shell scripting?
I created a quick one that has four steps.