I’m diving into AppleScripting to solve an immediate, simple problem and I’m having a very difficult tme finding decent reference materials! It’s like Apple has this tool but forces you to buy the development studio in order to get the reference materials required to learn it!!
Anyhow, I’m trying to figure out what the proper script code would be to open a document located in a particular directory using a particular program. I’ve found some references to this sort of thing but haven’t quite been able to sort it out (basically, I’m asking someone to do my work for me - I’m man enough to admit it).
Welcome to MacScripter, and don’t worry, everyone starts somewhere.
Yes, initially, Applescript can seem frustrating, but here is a free document to help you learn some of the basic commands and syntax.
Typically, you would “tell” an application to open, or open a file. If you know that the default application that the file is associated with is the correct application, use this little script to open it:
set a to choose file
tell application "Finder" to open a
In any case, this is definitely the forum to help you out. Why don’t you offer up a few more specifics, and see what assistance comes your way.
tell application "Finder"
activate
open document file "myletter.db" of folder "Clients & Profits" of folder "Applications" of startup disk using application file ((path to applications folder as Unicode text) & "TextEdit.app")
end tell
I have two questions related to this:
Am I specifying the location of myletter.db in the most efficient way? It seems kind of weird to specify the path as “of folder of foleder blah blah blah”; I would’ve expected something more like Applications:Clients & Profits:myletter.db
Secondly, is the “(path to applications folder as Unicode text)” valid? Again, it seems very fuzzy to me and, since I snagged it from another posting, I’m unsure if that’s what the syntax is supposed to be or if it’s a placeholder for what the code’s supposed to be!
Yes, this is valid. It makes the path text for you.
It is a bit tedious to say set thisFolder to folder “a” of folder “b” of da de da, but that’s the way to do it. You can also write it in the form
set theFile to "yourHardDriveName:Users:yourUserName:Desktop:fileName"
When you use this method a folder has a colon after the name and a file does not. Also if you “tell” the finder to open the file you reference it as such
tell application "Finder"
open file theFile -- or open folder if it is a folder
end tell
You can try this. It takes less typing and enables you to change the file to be opened.
set theChoice to choose file with prompt "Choose file to open." -- you can also add "without invisibles" so you don't see the invisible files.
tell application "Finder"
open theChoice
end tell
If you’re using the ‘path to’ command to locate a specified folder, it’s a good idea to keep it out of an application tell statement where possible. Some applications (notably System Events) can sometimes screw up the results.
Also, unless you have more than one version of an application on board, and wish to use one in particular, specifying the path to the app isn’t normally necessary. Neither is using the Finder. You could, for example, simply do something like this:
set targetFile to (path to applications folder as Unicode text) & "Clients & Profits:myletter.db"
tell application "TextEdit"
activate
open targetFile
end tell
(Incidentally, while it’s not for me to comment on the location of document files, you could consider ‘path to documents folder’ as an alternative…)