RestonMacGuy

Q: Bedeviled by AppleScript Folder Actions

I admit I'm stumped.

 

My goal:  When a file is added to a folder, simply rename it by removing a prefix.

 

I can do this via shell script.  I can do it via straight AppleScript, but when I try to make this a Folder Action, nothing happens.

 

Here is the requirement:  A file named '[PROJECT]xxxxyyyyzzz.doc' is dropped into a folder.  The file should be renamed as just 'xxxxyyyyzzz.doc.'

 

Here is my shell script:

INP="$1"

#echo "$1"

#

# Check to make sure the new file is indeed, an PROJECT torrent

#

if [ "${INP:0:8}" = "[PROJECT]" ]; then

    NEWF="${INP:8}"

    # Here we rename the file

    mv $INP $NEWF

    echo Changed $INP to $NEWF

fi

 

This works from a terminal command line when the name of the file is used as an argument.

 

Here is my plain AppleScript:

 

try

        tell application "Finder"

            -- activate -- is this necessary?   

            set the_file_name to the name of the_file as text -- needed for string manipulation

            set the_prefix to characters 1 through 9 of the_file_name as text -- The length of the string "[PROJECT]"

            set the_suffix to characters 10 through (the length of the_file_name) of the_file_name as text -- the rest of the file name  

            if the_prefix is equal to "[PROJECT]" then

                -- the_suffix contains the stripped value of the file name

                set the name of (file the_file) to the_suffix

            end if

        end tell

    end try

 

But when I try to make the AppleScript a droplet or a Folder Action, or when I use Automator to make a Folder Action, I get no result -- no renaming is done.

 

I know I'm probably missing something obvious, but the goal is to make renaming these files a Folder Action.

 

I'm on MacOS X 10.11.6

Script Editor 2.8.1

AppleScript 2.5

MacBook Air, OS X El Capitan (10.11.6), 2.0 GHz, 8GB, 512 Flash HD

Posted on Sep 26, 2016 11:59 AM

Close

Q: Bedeviled by AppleScript Folder Actions

  • All replies
  • Helpful answers