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!
$(cd $FolderPath)
instead ofcd $FolderPath
?cd $FolderPath
first and it didn't work so I tried forcing the command with$()
and just didn't pull it out after it still didn't work.$()
, run it directly inbash
to see what happens.$()
as it runs the command within the parens in a subshell and returns the result (though looking back at my command now it wouldn't do anything with the cd command because the navigation's scope is the subshell). Either way, I did run it in bash and it works fine (I built it originally for bash). As a test just now, I wrote the following command which executes successfully when run from a shell:say $(pwd) FolderPath="/Volumes/Secure/TEMP/" cd $FolderPath say $(pwd)
. The same structure used in automator does not successfully change directory.cd
doesn't actually change directories.