I'm trying to create a context menu command via Automator service. The context menu command will run a shell script to create some default documents in the given directory.
I wrote out the shell script which executes correctly when run from the target directory:
#!/bin/bash
touch History.markdown
touch Notes.markdown
touch Plan.markdown
touch ProjectName.tmproj
cat <<TEXT > ProjectName.tmproj
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>currentDocument</key>
<string>Notes.markdown</string>
<key>documents</key>
<array>
<dict>
<key>filename</key>
<string>Notes.markdown</string>
<key>selected</key>
<true/>
</dict>
<dict>
<key>filename</key>
<string>History.markdown</string>
</dict>
<dict>
<key>filename</key>
<string>Plan.markdown</string>
</dict>
</array>
<key>openDocuments</key>
<array>
<string>History.markdown</string>
<string>Notes.markdown</string>
<string>Plan.markdown</string>
</array>
<key>fileHierarchyDrawerWidth</key>
<integer>200</integer>
<key>metaData</key>
<dict/>
<key>showFileHierarchyDrawer</key>
<true/>
<key>windowFrame</key>
<string>{{113, 95}, {1230, 900}}</string>
</dict>
</plist>
TEXT
I then opened Automator and created a new service. I set the service to receive selected input of folders
in Finder
.
I then added the following to the top of my shell script to received the selected directory as standard input:
FolderPath=$1
$(cd $FolderPath)
The variable FolderPath
definitely gets the current directory. I added a say $FolderPath
to the shell script and it says out the entire path when run from the context menu command.
The problem is that when I try to change to the directory in the variable, nothing happens. I put another say
command that spoke the command pwd
after the directory change and it says that I'm at my user's root.
Is there something that automator does differently when dealing with shell scripts?
Here's a screen shot of the entire service.
I should also mention that I have tried to just concatenate the folder path and the file name that I've tried to create with the touch
command and when I do so it only uses the string name of the file to create:
say "$FilePath/History.markdown" # only says "History dot markdown"
Any help would be greatly appreciated!
Best Answer
The problem here is you have the input being passed to
stdin
. I don't know why that's the default, but you should be able to just change the popup menu in the top right to "as arguments." I did that and addedcd "$1"
to the top of the script and it worked.