Hi all,
An ApplesScript neophyte, I work in a digital photostudio where the photogs are not always conscientious about their copying and pasting when they’re naming image files. As a result, I get files uploaded frequently to servers with leading and/or trailing spaces in their filenames. As yo can probably imagine, this causes all sorts of problems with our workflow.
What I would like to do is to present the photogs with a droplet that they can use before they upload the files that would take a bunch of files or folders dropped on it and strip out any trailing or leading spaces in the filenames. At first, I figured that would be easy to find something already written that would do the trick, but all my searching has turned up nothing that really fits my needs.
So I made an attempt to assemble an AppleScript of my own. I say “assemble” rather than “write” because like any sensible person would, I’ve stolen most of it from other scripts, mostly Apple’s examples.
However, I still don’t have a working solution. Here’s the script as it exists so far, please don’t laugh too much:
-- This droplet processes both files or folders of files dropped onto the applet
on open (these_items)
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if folder of the item_info is false then
process_item(this_item)
end if
end repeat
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if folder of the item_info is false then
process_item(this_item)
end if
end repeat
end process_folder
on trim_line(this_text, trim_chars, trim_indicator)
-- 0 = beginning, 1 = end, 2 = both
set x to the length of the trim_chars
-- TRIM BEGINNING
if the trim_indicator is in {0, 2} then
repeat while this_text begins with the trim_chars
try
set this_text to characters (x + 1) thru -1 of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
-- TRIM ENDING
if the trim_indicator is in {1, 2} then
repeat while this_text ends with the trim_chars
try
set this_text to characters 1 thru -(x + 1) of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return this_text
end trim_line
-- this sub-routine processes files
on process_item(this_item)
trim_line(this_item, " ", 2)
end process_item
Now, I realize that in my last subroutine, there’s no command that actually sets the name of the file being processed to the text returned by the trim_line handler, but that’s because I can’t figure out how to do it. I also realize, sort of, that in the last subroutine, “this_item” is a file reference in alias form, and I’ve read a bunch of stuff about file references, and dereferencing, but I still can’t make heads or tails of this. No matter which way I try to set the name of the file, AppleScript still reports that it can’t get the name of the alias.
Is there some kind soul out there willing to take on a charity case and help me figure out what I am doing?