I’m using OS X 10.2.7. Each week we receive a set of files from one of our clients. These files are grouped by sets of two into individual folders. Each folder contains a text (.TXT) file and a PDF. The names of the two files and their containing folder are a group number. For instance, a folder named 8888-0000 contains the files 8888-0000.TXT and 8888-0000.PDF.
Each week, I copy these folders into a main job folder. I must then open each text file and add a block of text to the beginning of it. The added text block is the same each week for each file.
Can anyone help me with the script I’m failing to write correctly? I need the script to follow these steps:
1 - ask me for the location of the main job folder
2 - add the block of text to each text file (that’s contained in a subfolder) using TextEdit
3 - save the changes to the text file
I can’t seem to figure out how to extract the subfolder name, add a “.TXT” to it, and have TextEdit open the file for alteration.
I’m a little confused by your last statement since there’s no need to use TextEdit at all for any of what you describe. Hopefully this will help (untested):
set main_Folder to (choose folder with prompt "Please select the main job folder')
tell application "Finder"
set subFolders to every folder of folder main_Folder
repeat with aFolder in subFolders
set job_name to name of aFolder
set text_file to open for access file (main_Folder & ":" & job_name & ":" & job_name & ".txt" as string) with write permission
set original_file_contents to read file text_file -- get current file contents
set EOF text_file to 0 -- reset the file contents
write "header to add to text file" to text_file
write original_file_contents to text_file
close access text_file
end repeat
end tell
The idea here is to find the name of the folder, then build a new pathname by appending that name to the main folder path. AppleScript can then open that file, read its current contents and then write new data to the file, overwriting the previous contents.
There is no need to use any external applications here other than the Finder
to get the folder listing.
I copied and pasted your text, making the necessary alteration (the “header text”). When I ran the script on a test folder on my local hard drive, I got the following error message:
“Finder got an error: File file Arcot Ramathorn:Test Folder:0407-0000:0407-0000.TXT is already open.”
The file 0407-0000.TXT is NOT open, however. I don’t understand this error. When it pops up, the following part of the script is highlighted:
“open for access file (main_Folder & “:” & job_name & “:” & job_name & “.TXT” as string) with write permission”
This script is a slight modification of the script above and can be saved as an application and then either run by double-clicking it or by dropping the job folder directly in its icon in the Finder:
You’ve no idea how much work you’re saving me. I’ve been so frustrated with this situation, I didn’t think about more obvious solutions to the problem. And you’re first script probably would have worked. That “already open” file was having access permission issues – I couldn’t delete it from my hard drive.