Hey everyone,
I need to make a script that does a find and replace on every text file thats dropped into a folder. It should place the modified file in a folder called “_Converted_Files” which is inside the folder that has the folder action script on it. At first, I created a script that prompted me for the source folder and a destination folder, and the find and replace functionality worked perfectly. The next step was to make the script work as a folder action. I tried the code pasted below, but all I get is a little flicker indicating something is happening, but the file doesnt get modified and doesnt show up in the destination folder. Can someone help me figure out whats wrong with my code? Many thanks in advance.
set findVar to "oldtext"
set replaceVar to "newtext"
on adding folder items to this_folder after receiving these_items
try
tell application "Finder"
set the destinationFolder to folder "_Converted_Files" of this_folder as alias
set the POSIX_destinationFolder to POSIX path of the destinationFolder
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set fileName to name of this_item
set POSIX_this_item to POSIX path of this_item
set theCommand to "sed 's_" & quoted form of findVar & "_" & quoted form of replaceVar & "_' <" & quoted form of POSIX_this_item & " >" & quoted form of POSIX_destinationFolder & "/" & quoted form of fileName
try
do shell script theCommand
end try
end repeat
end tell
end try
end adding folder items to
AppleScript: 11.0.7
Browser: Firefox 3.0b4
Operating System: Mac OS X (10.4)
Hi,
maybe too many quoted of
.
set theCommand to "sed 's_" & quoted form of findVar & "_" & quoted form of replaceVar & "_' <" & quoted form of POSIX_this_item & " >" & quoted form of (POSIX_destinationFolder & "/" & fileName)
.
Hi Stefan, thanks for the idea, but turns out the main problem was the findVar and replaceVar variables being set outside of the on handler. When I moved them inside the tell block, things started falling into place. Heres the new code, which works, but I have a new question to go with it.
At this point I need to search the files inside the “_Converted_Playlists” folder and check to see if the file name of the current file exists there. The output of the sed command will overwrite the existing file in that folder, so if a file with that name exists, I want to prepend the output name of the new (old or old file, whichevers easier) with a mark, like an underscore. Any suggestions on the best way to do this?
on adding folder items to thisFolder after receiving theseItems
try
tell application "Finder"
set findVar to "oldtext"
set replaceVar to "newtext"
if not (exists folder "_Converted_Playlists" of thisFolder) then
make new folder at thisFolder with properties {name:"_Converted_Playlists"}
end if
set destinationFolder to folder "_Converted_Playlists" of thisFolder as alias
set POSIX_destinationFolder to the POSIX path of the destinationFolder
repeat with i from 1 to number of items in theseItems
set thisItem to item i of theseItems
set fileName to name of thisItem
set POSIX_thisItem to the POSIX path of thisItem
if fileName contains "xml" then
--create code here to search _converted_Playlists for file of same name. prepend with "_" if exists
set theCommand to "sed 's_" & quoted form of findVar & "_" & quoted form of replaceVar & "_' <" & quoted form of POSIX_thisItem & " >" & quoted form of POSIX_destinationFolder & "/" & quoted form of fileName
try
do shell script theCommand
end try
move thisItem to the trash
end if
end repeat
end tell
end try
end adding folder items to