Earlier, the folks here helped me to create a script to rename files via appleScript from within a FileMaker Pro 6 script. Now I need another appleScript to merge text files, also called from the FMP6 script. Specifically I will have file A which is neverchanging, and is a “header” file; and I have file C which is also neverchanging and is the “footer” file. File B will be created by the FMP6 script and will be the “body” or middle text of the merged file. I need to merge the files, and save the resulting file under file B’s name. Below is the earlier script that was created to rename the created file.
tell application "Finder" to set deskPath to (path to desktop folder from user domain) as string
tell application "FileMaker Pro" to set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder" to set name of alias (deskPath & "collect:" & oldName) to newName
Reading and writing a text file is easy. I would read in the 3 files and then make one long text string out of that and then write it back to disk. Something like this will work…
set file1Path to "path:to:header:file"
set file2Path to "path:to:body:file"
set file3Path to "path:to:footer:file"
set file1Text to read file file1Path
set file2Text to read file file2Path
set file3Text to read file file3Path
set finalText to file1Text & return & file2Text & return & file3Text
writeTo(finalText, file2Path, false, string) -- this will over-write file2 with the new text
(*================= SUBROUTINE ====================*)
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
try
set target_file to target_file as Unicode text
if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as mode
close access the open_target_file
return true
on error
try
close access file open_target_file
end try
return false
end try
end writeTo
Thanks for the script. The only part I’m not clear on is the second line of the subroutine, ‘if target_file does not contain “:” then set target_file to POSIX file target_file as Unicode text’
Please explain why you check for a colon, and also what a POSIX file is.
The mac has 4 ways of specifying file paths. Here’s the 4 ways for a file on my desktop called aaa.txt.
a file specification (note the word “file” in the following example)
file “Leopard:Users:hmcshane:Desktop:aaa.txt”
a Finder specification. The Finder uses it’s own unique way to detail the path to a file.
document file “aaa.txt” of folder “Desktop” of folder “hmcshane” of folder “Users” of startup disk of application “Finder”
a mac text path which uses colons to separate the paths
Leopard:Users:hmcshane:Desktop:aaa.txt
a unix text path using slashes
/Users/hmcshane/Desktop/aaa.txt
So I wrote the subroutine to accept any of those 4 types. I really want a path in the form of example 3 above so the first 2 lines of the script will convert any of the 4 examples into exmple 3. I did this so I can use the subroutine in any script I may write and have confidence that the subroutine will continue to work.
Since I want example 3 style, I need to convert 1, 2, and 4 into 3. The first line of the subroutine handles 1 and 2. Basically if you coerce 1 or 2 into text you end up with example 3. So after the first line I now know I have either example 3 or example 4. The second line of the subroutine handles example 4. It changes a unix style path into a mac path. That’s the “POSIX file” part. Using posix file you change example 4 into example 1 above, then I coerce that into text… which gives me example 3.
File paths can be very confusing because there’s the different types that I explained. If your applescript command expects one type and you feed it a different type you get errors… and to reduce errors in my own scripts I prefer to work with style 3 so I learned these conversions early on in my scripting.
The following appleScript works through the script editor, but fails when I try to add it to a FileMaker Pro script.
set file1Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:review begin part"
set file2Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:ssr_h1"
set file3Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:ssr"
set file4Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:review end part"
set file5Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:round 1:ssr.php"
set file1Text to read file file1Path
set file2Text to read file file2Path
set file3Text to read file file3Path
set file4Text to read file file4Path
set finalText to file1Text & return & file2Text & return & file3Text & return & file4Text
writeTo(finalText, file5Path, false, string)
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
try
set target_file to target_file as Unicode text
if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as mode
close access the open_target_file
return true
on error
try
close access file open_target_file
end try
return false
end try
end writeTo
FMP gives me an error message that says, “Expected end of line, etc. but found class name.” and indicates that the error occurs at the word “file” on the line that reads, “set file1Text to read file file1Path.”
This occurs both with FMP 6 and with the latest version 10.
There are a few AppleScript (actually Standard Additions) commands which FileMaker does not like. Read and write file is one of them. It is easy enough to avoid. Put them within a tell block to some other application. Best one is probably System Events. Finder is OK, if not a lengthy operation.
I believe the write file subroutine would be OK, as any subroutine is outside the (implied) FileMaker tell block.
Tell application "System Events"
set file1Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:review begin part"
set file2Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:ssr_h1"
set file3Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:ssr"
set file4Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:review end part"
set file5Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:round 1:ssr.php"
set file1Text to read file file1Path
set file2Text to read file file2Path
set file3Text to read file file3Path
set file4Text to read file file4Path
set finalText to file1Text & return & file2Text & return & file3Text & return & file4Text
writeTo(finalText, file5Path, false, string)
end Tell
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
try
set target_file to target_file as Unicode text
if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as mode
close access the open_target_file
return true
on error
try
close access file open_target_file
end try
return false
end try
end writeTo
But I got the error message …
error “System Events got an error: Can’t continue writeTo.” number -1708
Thanks Stefan. That satisfies the appleScript editor, but FMP still balks at it. Here is the refined script…
tell application "System Events"
set file1Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:review begin part"
set file2Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:ssr_h1"
set file3Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:ssr"
set file4Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:review end part"
set file5Path to "Macintosh HD:Users:chartley:Desktop:academic web files:review files:round 1:ssr.php"
set file1Text to read file file1Path
set file2Text to read file file2Path
set file3Text to read file file3Path
set file4Text to read file file4Path
set finalText to file1Text & return & file2Text & return & file3Text & return & file4Text
end tell
writeTo(finalText, file5Path, false, string)
on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
try
set target_file to target_file as Unicode text
if target_file does not contain ":" then set target_file to POSIX file target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as mode
close access the open_target_file
return true
on error
try
close access file open_target_file
end try
return false
end try
end writeTo
FMP gives me the error message “Expected end of line, etc. but found identifer.” and highlighted “this_data” in the line…
write this_data to the open_target_file starting at eof as mode