1

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. enter image description here

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!

  • Why do you use $(cd $FolderPath) instead of cd $FolderPath? – nohillside Feb 23 '14 at 16:02
  • I tried 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. – Chris Schmitz Feb 23 '14 at 16:11
  • 1
    You can't force a command with $(), run it directly in bash to see what happens. – nohillside Feb 23 '14 at 16:14
  • With some commands you can force it using $() 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. – Chris Schmitz Feb 23 '14 at 16:27
  • Sorry, I was running out of room in the last comment. If you create an automator service with just the steps: ` #!/bin/bash say $(pwd) FolderPath=$1 cd $FolderPath say $(pwd)' The finder will speak the same directory each time. The cd doesn't actually change directories. – Chris Schmitz Feb 23 '14 at 16:29
3

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 added cd "$1" to the top of the script and it worked.

  • Awesome! Thanks for pointing that out. I switched it to as arguments and everything worked as it should. Thanks for the help! – Chris Schmitz Feb 23 '14 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .