Steph,
Sorry for the delay... just saw your last post - i check these boards somewhat regularly (at least lately), but this particular forum is so slow sometimes that i miss stuff.
As for some good sites for learning applescript, i like
Macscripter.net for news, scripting additions (osax) that supplement the built-in functionality of applescript, and especially for the Bulletin Board System - which has a wealth of information on troubleshooting applescript... essentially like these forums but dedicated to applescript questions. Most of the time, you'll get a quick response & some great examples for getting started and troubleshooting. And also check out the
Applescript Links section for dozens of more links. There's Bill Cheeseman's site,
AppleScript Sourcebook, another great site for examples, news, and the tips section is very good. And there's also Bill Briggs'
Applescript Primers which covers the ins & outs of various scripting routines. And lots more - Sherlock searches for applescript + your scripting term of interest often digs up postings from the classic Macscrpt Digest forums, which are usually very helpful & still applicable.
So, there's usually lots of great places for assistance, which i kind of wish i had taken advantage of, or known about, years ago.
About that script... it's no cakewalk in terms of scripting - there's a lot of elements that come into play; the trick is getting all of them to work well together. As you've probably noticed, there's usually any number of ways to set up a script to get it to do exactly what you want it to do - and it often just comes down to habits for performing certain tasks with certain routines. Sometimes you'll need indirect methods for getting a script to do what you need it to do, as the applescript language is not comprehensive (so the need for scripting additions), and a script will often force you to give it directions in exactly the format it wants to hear them.
Also, certain tasks are best delegated to, and may only be understood by, certain applications, each of which has it's own dictionary. You'll notice that scriptable apps and scripting additions are your friends... to get started with them, open their dictionaries (from Script Editor, File -> Open Dictionary..., then choose the application or scripting addition you're interested in). Sometimes the syntax is obvious, and sometimes it's obscure, poorly documented, and impossible to implement - maybe stay away from those ones. You'll also notice that without scripting additions only the Finder knows how to move/duplicate/rename/set information (not all) for files. It's often best to delegate responsibility for a task to the application that knows how to do that task best, then . And if the script can perform the task itself without telling another application, by all means let it. This is especially true of the Finder, which is notoriously slow in scripts, and appears to have bigger fish to fry... if you don't keep it continually busy in the script, it will force the script to stop.
One other thing, most of the time, you only need to address the application once to get it's attention (and usually in later versions of applescript, the application will automatically "activate" if you address it), and delegate responsibility as needed. If you're telling the same app to do do something else, you can omit the extra "tell" block. So you could say:
tell application "Finder"
select file filetoOpen
duplicate file filetoOpen to desktop
open file filetoOpen
end tell
In your additions to the script, The Finder can open the file, but of course then the frontmost app is SimpleText... so to get the Finder's attention again, you'd have to say something like:
tell application "Finder" to (do something)
Which you did, but unfortunately, the Finder only knows 2 meanings for "copy":
duplicate the file itself to some location, or
copy some data to a record of some sort
And so the Finder is left wondering where to copy the data, and what data to copy. To copy text from an open document would usually require that functionality in the application's dictionary (& SimpleText doesn't have that built-in dictionary) or a scripting addition. For example, there's a scripting addition that simulates keyboard activity, which would allow you to simulate pressing command-A (select all) and command-c (copy) which would put the text on the clipboard.
Another way to extract data (text) from a file is to use the "open for access", "read", and "write" commands from the "Standard Additions" scripting addition that is automatically installed with applescript. These commands allow you to open the data fork of the file, read it's contents & make them into an item (variable), and write them somewhere. This is what I've used in the example below... I omitted the part about opening the file, since you don't need it open in a text editor to get the text data from it, and neither the Finder nor SimpleText knows how to close the file... best way to do that would be to say:
tell application "SimpleText" to quit
Anyway, here's the script. Let me say that although I have FileMaker Pro 5, I have NEVER used it, so the part about FileMaker Pro is just an example - it will get the beginning of the text into a cell, but will stop after the first carriage return in the text. There are some secrets to entering data in FileMaker from a script that would take some time to work out. Specifically, you may need to tell it to import the text in blocks, using a delimiter of some sort (like returns). If I come up with anything, I'll let you know, as I'm interested in setting up some databases. There's also a handy built-in scripting functionality (as well as several examples somewhere in its folder) in FileMaker for running scripts from within the app. To set the name of the storage folder, the current date function is handy. Whomever told you applescript was an easy language to learn may have been thinking of something else.
Here's that script... again, change the names where indicated, & the FileMaker stuff needs some work to get it going. For some quick help with that, I've noticed there are a few seasoned FileMaker scripters over at the Macscripter.net bulletin boards (see link above).
Hope it helps...
Oh yeah; the separate handler (on something... in this case, "on process_File(filetoOpen)" is to separate out the text reading & FileMaker import stuff, so the script doesn't "remember" that info the next time it's run - variables tend to carry over between runs of the script unless you tell the script to deal with them only when requested (by the handler)
Good luck, & post back if you have any other questions.
acur
Code:
property file_Ext : ".txt"
property theFolder : alias "Macintosh HD :Documents:" (*change to the path to your folder of choice*)
set text_List to {} -- always start with an empty list of files
set filesList to list folder theFolder without invisibles
repeat with anItem in filesList -- find all files ending in file_Ext in theFolder
set theFile to (theFolder as string) & contents of anItem
set fileInfo to info for alias theFile
if the name of the fileInfo contains file_Ext then
copy name of fileInfo to the end of text_List
end if
end repeat
if (count of text_List) > 1 then
-- if multiple files ending in file_Ext found, choose the file(s) to open
set slctd_Files to (choose from list (text_List) with prompt "Please choose a text file to open" with multiple selections allowed) as list
repeat with aFile in slctd_Files -- opens all files selected in the dialog
set filetoOpen to (theFolder & aFile) as string
process_File(alias filetoOpen)
end repeat
else -- none found, or only one file ending in file_Ext found in theFolder
try
set filetoOpen to (theFolder & (text_List as string)) as string
process_File(alias filetoOpen)
on error -- no files found ending in file_Ext in theFolder
display dialog ("No files ending in '.txt' were found in the folder '" & theFolder & "'") buttons {"OK"} default button "OK" with icon 1
end try
end if
on process_File(filetoOpen) -- handler for getting text data from file & importing into FileMaker.
set fldr_Name to ("TextFiles " & (month of (current date) & (year of (current date)))) as string (*change "TextFiles" to your name of choice*)
set txtFile_Folder to (("Macintosh HD :Documents:TextFiles:" & fldr_Name) as string) (*change to the path to the folder where you want to store all the dated folders*)
set file_Data to open for access filetoOpen
set textInfo to read file_Data as text
close access file_Data
tell application "Finder"
if not (exists of alias txtFile_Folder) then -- if the dated folder for the current month isn't there, make it!
make folder at desktop with properties {name:txtFile_Folder}
end if
move file filetoOpen to alias txtFile_Folder
end tell
tell application "FileMaker Pro"
tell database "TextFileDB.fp5" (*change to the name of your FileMaker database*)
set new_Rec to create new record with data textInfo
end tell
end tell
end process_File
[This message has been edited by acur128 (edited 05-22-2001).]