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

Reply
2 replies

Sep 26, 2016 6:58 PM in response to RestonMacGuy

Doesn't it always seem to happen? You work the better part of the day writing and testing code to no end, and then out of frustration, post your problem for all the world to see. Then, a half hour after doing so, the answer comes to you, and it's so obvious that it's almost embarrassing!


I found I could accomplish my task by using a two-step Automater folder action:


User uploaded file


Duh. I'm so embarrassed...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Bedeviled by AppleScript Folder Actions

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.